blob: 98885eec55bd1cb9c92793e201e265638c8a8c8f [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"
Chris Jonesdaacb592021-03-09 17:03:29 +000081#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000083#include "pk_wrap.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:
Gilles Peskinea5905292018-02-07 20:59:33 +0100154 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9a944802018-06-21 09:35:35 +0200155 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
156 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
157 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
158 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
159 case MBEDTLS_ERR_ASN1_INVALID_DATA:
160 return( PSA_ERROR_INVALID_ARGUMENT );
161 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
162 return( PSA_ERROR_INSUFFICIENT_MEMORY );
163 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
164 return( PSA_ERROR_BUFFER_TOO_SMALL );
165
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100169 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
170 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
181 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100182
Gilles Peskine26869f22019-05-06 15:25:00 +0200183 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
184 return( PSA_ERROR_INVALID_ARGUMENT );
185
186 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
187 return( PSA_ERROR_BAD_STATE );
188 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
189 return( PSA_ERROR_INVALID_SIGNATURE );
190
Gilles Peskinea5905292018-02-07 20:59:33 +0100191 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
192 return( PSA_ERROR_NOT_SUPPORTED );
193 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
194 return( PSA_ERROR_INVALID_ARGUMENT );
195 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
196 return( PSA_ERROR_INSUFFICIENT_MEMORY );
197 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
198 return( PSA_ERROR_INVALID_PADDING );
199 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200200 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
202 return( PSA_ERROR_INVALID_SIGNATURE );
203 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200204 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100205
Gilles Peskine82e57d12020-11-13 21:31:17 +0100206#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
207 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100208 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
209 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
211 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
212 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
213 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
214 return( PSA_ERROR_NOT_SUPPORTED );
215 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
216 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100217#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100218
219 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
220 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100221
Gilles Peskinee59236f2018-01-27 23:32:46 +0100222 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
223 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
224 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
225 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100226
227 case MBEDTLS_ERR_GCM_AUTH_FAILED:
228 return( PSA_ERROR_INVALID_SIGNATURE );
229 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200230 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
233 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100234 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
235 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
237 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
238 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
239 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
240 return( PSA_ERROR_NOT_SUPPORTED );
241 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
242 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
243#endif
244
Gilles Peskinea5905292018-02-07 20:59:33 +0100245 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
248 return( PSA_ERROR_INVALID_ARGUMENT );
249 case MBEDTLS_ERR_MD_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
252 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294
Gilles Peskineff2d2002019-05-06 15:26:23 +0200295 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
298 return( PSA_ERROR_NOT_SUPPORTED );
299
Gilles Peskinea5905292018-02-07 20:59:33 +0100300 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_RSA_INVALID_PADDING:
303 return( PSA_ERROR_INVALID_PADDING );
304 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
307 return( PSA_ERROR_INVALID_ARGUMENT );
308 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
309 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200310 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100311 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
312 return( PSA_ERROR_INVALID_SIGNATURE );
313 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
314 return( PSA_ERROR_BUFFER_TOO_SMALL );
315 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100316 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100317
318 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
319 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320
itayzafrir5c753392018-05-08 11:18:38 +0300321 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300322 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300323 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300324 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
325 return( PSA_ERROR_BUFFER_TOO_SMALL );
326 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
327 return( PSA_ERROR_NOT_SUPPORTED );
328 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
329 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
330 return( PSA_ERROR_INVALID_SIGNATURE );
331 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
332 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100333 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
334 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100335
Janos Follatha13b9052019-11-22 12:48:59 +0000336 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
337 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300338
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 default:
David Saadab4ecc272019-02-14 13:48:10 +0200340 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 }
342}
343
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200344
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200345
346
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100347/****************************************************************/
348/* Key management */
349/****************************************************************/
350
Gilles Peskine73167e12019-07-12 23:44:37 +0200351#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
352static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
353{
Gilles Peskine8e338702019-07-30 20:06:31 +0200354 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200355}
356#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
357
John Durkop07cc04a2020-11-16 22:08:34 -0800358/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
359 * current test driver in key_management.c is using this function
360 * when accelerators are used for ECC key pair and public key.
361 * Once that dependency is resolved these guards can be removed.
362 */
John Durkop9814fa22020-11-04 12:28:15 -0800363#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800364 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
365 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
366 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100367mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100368 size_t bits,
369 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200370{
371 switch( curve )
372 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100373 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100374 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100375 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100376#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100377 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100378 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100379#endif
380#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100381 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100382 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100383#endif
384#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100385 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100386 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100387#endif
388#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100389 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100390 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100391#endif
392#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100393 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100394 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 528:
396 if( bits_is_sloppy )
397 return( MBEDTLS_ECP_DP_SECP521R1 );
398 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100399#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100400 }
401 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100402
Paul Elliott8ff510a2020-06-02 17:19:28 +0100403 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100404 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100405 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100406#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100408 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100409#endif
410#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100413#endif
414#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100415 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100416 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100417#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100418 }
419 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100420
Paul Elliott8ff510a2020-06-02 17:19:28 +0100421 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100422 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100423 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100424#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 case 256:
428 if( bits_is_sloppy )
429 return( MBEDTLS_ECP_DP_CURVE25519 );
430 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100431#endif
432#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100434 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100435#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100436 }
437 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100438
Paul Elliott8ff510a2020-06-02 17:19:28 +0100439 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100440 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100441 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100442#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100444 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100445#endif
446#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100448 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#endif
450#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100452 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100453#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100454 }
455 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200456 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100458 (void) bits_is_sloppy;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200460}
John Durkop9814fa22020-11-04 12:28:15 -0800461#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800462 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
463 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
464 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200465
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200466static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
467 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200468{
469 /* Check that the bit size is acceptable for the key type */
470 switch( type )
471 {
472 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200473 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200474 case PSA_KEY_TYPE_DERIVE:
475 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100476#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 case PSA_KEY_TYPE_AES:
478 if( bits != 128 && bits != 192 && bits != 256 )
479 return( PSA_ERROR_INVALID_ARGUMENT );
480 break;
481#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100482#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200483 case PSA_KEY_TYPE_CAMELLIA:
484 if( bits != 128 && bits != 192 && bits != 256 )
485 return( PSA_ERROR_INVALID_ARGUMENT );
486 break;
487#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100488#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489 case PSA_KEY_TYPE_DES:
490 if( bits != 64 && bits != 128 && bits != 192 )
491 return( PSA_ERROR_INVALID_ARGUMENT );
492 break;
493#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100494#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 case PSA_KEY_TYPE_ARC4:
496 if( bits < 8 || bits > 2048 )
497 return( PSA_ERROR_INVALID_ARGUMENT );
498 break;
499#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100500#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200501 case PSA_KEY_TYPE_CHACHA20:
502 if( bits != 256 )
503 return( PSA_ERROR_INVALID_ARGUMENT );
504 break;
505#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506 default:
507 return( PSA_ERROR_NOT_SUPPORTED );
508 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200509 if( bits % 8 != 0 )
510 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200512 return( PSA_SUCCESS );
513}
514
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100515/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100516 *
Steven Cooremancd640932021-03-08 12:00:27 +0100517 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
518 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100519 *
520 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100521 * \param[in] key_type The key type of the key to be used with the
522 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100523 *
524 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100525 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100526 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100527 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100528 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100529MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100530 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100531 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100532{
Steven Cooremancd640932021-03-08 12:00:27 +0100533 if( PSA_ALG_IS_HMAC( algorithm ) )
534 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100535 if( key_type == PSA_KEY_TYPE_HMAC )
536 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100537 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100538
539 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100540 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100541 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
542 * key. */
543 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
544 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
545 {
546 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
547 * the block length (larger than 1) for block ciphers. */
548 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
549 return( PSA_SUCCESS );
550 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100551 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100552
553 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554}
555
Steven Cooreman75b74362020-07-28 14:30:13 +0200556/** Try to allocate a buffer to an empty key slot.
557 *
558 * \param[in,out] slot Key slot to attach buffer to.
559 * \param[in] buffer_length Requested size of the buffer.
560 *
561 * \retval #PSA_SUCCESS
562 * The buffer has been successfully allocated.
563 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
564 * Not enough memory was available for allocation.
565 * \retval #PSA_ERROR_ALREADY_EXISTS
566 * Trying to allocate a buffer to a non-empty key slot.
567 */
568static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
569 size_t buffer_length )
570{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100571 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200572 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200573
Ronald Cronea0f8a62020-11-25 17:52:23 +0100574 slot->key.data = mbedtls_calloc( 1, buffer_length );
575 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200576 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200577
Ronald Cronea0f8a62020-11-25 17:52:23 +0100578 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200579 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200580}
581
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200582psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
583 const uint8_t* data,
584 size_t data_length )
585{
586 psa_status_t status = psa_allocate_buffer_to_slot( slot,
587 data_length );
588 if( status != PSA_SUCCESS )
589 return( status );
590
Ronald Cronea0f8a62020-11-25 17:52:23 +0100591 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200592 return( PSA_SUCCESS );
593}
594
Ronald Crona0fe59f2020-11-29 11:14:53 +0100595psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100596 const psa_key_attributes_t *attributes,
597 const uint8_t *data, size_t data_length,
598 uint8_t *key_buffer, size_t key_buffer_size,
599 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
602 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200604 /* zero-length keys are never supported. */
605 if( data_length == 0 )
606 return( PSA_ERROR_NOT_SUPPORTED );
607
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100608 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100610 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200611
Steven Cooreman75b74362020-07-28 14:30:13 +0200612 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
613 if( data_length > SIZE_MAX / 8 )
614 return( PSA_ERROR_NOT_SUPPORTED );
615
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200616 /* Enforce a size limit, and in particular ensure that the bit
617 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100618 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200619 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200620
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100621 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200622 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200623 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200624
Ronald Crondd04d422020-11-28 15:14:42 +0100625 /* Copy the key material. */
626 memcpy( key_buffer, data, data_length );
627 *key_buffer_length = data_length;
628 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629
Steven Cooreman40120f62020-10-29 11:42:22 +0100630 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100631 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100632 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200633 {
John Durkop9814fa22020-11-04 12:28:15 -0800634#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
635 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100636 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200637 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100638 return( mbedtls_psa_ecp_import_key( attributes,
639 data, data_length,
640 key_buffer, key_buffer_size,
641 key_buffer_length,
642 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100643 }
John Durkop9814fa22020-11-04 12:28:15 -0800644#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
645 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700646#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
647 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100648 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200649 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100650 return( mbedtls_psa_rsa_import_key( attributes,
651 data, data_length,
652 key_buffer, key_buffer_size,
653 key_buffer_length,
654 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200655 }
John Durkop9814fa22020-11-04 12:28:15 -0800656#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
657 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100658 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100659
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100660 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100661}
662
Gilles Peskinef603c712019-01-19 13:40:11 +0100663/** Calculate the intersection of two algorithm usage policies.
664 *
665 * Return 0 (which allows no operation) on incompatibility.
666 */
667static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100668 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100669 psa_algorithm_t alg1,
670 psa_algorithm_t alg2 )
671{
Gilles Peskine549ea862019-05-22 11:45:59 +0200672 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100673 if( alg1 == alg2 )
674 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100675 /* If the policies are from the same hash-and-sign family, check
676 * if one is a wildcard. If so the other has the specific algorithm. */
677 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
678 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
679 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100680 {
Steven Cooreman03488022021-02-18 12:07:20 +0100681 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
682 return( alg2 );
683 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
684 return( alg1 );
685 }
686 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100687 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100688 * restrictive tag length. */
689 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
690 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
691 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
692 {
693 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
694 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100695 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100696
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100697 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100698 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
699 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100700 {
Steven Cooremancd640932021-03-08 12:00:27 +0100701 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
702 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100703 }
704 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100705 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100706 ( alg1_len <= alg2_len ) )
707 {
708 return( alg2 );
709 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100710 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100711 ( alg2_len <= alg1_len ) )
712 {
713 return( alg1 );
714 }
715 }
716 /* If the policies are from the same MAC family, check whether one
717 * of them is a minimum-MAC-length policy. Calculate the most
718 * restrictive tag length. */
719 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
720 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
721 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
722 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100723 /* Validate the combination of key type and algorithm. Since the base
724 * algorithm of alg1 and alg2 are the same, we only need this once. */
725 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100726 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100727
Steven Cooreman31a876d2021-03-03 20:47:40 +0100728 /* Get the (exact or at-least) output lengths for both sides of the
729 * requested intersection. None of the currently supported algorithms
730 * have an output length dependent on the actual key size, so setting it
731 * to a bogus value of 0 is currently OK.
732 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100733 * Note that for at-least-this-length wildcard algorithms, the output
734 * length is set to the shortest allowed length, which allows us to
735 * calculate the most restrictive tag length for the intersection. */
736 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
737 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100738 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100739
Steven Cooremana1d83222021-02-25 10:20:29 +0100740 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100741 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
742 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100743 {
Steven Cooremancd640932021-03-08 12:00:27 +0100744 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100745 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100746
747 /* If only one is an at-least-this-length policy, the intersection would
748 * be the other (fixed-length) policy as long as said fixed length is
749 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100750 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100751 {
Steven Cooremancd640932021-03-08 12:00:27 +0100752 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100753 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100754 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100755 {
Steven Cooremancd640932021-03-08 12:00:27 +0100756 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100757 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100758
Steven Cooremancd640932021-03-08 12:00:27 +0100759 /* If none of them are wildcards, check whether they define the same tag
760 * length. This is still possible here when one is default-length and
761 * the other specific-length. Ensure to always return the
762 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100763 if( alg1_len == alg2_len )
764 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100765 }
766 /* If the policies are incompatible, allow nothing. */
767 return( 0 );
768}
769
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100770static int psa_key_algorithm_permits( psa_key_type_t key_type,
771 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200772 psa_algorithm_t requested_alg )
773{
Gilles Peskine549ea862019-05-22 11:45:59 +0200774 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200775 if( requested_alg == policy_alg )
776 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100777 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
778 * and requested_alg is the same hash-and-sign family with any hash,
779 * then requested_alg is compliant with policy_alg. */
780 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
781 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200782 {
Steven Cooreman03488022021-02-18 12:07:20 +0100783 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
784 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
785 }
786 /* If policy_alg is a wildcard AEAD algorithm of the same base as
787 * the requested algorithm, check the requested tag length to be
788 * equal-length or longer than the wildcard-specified length. */
789 if( PSA_ALG_IS_AEAD( policy_alg ) &&
790 PSA_ALG_IS_AEAD( requested_alg ) &&
791 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
792 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100793 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100794 {
795 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
796 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
797 }
Steven Cooremancd640932021-03-08 12:00:27 +0100798 /* If policy_alg is a MAC algorithm of the same base as the requested
799 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100800 if( PSA_ALG_IS_MAC( policy_alg ) &&
801 PSA_ALG_IS_MAC( requested_alg ) &&
802 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100803 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100804 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100805 /* Validate the combination of key type and algorithm. Since the policy
806 * and requested algorithms are the same, we only need this once. */
807 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100808 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100809
Steven Cooreman31a876d2021-03-03 20:47:40 +0100810 /* Get both the requested output length for the algorithm which is to be
811 * verified, and the default output length for the base algorithm.
812 * Note that none of the currently supported algorithms have an output
813 * length dependent on actual key size, so setting it to a bogus value
814 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100815 size_t requested_output_length = PSA_MAC_LENGTH(
816 key_type, 0, requested_alg );
817 size_t default_output_length = PSA_MAC_LENGTH(
818 key_type, 0,
819 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100820
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100821 /* If the policy is default-length, only allow an algorithm with
822 * a declared exact-length matching the default. */
823 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100824 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100825
826 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100827 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100828 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
829 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
830 {
831 return( 1 );
832 }
833
Steven Cooremancd640932021-03-08 12:00:27 +0100834 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
835 * check for the requested MAC length to be equal to or longer than the
836 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100837 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
838 {
839 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100840 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100841 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200842 }
Steven Cooremance48e852020-10-05 16:02:45 +0200843 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200844 * a key derivation with that key agreement should also be allowed. This
845 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200846 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
847 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
848 {
849 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
850 policy_alg );
851 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100852 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200853 return( 0 );
854}
855
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100856/** Test whether a policy permits an algorithm.
857 *
858 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100859 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100860 * \note This function requires providing the key type for which the policy is
861 * being validated, since some algorithm policy definitions (e.g. MAC)
862 * have different properties depending on what kind of cipher it is
863 * combined with.
864 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100865 * \retval PSA_SUCCESS When \p alg is a specific algorithm
866 * allowed by the \p policy.
867 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
868 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
869 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100870 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100871static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100872 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100873 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100874{
Steven Cooremand788fab2021-02-25 11:29:17 +0100875 /* '0' is not a valid algorithm */
876 if( alg == 0 )
877 return( PSA_ERROR_INVALID_ARGUMENT );
878
Steven Cooreman7e39f052021-02-23 14:18:32 +0100879 /* A requested algorithm cannot be a wildcard. */
880 if( PSA_ALG_IS_WILDCARD( alg ) )
881 return( PSA_ERROR_INVALID_ARGUMENT );
882
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100883 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
884 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100885 return( PSA_SUCCESS );
886 else
887 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100888}
889
Gilles Peskinef603c712019-01-19 13:40:11 +0100890/** Restrict a key policy based on a constraint.
891 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100892 * \note This function requires providing the key type for which the policy is
893 * being restricted, since some algorithm policy definitions (e.g. MAC)
894 * have different properties depending on what kind of cipher it is
895 * combined with.
896 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100897 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100898 * \param[in,out] policy The policy to restrict.
899 * \param[in] constraint The policy constraint to apply.
900 *
901 * \retval #PSA_SUCCESS
902 * \c *policy contains the intersection of the original value of
903 * \c *policy and \c *constraint.
904 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100905 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100906 * \c *policy is unchanged.
907 */
908static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100909 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100910 psa_key_policy_t *policy,
911 const psa_key_policy_t *constraint )
912{
913 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100914 psa_key_policy_algorithm_intersection( key_type, policy->alg,
915 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200916 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100917 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
918 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100919 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
920 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200921 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
922 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100923 policy->usage &= constraint->usage;
924 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200925 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100926 return( PSA_SUCCESS );
927}
928
Ronald Cron5c522922020-11-14 16:35:34 +0100929/** Get the description of a key given its identifier and policy constraints
930 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200931 *
Ronald Cron5c522922020-11-14 16:35:34 +0100932 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100933 * nonzero, the key must allow operations with this algorithm. If \p alg is
934 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100935 *
Ronald Cron5c522922020-11-14 16:35:34 +0100936 * In case of a persistent key, the function loads the description of the key
937 * into a key slot if not already done.
938 *
939 * On success, the returned key slot is locked. It is the responsibility of
940 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200941 */
Ronald Cron5c522922020-11-14 16:35:34 +0100942static psa_status_t psa_get_and_lock_key_slot_with_policy(
943 mbedtls_svc_key_id_t key,
944 psa_key_slot_t **p_slot,
945 psa_key_usage_t usage,
946 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100947{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200948 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
949 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100950
Ronald Cron5c522922020-11-14 16:35:34 +0100951 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100952 if( status != PSA_SUCCESS )
953 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200954 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100955
956 /* Enforce that usage policy for the key slot contains all the flags
957 * required by the usage parameter. There is one exception: public
958 * keys can always be exported, so we treat public key objects as
959 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200960 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100961 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200962
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100964 {
965 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200966 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100967 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100968
969 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100970 if( alg != 0 )
971 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100972 status = psa_key_policy_permits( &slot->attr.policy,
973 slot->attr.type,
974 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +0100975 if( status != PSA_SUCCESS )
976 goto error;
977 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100978
Darryl Green06fd18d2018-07-16 11:21:11 +0100979 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200980
981error:
982 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100983 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200984
985 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100986}
Darryl Green940d72c2018-07-13 13:18:51 +0100987
Ronald Cron5c522922020-11-14 16:35:34 +0100988/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200989 *
990 * A transparent key is a key for which the key material is directly
991 * available, as opposed to a key in a secure element.
992 *
Ronald Cron5c522922020-11-14 16:35:34 +0100993 * This is a temporary function to use instead of
994 * psa_get_and_lock_key_slot_with_policy() until secure element support is
995 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200996 *
Ronald Cron5c522922020-11-14 16:35:34 +0100997 * On success, the returned key slot is locked. It is the responsibility of the
998 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200999 */
1000#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001001static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1002 mbedtls_svc_key_id_t key,
1003 psa_key_slot_t **p_slot,
1004 psa_key_usage_t usage,
1005 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001006{
Ronald Cron5c522922020-11-14 16:35:34 +01001007 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1008 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001009 if( status != PSA_SUCCESS )
1010 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001011
Gilles Peskineadad8132019-07-25 11:31:23 +02001012 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001013 {
Ronald Cron5c522922020-11-14 16:35:34 +01001014 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001015 *p_slot = NULL;
1016 return( PSA_ERROR_NOT_SUPPORTED );
1017 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001018
Gilles Peskine28f8f302019-07-24 13:30:31 +02001019 return( PSA_SUCCESS );
1020}
1021#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1022/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001023#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1024 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001025#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1026
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001027/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001028static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001029{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001030 /* Data pointer will always be either a valid pointer or NULL in an
1031 * initialized slot, so we can just free it. */
1032 if( slot->key.data != NULL )
1033 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1034
1035 mbedtls_free( slot->key.data );
1036 slot->key.data = NULL;
1037 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001038
1039 return( PSA_SUCCESS );
1040}
1041
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001042/** Completely wipe a slot in memory, including its policy.
1043 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001044psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001045{
1046 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001047
1048 /*
1049 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001050 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001051 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1052 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001053 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001054 */
Ronald Cron5c522922020-11-14 16:35:34 +01001055 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001056 {
1057#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001058 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001059#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001060 status = PSA_ERROR_CORRUPTION_DETECTED;
1061 }
1062
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001063 /* Multipart operations may still be using the key. This is safe
1064 * because all multipart operation objects are independent from
1065 * the key slot: if they need to access the key after the setup
1066 * phase, they have a copy of the key. Note that this means that
1067 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001068 /* At this point, key material and other type-specific content has
1069 * been wiped. Clear remaining metadata. We can call memset and not
1070 * zeroize because the metadata is not particularly sensitive. */
1071 memset( slot, 0, sizeof( *slot ) );
1072 return( status );
1073}
1074
Ronald Croncf56a0a2020-08-04 09:51:30 +02001075psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001076{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001077 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001078 psa_status_t status; /* status of the last operation */
1079 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001080#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1081 psa_se_drv_table_entry_t *driver;
1082#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001083
Ronald Croncf56a0a2020-08-04 09:51:30 +02001084 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001085 return( PSA_SUCCESS );
1086
Ronald Cronf2911112020-10-29 17:51:10 +01001087 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001088 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001089 * key, this will load the key description from persistent memory if not
1090 * done yet. We cannot avoid this loading as without it we don't know if
1091 * the key is operated by an SE or not and this information is needed by
1092 * the current implementation.
1093 */
Ronald Cron5c522922020-11-14 16:35:34 +01001094 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001095 if( status != PSA_SUCCESS )
1096 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001097
Ronald Cronf2911112020-10-29 17:51:10 +01001098 /*
1099 * If the key slot containing the key description is under access by the
1100 * library (apart from the present access), the key cannot be destroyed
1101 * yet. For the time being, just return in error. Eventually (to be
1102 * implemented), the key should be destroyed when all accesses have
1103 * stopped.
1104 */
Ronald Cron5c522922020-11-14 16:35:34 +01001105 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001106 {
Ronald Cron5c522922020-11-14 16:35:34 +01001107 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001108 return( PSA_ERROR_GENERIC_ERROR );
1109 }
1110
Gilles Peskine354f7672019-07-12 23:46:38 +02001111#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001112 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001113 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001114 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001115 /* For a key in a secure element, we need to do three things:
1116 * remove the key file in internal storage, destroy the
1117 * key inside the secure element, and update the driver's
1118 * persistent data. Start a transaction that will encompass these
1119 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001120 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001121 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001122 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001123 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001124 status = psa_crypto_save_transaction( );
1125 if( status != PSA_SUCCESS )
1126 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001127 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001128 /* We should still try to destroy the key in the secure
1129 * element and the key metadata in storage. This is especially
1130 * important if the error is that the storage is full.
1131 * But how to do it exactly without risking an inconsistent
1132 * state after a reset?
1133 * https://github.com/ARMmbed/mbed-crypto/issues/215
1134 */
1135 overall_status = status;
1136 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001137 }
1138
Ronald Cronea0f8a62020-11-25 17:52:23 +01001139 status = psa_destroy_se_key( driver,
1140 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001141 if( overall_status == PSA_SUCCESS )
1142 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001143 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001144#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1145
Darryl Greend49a4992018-06-18 17:27:26 +01001146#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001147 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001148 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001149 status = psa_destroy_persistent_key( slot->attr.id );
1150 if( overall_status == PSA_SUCCESS )
1151 overall_status = status;
1152
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001153 /* TODO: other slots may have a copy of the same key. We should
1154 * invalidate them.
1155 * https://github.com/ARMmbed/mbed-crypto/issues/214
1156 */
Darryl Greend49a4992018-06-18 17:27:26 +01001157 }
1158#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001159
Gilles Peskinefc762652019-07-22 19:30:34 +02001160#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1161 if( driver != NULL )
1162 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001163 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001164 if( overall_status == PSA_SUCCESS )
1165 overall_status = status;
1166 status = psa_crypto_stop_transaction( );
1167 if( overall_status == PSA_SUCCESS )
1168 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001169 }
1170#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1171
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001172#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1173exit:
1174#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001175 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001176 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1177 if( overall_status == PSA_SUCCESS )
1178 overall_status = status;
1179 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001180}
1181
John Durkop07cc04a2020-11-16 22:08:34 -08001182#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001183 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001184static psa_status_t psa_get_rsa_public_exponent(
1185 const mbedtls_rsa_context *rsa,
1186 psa_key_attributes_t *attributes )
1187{
1188 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001190 uint8_t *buffer = NULL;
1191 size_t buflen;
1192 mbedtls_mpi_init( &mpi );
1193
1194 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1195 if( ret != 0 )
1196 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001197 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1198 {
1199 /* It's the default value, which is reported as an empty string,
1200 * so there's nothing to do. */
1201 goto exit;
1202 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001203
1204 buflen = mbedtls_mpi_size( &mpi );
1205 buffer = mbedtls_calloc( 1, buflen );
1206 if( buffer == NULL )
1207 {
1208 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1209 goto exit;
1210 }
1211 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1212 if( ret != 0 )
1213 goto exit;
1214 attributes->domain_parameters = buffer;
1215 attributes->domain_parameters_size = buflen;
1216
1217exit:
1218 mbedtls_mpi_free( &mpi );
1219 if( ret != 0 )
1220 mbedtls_free( buffer );
1221 return( mbedtls_to_psa_error( ret ) );
1222}
John Durkop07cc04a2020-11-16 22:08:34 -08001223#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001224 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001225
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001226/** Retrieve all the publicly-accessible attributes of a key.
1227 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001228psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001229 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001230{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001231 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001232 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001233 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001234
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001235 psa_reset_key_attributes( attributes );
1236
Ronald Cron5c522922020-11-14 16:35:34 +01001237 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001238 if( status != PSA_SUCCESS )
1239 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001240
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001241 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001242 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1243 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1244
1245#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1246 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001247 psa_set_key_slot_number( attributes,
1248 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001249#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001250
Gilles Peskine8e338702019-07-30 20:06:31 +02001251 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001252 {
John Durkop07cc04a2020-11-16 22:08:34 -08001253#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001254 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001255 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001256 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001257#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001258 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001259 * is not yet implemented.
1260 * https://github.com/ARMmbed/mbed-crypto/issues/216
1261 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001262 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001263 break;
1264#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001265 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001266 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001267
Ronald Cron90857082020-11-25 14:58:33 +01001268 status = mbedtls_psa_rsa_load_representation(
1269 slot->attr.type,
1270 slot->key.data,
1271 slot->key.bytes,
1272 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001273 if( status != PSA_SUCCESS )
1274 break;
1275
Steven Cooremana2371e52020-07-28 14:30:39 +02001276 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001277 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001278 mbedtls_rsa_free( rsa );
1279 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001280 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001281 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001282#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001283 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001284 default:
1285 /* Nothing else to do. */
1286 break;
1287 }
1288
1289 if( status != PSA_SUCCESS )
1290 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001291
Ronald Cron5c522922020-11-14 16:35:34 +01001292 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001293
Ronald Cron5c522922020-11-14 16:35:34 +01001294 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001295}
1296
Gilles Peskinec8000c02019-08-02 20:15:51 +02001297#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1298psa_status_t psa_get_key_slot_number(
1299 const psa_key_attributes_t *attributes,
1300 psa_key_slot_number_t *slot_number )
1301{
1302 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1303 {
1304 *slot_number = attributes->slot_number;
1305 return( PSA_SUCCESS );
1306 }
1307 else
1308 return( PSA_ERROR_INVALID_ARGUMENT );
1309}
1310#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1311
Ronald Crond18b5f82020-11-25 16:46:05 +01001312static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1313 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001314 uint8_t *data,
1315 size_t data_size,
1316 size_t *data_length )
1317{
Ronald Crond18b5f82020-11-25 16:46:05 +01001318 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001319 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001320 memcpy( data, key_buffer, key_buffer_size );
1321 memset( data + key_buffer_size, 0,
1322 data_size - key_buffer_size );
1323 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001324 return( PSA_SUCCESS );
1325}
1326
Ronald Cron7285cda2020-11-26 14:46:37 +01001327psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001328 const psa_key_attributes_t *attributes,
1329 const uint8_t *key_buffer, size_t key_buffer_size,
1330 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001331{
Ronald Crond18b5f82020-11-25 16:46:05 +01001332 psa_key_type_t type = attributes->core.type;
1333
Ronald Crond18b5f82020-11-25 16:46:05 +01001334 if( key_type_is_raw_bytes( type ) ||
1335 PSA_KEY_TYPE_IS_RSA( type ) ||
1336 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001337 {
1338 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001339 key_buffer, key_buffer_size,
1340 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001341 }
1342 else
1343 {
1344 /* This shouldn't happen in the reference implementation, but
1345 it is valid for a special-purpose implementation to omit
1346 support for exporting certain key types. */
1347 return( PSA_ERROR_NOT_SUPPORTED );
1348 }
1349}
1350
1351psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1352 uint8_t *data,
1353 size_t data_size,
1354 size_t *data_length )
1355{
1356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1357 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1358 psa_key_slot_t *slot;
1359
Ronald Crone907e552021-01-18 13:22:38 +01001360 /* Reject a zero-length output buffer now, since this can never be a
1361 * valid key representation. This way we know that data must be a valid
1362 * pointer and we can do things like memset(data, ..., data_size). */
1363 if( data_size == 0 )
1364 return( PSA_ERROR_BUFFER_TOO_SMALL );
1365
Ronald Cron9486f9d2020-11-26 13:36:23 +01001366 /* Set the key to empty now, so that even when there are errors, we always
1367 * set data_length to a value between 0 and data_size. On error, setting
1368 * the key to empty is a good choice because an empty key representation is
1369 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001370 *data_length = 0;
1371
Ronald Cron9486f9d2020-11-26 13:36:23 +01001372 /* Export requires the EXPORT flag. There is an exception for public keys,
1373 * which don't require any flag, but
1374 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1375 */
1376 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1377 PSA_KEY_USAGE_EXPORT, 0 );
1378 if( status != PSA_SUCCESS )
1379 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001380
Ronald Crond18b5f82020-11-25 16:46:05 +01001381 psa_key_attributes_t attributes = {
1382 .core = slot->attr
1383 };
Ronald Cron67227982020-11-26 15:16:05 +01001384 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001385 slot->key.data, slot->key.bytes,
1386 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001387
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 unlock_status = psa_unlock_key_slot( slot );
1389
1390 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1391}
1392
Ronald Cron7285cda2020-11-26 14:46:37 +01001393psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001394 const psa_key_attributes_t *attributes,
1395 const uint8_t *key_buffer,
1396 size_t key_buffer_size,
1397 uint8_t *data,
1398 size_t data_size,
1399 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001400{
Ronald Crond18b5f82020-11-25 16:46:05 +01001401 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001402
Ronald Crond18b5f82020-11-25 16:46:05 +01001403 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001404 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001405 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001406 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001407 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001409 key_buffer, key_buffer_size,
1410 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001411 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001412
Ronald Crond18b5f82020-11-25 16:46:05 +01001413 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001414 {
John Durkop0e005192020-10-31 22:06:54 -07001415#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1416 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001417 return( mbedtls_psa_rsa_export_public_key( attributes,
1418 key_buffer,
1419 key_buffer_size,
1420 data,
1421 data_size,
1422 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001423#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001424 /* We don't know how to convert a private RSA key to public. */
1425 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001426#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1427 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001428 }
1429 else
Moran Pekera998bc62018-04-16 18:16:20 +03001430 {
John Durkop6ba40d12020-11-10 08:50:04 -08001431#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1432 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001433 return( mbedtls_psa_ecp_export_public_key( attributes,
1434 key_buffer,
1435 key_buffer_size,
1436 data,
1437 data_size,
1438 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001439#else
1440 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001441 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001442#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1443 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001444 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001445 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001446 else
1447 {
1448 /* This shouldn't happen in the reference implementation, but
1449 it is valid for a special-purpose implementation to omit
1450 support for exporting certain key types. */
1451 return( PSA_ERROR_NOT_SUPPORTED );
1452 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001453}
Ronald Crond18b5f82020-11-25 16:46:05 +01001454
Ronald Croncf56a0a2020-08-04 09:51:30 +02001455psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001456 uint8_t *data,
1457 size_t data_size,
1458 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001459{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001461 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001462 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001463
Ronald Crone907e552021-01-18 13:22:38 +01001464 /* Reject a zero-length output buffer now, since this can never be a
1465 * valid key representation. This way we know that data must be a valid
1466 * pointer and we can do things like memset(data, ..., data_size). */
1467 if( data_size == 0 )
1468 return( PSA_ERROR_BUFFER_TOO_SMALL );
1469
Darryl Greendd8fb772018-11-07 16:00:44 +00001470 /* Set the key to empty now, so that even when there are errors, we always
1471 * set data_length to a value between 0 and data_size. On error, setting
1472 * the key to empty is a good choice because an empty key representation is
1473 * unlikely to be accepted anywhere. */
1474 *data_length = 0;
1475
1476 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001477 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001478 if( status != PSA_SUCCESS )
1479 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001480
Ronald Cron9486f9d2020-11-26 13:36:23 +01001481 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1482 {
1483 status = PSA_ERROR_INVALID_ARGUMENT;
1484 goto exit;
1485 }
1486
Ronald Crond18b5f82020-11-25 16:46:05 +01001487 psa_key_attributes_t attributes = {
1488 .core = slot->attr
1489 };
Ronald Cron67227982020-11-26 15:16:05 +01001490 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001491 &attributes, slot->key.data, slot->key.bytes,
1492 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001493
1494exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001495 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001496
Ronald Cron5c522922020-11-14 16:35:34 +01001497 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001498}
1499
Gilles Peskine91e8c332019-08-02 19:19:39 +02001500#if defined(static_assert)
1501static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1502 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001503static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001504 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001505static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001506 "One or more key attribute flag is listed as both internal-only and external-only" );
1507#endif
1508
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001509/** Validate that a key policy is internally well-formed.
1510 *
1511 * This function only rejects invalid policies. It does not validate the
1512 * consistency of the policy with respect to other attributes of the key
1513 * such as the key type.
1514 */
1515static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001516{
1517 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001518 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001519 PSA_KEY_USAGE_ENCRYPT |
1520 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001521 PSA_KEY_USAGE_SIGN_HASH |
1522 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001523 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1524 return( PSA_ERROR_INVALID_ARGUMENT );
1525
Gilles Peskine4747d192019-04-17 15:05:45 +02001526 return( PSA_SUCCESS );
1527}
1528
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001529/** Validate the internal consistency of key attributes.
1530 *
1531 * This function only rejects invalid attribute values. If does not
1532 * validate the consistency of the attributes with any key data that may
1533 * be involved in the creation of the key.
1534 *
1535 * Call this function early in the key creation process.
1536 *
1537 * \param[in] attributes Key attributes for the new key.
1538 * \param[out] p_drv On any return, the driver for the key, if any.
1539 * NULL for a transparent key.
1540 *
1541 */
1542static psa_status_t psa_validate_key_attributes(
1543 const psa_key_attributes_t *attributes,
1544 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001545{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001546 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001547 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001548 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001549
Ronald Cron54b90082020-10-29 15:26:43 +01001550 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001551 if( status != PSA_SUCCESS )
1552 return( status );
1553
Ronald Crond2ed4812020-07-17 16:11:30 +02001554 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001555 if( status != PSA_SUCCESS )
1556 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001557
Ronald Cron65f38a32020-10-23 17:11:13 +02001558 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1559 {
1560 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1561 return( PSA_ERROR_INVALID_ARGUMENT );
1562 }
1563 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001564 {
Ronald Cron77e412c2021-03-31 17:36:31 +02001565 if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
1566 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Crond2ed4812020-07-17 16:11:30 +02001567 }
1568
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001569 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001570 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001571 return( status );
1572
1573 /* Refuse to create overly large keys.
1574 * Note that this doesn't trigger on import if the attributes don't
1575 * explicitly specify a size (so psa_get_key_bits returns 0), so
1576 * psa_import_key() needs its own checks. */
1577 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1578 return( PSA_ERROR_NOT_SUPPORTED );
1579
Gilles Peskine91e8c332019-08-02 19:19:39 +02001580 /* Reject invalid flags. These should not be reachable through the API. */
1581 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1582 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1583 return( PSA_ERROR_INVALID_ARGUMENT );
1584
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001585 return( PSA_SUCCESS );
1586}
1587
Gilles Peskine4747d192019-04-17 15:05:45 +02001588/** Prepare a key slot to receive key material.
1589 *
1590 * This function allocates a key slot and sets its metadata.
1591 *
1592 * If this function fails, call psa_fail_key_creation().
1593 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001594 * This function is intended to be used as follows:
1595 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001596 * it with the specified attributes, and in case of a volatile key assign it
1597 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001598 * -# Populate the slot with the key material.
1599 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1600 * In case of failure at any step, stop the sequence and call
1601 * psa_fail_key_creation().
1602 *
Ronald Cron5c522922020-11-14 16:35:34 +01001603 * On success, the key slot is locked. It is the responsibility of the caller
1604 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001605 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001606 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001607 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001608 * \param[out] p_slot On success, a pointer to the prepared slot.
1609 * \param[out] p_drv On any return, the driver for the key, if any.
1610 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001611 *
1612 * \retval #PSA_SUCCESS
1613 * The key slot is ready to receive key material.
1614 * \return If this function fails, the key slot is an invalid state.
1615 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001616 */
1617static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001618 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001619 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001620 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001621 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001622{
1623 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001624 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001625 psa_key_slot_t *slot;
1626
Gilles Peskinedf179142019-07-15 22:02:14 +02001627 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001628 *p_drv = NULL;
1629
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001630 status = psa_validate_key_attributes( attributes, p_drv );
1631 if( status != PSA_SUCCESS )
1632 return( status );
1633
Ronald Cronc4d1b512020-07-31 11:26:37 +02001634 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 if( status != PSA_SUCCESS )
1636 return( status );
1637 slot = *p_slot;
1638
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001639 /* We're storing the declared bit-size of the key. It's up to each
1640 * creation mechanism to verify that this information is correct.
1641 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001642 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001643 * is optional (import, copy). In case of a volatile key, assign it the
1644 * volatile key identifier associated to the slot returned to contain its
1645 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001646
1647 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001648 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1649 {
1650#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1651 slot->attr.id = volatile_key_id;
1652#else
1653 slot->attr.id.key_id = volatile_key_id;
1654#endif
1655 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001656
Gilles Peskine91e8c332019-08-02 19:19:39 +02001657 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001658 * external-only flags, query `attributes`. Thanks to the check
1659 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001660 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001661 * may have set. */
1662 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001663
Gilles Peskinecbaff462019-07-12 23:46:04 +02001664#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001665 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001666 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001667 * create the key file in internal storage, create the
1668 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001669 * persistent data. This is done by starting a transaction that will
1670 * encompass these three actions.
1671 * For registering a volatile key, we just need to find an appropriate
1672 * slot number inside the SE. Since the key is designated volatile, creating
1673 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001674 /* The first thing to do is to find a slot number for the new key.
1675 * We save the slot number in persistent storage as part of the
1676 * transaction data. It will be needed to recover if the power
1677 * fails during the key creation process, to clean up on the secure
1678 * element side after restarting. Obtaining a slot number from the
1679 * secure element driver updates its persistent state, but we do not yet
1680 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001681 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001682 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001683 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001684 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001685 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001686 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001687 if( status != PSA_SUCCESS )
1688 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001689
1690 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001691 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001692 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1693 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001694 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001695 psa_crypto_transaction.key.id = slot->attr.id;
1696 status = psa_crypto_save_transaction( );
1697 if( status != PSA_SUCCESS )
1698 {
1699 (void) psa_crypto_stop_transaction( );
1700 return( status );
1701 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001702 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001703
1704 status = psa_copy_key_material_into_slot(
1705 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001706 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001707
1708 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1709 {
1710 /* Key registration only makes sense with a secure element. */
1711 return( PSA_ERROR_INVALID_ARGUMENT );
1712 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001713#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1714
Ronald Cronc4d1b512020-07-31 11:26:37 +02001715 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001716}
Gilles Peskine4747d192019-04-17 15:05:45 +02001717
1718/** Finalize the creation of a key once its key material has been set.
1719 *
1720 * This entails writing the key to persistent storage.
1721 *
1722 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001723 * See the documentation of psa_start_key_creation() for the intended use
1724 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001725 *
Ronald Cron5c522922020-11-14 16:35:34 +01001726 * If the finalization succeeds, the function unlocks the key slot (it was
1727 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1728 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001729 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001730 * \param[in,out] slot Pointer to the slot with key material.
1731 * \param[in] driver The secure element driver for the key,
1732 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001733 * \param[out] key On success, identifier of the key. Note that the
1734 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001735 *
1736 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001737 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001738 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1739 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1740 * \retval #PSA_ERROR_ALREADY_EXISTS
1741 * \retval #PSA_ERROR_DATA_INVALID
1742 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001743 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001744 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001745 * \return If this function fails, the key slot is an invalid state.
1746 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001747 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001748static psa_status_t psa_finish_key_creation(
1749 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001750 psa_se_drv_table_entry_t *driver,
1751 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001752{
1753 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001754 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001756
1757#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001758 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001760#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1761 if( driver != NULL )
1762 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001763 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001764 psa_key_slot_number_t slot_number =
1765 psa_key_slot_get_slot_number( slot ) ;
1766
Gilles Peskineb46bef22019-07-30 21:32:04 +02001767#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001768 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001769 sizeof( data.slot_number ),
1770 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001771#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001772 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001773 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001774 (uint8_t*) &data,
1775 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001776 }
1777 else
1778#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1779 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001780 /* Key material is saved in export representation in the slot, so
1781 * just pass the slot buffer for storage. */
1782 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001783 slot->key.data,
1784 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001785 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001786 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001787#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1788
Gilles Peskinecbaff462019-07-12 23:46:04 +02001789#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001790 /* Finish the transaction for a key creation. This does not
1791 * happen when registering an existing key. Detect this case
1792 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001793 * creation of a persistent key in a secure element requires a transaction,
1794 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001795 if( driver != NULL &&
1796 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001797 {
1798 status = psa_save_se_persistent_data( driver );
1799 if( status != PSA_SUCCESS )
1800 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001801 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001802 return( status );
1803 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001804 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001805 }
1806#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1807
Ronald Cron50972942020-11-14 11:28:25 +01001808 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001809 {
1810 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001811 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001812 if( status != PSA_SUCCESS )
1813 *key = MBEDTLS_SVC_KEY_ID_INIT;
1814 }
Ronald Cron50972942020-11-14 11:28:25 +01001815
Gilles Peskine4747d192019-04-17 15:05:45 +02001816 return( status );
1817}
1818
1819/** Abort the creation of a key.
1820 *
1821 * You may call this function after calling psa_start_key_creation(),
1822 * or after psa_finish_key_creation() fails. In other circumstances, this
1823 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001824 * See the documentation of psa_start_key_creation() for the intended use
1825 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001826 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001827 * \param[in,out] slot Pointer to the slot with key material.
1828 * \param[in] driver The secure element driver for the key,
1829 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001830 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001831static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001832 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001833{
Gilles Peskine011e4282019-06-26 18:34:38 +02001834 (void) driver;
1835
Gilles Peskine4747d192019-04-17 15:05:45 +02001836 if( slot == NULL )
1837 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001838
1839#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001840 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001841 * element, and the failure happened later (when saving metadata
1842 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001843 * element.
1844 * https://github.com/ARMmbed/mbed-crypto/issues/217
1845 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001846
Gilles Peskined7729582019-08-05 15:55:54 +02001847 /* Abort the ongoing transaction if any (there may not be one if
1848 * the creation process failed before starting one, or if the
1849 * key creation is a registration of a key in a secure element).
1850 * Earlier functions must already have done what it takes to undo any
1851 * partial creation. All that's left is to update the transaction data
1852 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001853 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001854#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1855
Gilles Peskine4747d192019-04-17 15:05:45 +02001856 psa_wipe_key_slot( slot );
1857}
1858
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001859/** Validate optional attributes during key creation.
1860 *
1861 * Some key attributes are optional during key creation. If they are
1862 * specified in the attributes structure, check that they are consistent
1863 * with the data in the slot.
1864 *
1865 * This function should be called near the end of key creation, after
1866 * the slot in memory is fully populated but before saving persistent data.
1867 */
1868static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001869 const psa_key_slot_t *slot,
1870 const psa_key_attributes_t *attributes )
1871{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001872 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001873 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001874 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001875 return( PSA_ERROR_INVALID_ARGUMENT );
1876 }
1877
1878 if( attributes->domain_parameters_size != 0 )
1879 {
John Durkop0e005192020-10-31 22:06:54 -07001880#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1881 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001882 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001883 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001884 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001885 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001887
Ronald Cron90857082020-11-25 14:58:33 +01001888 psa_status_t status = mbedtls_psa_rsa_load_representation(
1889 slot->attr.type,
1890 slot->key.data,
1891 slot->key.bytes,
1892 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001893 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001894 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001895
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896 mbedtls_mpi_init( &actual );
1897 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001898 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001900 mbedtls_rsa_free( rsa );
1901 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001902 if( ret != 0 )
1903 goto rsa_exit;
1904 ret = mbedtls_mpi_read_binary( &required,
1905 attributes->domain_parameters,
1906 attributes->domain_parameters_size );
1907 if( ret != 0 )
1908 goto rsa_exit;
1909 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1910 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1911 rsa_exit:
1912 mbedtls_mpi_free( &actual );
1913 mbedtls_mpi_free( &required );
1914 if( ret != 0)
1915 return( mbedtls_to_psa_error( ret ) );
1916 }
1917 else
John Durkop9814fa22020-11-04 12:28:15 -08001918#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1919 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001920 {
1921 return( PSA_ERROR_INVALID_ARGUMENT );
1922 }
1923 }
1924
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001925 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001926 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001927 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001928 return( PSA_ERROR_INVALID_ARGUMENT );
1929 }
1930
1931 return( PSA_SUCCESS );
1932}
1933
Gilles Peskine4747d192019-04-17 15:05:45 +02001934psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001935 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001936 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001937 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001938{
1939 psa_status_t status;
1940 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001941 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001942 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001943
Ronald Cron81709fc2020-11-14 12:10:32 +01001944 *key = MBEDTLS_SVC_KEY_ID_INIT;
1945
Gilles Peskine0f84d622019-09-12 19:03:13 +02001946 /* Reject zero-length symmetric keys (including raw data key objects).
1947 * This also rejects any key which might be encoded as an empty string,
1948 * which is never valid. */
1949 if( data_length == 0 )
1950 return( PSA_ERROR_INVALID_ARGUMENT );
1951
Gilles Peskinedf179142019-07-15 22:02:14 +02001952 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001953 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001954 if( status != PSA_SUCCESS )
1955 goto exit;
1956
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001957 /* In the case of a transparent key or an opaque key stored in local
1958 * storage (thus not in the case of generating a key in a secure element
1959 * or cryptoprocessor with storage), we have to allocate a buffer to
1960 * hold the generated key material. */
1961 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001962 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001963 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001964 if( status != PSA_SUCCESS )
1965 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001966 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001967
1968 bits = slot->attr.bits;
1969 status = psa_driver_wrapper_import_key( attributes,
1970 data, data_length,
1971 slot->key.data,
1972 slot->key.bytes,
1973 &slot->key.bytes, &bits );
1974 if( status != PSA_SUCCESS )
1975 goto exit;
1976
1977 if( slot->attr.bits == 0 )
1978 slot->attr.bits = (psa_key_bits_t) bits;
1979 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001980 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001981 status = PSA_ERROR_INVALID_ARGUMENT;
1982 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001983 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001984
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001985 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001986 if( status != PSA_SUCCESS )
1987 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988
Ronald Cron81709fc2020-11-14 12:10:32 +01001989 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001991 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001992 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001993
Gilles Peskine4747d192019-04-17 15:05:45 +02001994 return( status );
1995}
1996
Gilles Peskined7729582019-08-05 15:55:54 +02001997#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1998psa_status_t mbedtls_psa_register_se_key(
1999 const psa_key_attributes_t *attributes )
2000{
2001 psa_status_t status;
2002 psa_key_slot_t *slot = NULL;
2003 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002005
2006 /* Leaving attributes unspecified is not currently supported.
2007 * It could make sense to query the key type and size from the
2008 * secure element, but not all secure elements support this
2009 * and the driver HAL doesn't currently support it. */
2010 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2011 return( PSA_ERROR_NOT_SUPPORTED );
2012 if( psa_get_key_bits( attributes ) == 0 )
2013 return( PSA_ERROR_NOT_SUPPORTED );
2014
2015 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002016 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002017 if( status != PSA_SUCCESS )
2018 goto exit;
2019
Ronald Cron81709fc2020-11-14 12:10:32 +01002020 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002021
2022exit:
2023 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002024 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002025
Gilles Peskined7729582019-08-05 15:55:54 +02002026 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002027 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002028 return( status );
2029}
2030#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2031
Gilles Peskinef603c712019-01-19 13:40:11 +01002032static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002033 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002034{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002035 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002036 source->key.data,
2037 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002038 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002039 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002040
Steven Cooreman398aee52020-10-13 14:35:45 +02002041 target->attr.type = source->attr.type;
2042 target->attr.bits = source->attr.bits;
2043
2044 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002045}
2046
Ronald Croncf56a0a2020-08-04 09:51:30 +02002047psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002048 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002049 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002050{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002052 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002053 psa_key_slot_t *source_slot = NULL;
2054 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002055 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002056 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002057
Ronald Cron81709fc2020-11-14 12:10:32 +01002058 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2059
Ronald Cron5c522922020-11-14 16:35:34 +01002060 status = psa_get_and_lock_transparent_key_slot_with_policy(
2061 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002062 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002063 goto exit;
2064
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002065 status = psa_validate_optional_attributes( source_slot,
2066 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002067 if( status != PSA_SUCCESS )
2068 goto exit;
2069
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002070 status = psa_restrict_key_policy( source_slot->attr.type,
2071 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002072 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002073 if( status != PSA_SUCCESS )
2074 goto exit;
2075
Ronald Cron81709fc2020-11-14 12:10:32 +01002076 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2077 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078 if( status != PSA_SUCCESS )
2079 goto exit;
2080
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002081#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2082 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002083 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002084 /* Copying to a secure element is not implemented yet. */
2085 status = PSA_ERROR_NOT_SUPPORTED;
2086 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002087 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002088#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002089
Ronald Cron6cc66312021-04-02 12:27:47 +02002090 if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
2091 {
2092 /*
2093 * Copying through an opaque driver is not implemented yet, consider
2094 * a lifetime with an external location as an invalid parameter for
2095 * now.
2096 */
2097 status = PSA_ERROR_INVALID_ARGUMENT;
2098 goto exit;
2099 }
2100
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002101 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002102 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002103 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002104
Ronald Cron81709fc2020-11-14 12:10:32 +01002105 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002106exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002107 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002108 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002109
Ronald Cron5c522922020-11-14 16:35:34 +01002110 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002111
Ronald Cron5c522922020-11-14 16:35:34 +01002112 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002113}
2114
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002115
2116
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002117/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002118/* Message digests */
2119/****************************************************************/
2120
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2122{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002123 /* Aborting a non-active operation is allowed */
2124 if( operation->id == 0 )
2125 return( PSA_SUCCESS );
2126
2127 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2128 operation->id = 0;
2129
2130 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002131}
2132
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002133psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002134 psa_algorithm_t alg )
2135{
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002136 /* A context must be freshly initialized before it can be set up. */
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002137 if( operation->id != 0 )
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002138 return( PSA_ERROR_BAD_STATE );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002139
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002140 if( !PSA_ALG_IS_HASH( alg ) )
Steven Cooreman0e307642021-02-18 16:18:32 +01002141 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002142
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002143 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2144 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002145 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2146
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002147 return( psa_driver_wrapper_hash_setup( operation, alg ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002148}
2149
2150psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2151 const uint8_t *input,
2152 size_t input_length )
2153{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002154 if( operation->id == 0 )
2155 return( PSA_ERROR_BAD_STATE );
Gilles Peskine94e44542018-07-12 16:58:43 +02002156
2157 /* Don't require hash implementations to behave correctly on a
2158 * zero-length input, which may have an invalid pointer. */
2159 if( input_length == 0 )
2160 return( PSA_SUCCESS );
2161
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002162 psa_status_t status = psa_driver_wrapper_hash_update( operation,
2163 input, input_length );
2164 if( status != PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002165 psa_hash_abort( operation );
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002166
2167 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002168}
2169
2170psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2171 uint8_t *hash,
2172 size_t hash_size,
2173 size_t *hash_length )
2174{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002175 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002176 if( operation->id == 0 )
2177 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002178
Steven Cooreman1e582352021-02-18 17:24:37 +01002179 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002180 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002181 psa_hash_abort( operation );
2182 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002183}
2184
Gilles Peskine2d277862018-06-18 15:41:12 +02002185psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2186 const uint8_t *hash,
2187 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002188{
2189 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2190 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002191 psa_status_t status = psa_hash_finish(
2192 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002193 actual_hash, sizeof( actual_hash ),
2194 &actual_hash_length );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195 if( status != PSA_SUCCESS )
2196 return( status );
2197 if( actual_hash_length != hash_length )
2198 return( PSA_ERROR_INVALID_SIGNATURE );
2199 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2200 return( PSA_ERROR_INVALID_SIGNATURE );
2201 return( PSA_SUCCESS );
2202}
2203
Gilles Peskine0a749c82019-11-28 19:33:58 +01002204psa_status_t psa_hash_compute( psa_algorithm_t alg,
2205 const uint8_t *input, size_t input_length,
2206 uint8_t *hash, size_t hash_size,
2207 size_t *hash_length )
2208{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002209 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002210 if( !PSA_ALG_IS_HASH( alg ) )
2211 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002212
Steven Cooreman1e582352021-02-18 17:24:37 +01002213 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2214 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002215}
2216
2217psa_status_t psa_hash_compare( psa_algorithm_t alg,
2218 const uint8_t *input, size_t input_length,
2219 const uint8_t *hash, size_t hash_length )
2220{
Steven Cooreman84d670d2021-02-18 16:22:53 +01002221 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2222 size_t actual_hash_length;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002223
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002224 if( !PSA_ALG_IS_HASH( alg ) )
2225 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002226
Steven Cooreman1e582352021-02-18 17:24:37 +01002227 psa_status_t status = psa_driver_wrapper_hash_compute(
2228 alg, input, input_length,
2229 actual_hash, sizeof(actual_hash),
2230 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002231 if( status != PSA_SUCCESS )
Steven Cooreman84d670d2021-02-18 16:22:53 +01002232 return( status );
2233 if( actual_hash_length != hash_length )
2234 return( PSA_ERROR_INVALID_SIGNATURE );
2235 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2236 return( PSA_ERROR_INVALID_SIGNATURE );
2237 return( PSA_SUCCESS );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002238}
2239
Gilles Peskineeb35d782019-01-22 17:56:16 +01002240psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2241 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002242{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002243 if( source_operation->id == 0 ||
2244 target_operation->id != 0 )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002245 {
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002246 return( PSA_ERROR_BAD_STATE );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002247 }
2248
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002249 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2250 target_operation );
2251 if( status != PSA_SUCCESS )
2252 psa_hash_abort( target_operation );
2253
2254 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002255}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256
2257
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002259/* MAC */
2260/****************************************************************/
2261
John Durkop6ba40d12020-11-10 08:50:04 -08002262#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002263static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002264{
Gilles Peskine2d277862018-06-18 15:41:12 +02002265 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002266 {
2267 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002268 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002269 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002270 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002271 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002272 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002273 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002274 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002275 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002276 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002277 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002278 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002279 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002280 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002281 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002282 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002283 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002284 return( 128 );
2285 default:
2286 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002287 }
2288}
John Durkop07cc04a2020-11-16 22:08:34 -08002289#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002290
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002291/* Initialize the MAC operation structure. Once this function has been
2292 * called, psa_mac_abort can run and will do the right thing. */
2293static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2294 psa_algorithm_t alg )
2295{
2296 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2297
Steven Cooreman15472f82021-03-02 16:16:22 +01002298 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002299 operation->key_set = 0;
2300 operation->iv_set = 0;
2301 operation->iv_required = 0;
2302 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002303 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002304
Ronald Cron3d471812021-03-18 13:40:31 +01002305#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01002306 if( operation->alg == PSA_ALG_CMAC )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002307 {
2308 operation->iv_required = 0;
2309 mbedtls_cipher_init( &operation->ctx.cmac );
2310 status = PSA_SUCCESS;
2311 }
2312 else
Ronald Cron3d471812021-03-18 13:40:31 +01002313#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
John Durkopd0321952020-10-29 21:37:36 -07002314#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002315 if( PSA_ALG_IS_HMAC( operation->alg ) )
2316 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002317 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
Steven Cooreman0e307642021-02-18 16:18:32 +01002318 operation->ctx.hmac.alg = 0;
Gilles Peskineff94abd2018-07-12 17:07:52 +02002319 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002320 }
2321 else
John Durkopd0321952020-10-29 21:37:36 -07002322#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002323 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002324 if( ! PSA_ALG_IS_MAC( alg ) )
2325 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002326 }
2327
2328 if( status != PSA_SUCCESS )
2329 memset( operation, 0, sizeof( *operation ) );
2330 return( status );
2331}
2332
John Durkop6ba40d12020-11-10 08:50:04 -08002333#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002334static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2335{
Gilles Peskine3f108122018-12-07 18:14:53 +01002336 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002337 return( psa_hash_abort( &hmac->hash_ctx ) );
2338}
John Durkop6ba40d12020-11-10 08:50:04 -08002339#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002340
Gilles Peskine8c9def32018-02-08 10:02:12 +01002341psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2342{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002343 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002344 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002345 /* The object has (apparently) been initialized but it is not
2346 * in use. It's ok to call abort on such an object, and there's
2347 * nothing to do. */
2348 return( PSA_SUCCESS );
2349 }
2350 else
Ronald Cron3d471812021-03-18 13:40:31 +01002351#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002352 if( operation->alg == PSA_ALG_CMAC )
2353 {
2354 mbedtls_cipher_free( &operation->ctx.cmac );
2355 }
2356 else
Ronald Cron3d471812021-03-18 13:40:31 +01002357#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
John Durkopd0321952020-10-29 21:37:36 -07002358#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002359 if( PSA_ALG_IS_HMAC( operation->alg ) )
2360 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002361 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002362 }
2363 else
John Durkopd0321952020-10-29 21:37:36 -07002364#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002365 {
2366 /* Sanity check (shouldn't happen: operation->alg should
2367 * always have been initialized to a valid value). */
2368 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002369 }
Moran Peker41deec42018-04-04 15:43:05 +03002370
Gilles Peskine8c9def32018-02-08 10:02:12 +01002371 operation->alg = 0;
2372 operation->key_set = 0;
2373 operation->iv_set = 0;
2374 operation->iv_required = 0;
2375 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002376 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002377
Gilles Peskine8c9def32018-02-08 10:02:12 +01002378 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002379
2380bad_state:
2381 /* If abort is called on an uninitialized object, we can't trust
2382 * anything. Wipe the object in case it contains confidential data.
2383 * This may result in a memory leak if a pointer gets overwritten,
2384 * but it's too late to do anything about this. */
2385 memset( operation, 0, sizeof( *operation ) );
2386 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387}
2388
Ronald Cron3d471812021-03-18 13:40:31 +01002389#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01002390static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation,
2391 psa_key_slot_t *slot )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002392{
Janos Follath24eed8d2019-11-22 13:21:35 +00002393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002394 const mbedtls_cipher_info_t *cipher_info =
2395 mbedtls_cipher_info_from_psa( PSA_ALG_CMAC,
2396 slot->attr.type, slot->attr.bits,
2397 NULL );
2398 if( cipher_info == NULL )
2399 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002400
2401 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2402 if( ret != 0 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002403 goto exit;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002404
2405 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002406 slot->key.data,
Steven Cooreman15472f82021-03-02 16:16:22 +01002407 slot->attr.bits );
2408exit:
2409 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002410}
Ronald Cron3d471812021-03-18 13:40:31 +01002411#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002412
John Durkop6ba40d12020-11-10 08:50:04 -08002413#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002414static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2415 const uint8_t *key,
2416 size_t key_length,
2417 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002418{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002419 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002420 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002421 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002422 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002423 psa_status_t status;
2424
Steven Cooreman0e307642021-02-18 16:18:32 +01002425 hmac->alg = hash_alg;
2426
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002427 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2428 * overflow below. This should never trigger if the hash algorithm
2429 * is implemented correctly. */
2430 /* The size checks against the ipad and opad buffers cannot be written
2431 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2432 * because that triggers -Wlogical-op on GCC 7.3. */
2433 if( block_size > sizeof( ipad ) )
2434 return( PSA_ERROR_NOT_SUPPORTED );
2435 if( block_size > sizeof( hmac->opad ) )
2436 return( PSA_ERROR_NOT_SUPPORTED );
2437 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002438 return( PSA_ERROR_NOT_SUPPORTED );
2439
Gilles Peskined223b522018-06-11 18:12:58 +02002440 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002441 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002442 status = psa_hash_compute( hash_alg, key, key_length,
2443 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002444 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002445 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002446 }
Gilles Peskine96889972018-07-12 17:07:03 +02002447 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2448 * but it is permitted. It is common when HMAC is used in HKDF, for
2449 * example. Don't call `memcpy` in the 0-length because `key` could be
2450 * an invalid pointer which would make the behavior undefined. */
2451 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002452 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002453
Gilles Peskined223b522018-06-11 18:12:58 +02002454 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2455 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002456 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002457 ipad[i] ^= 0x36;
2458 memset( ipad + key_length, 0x36, block_size - key_length );
2459
2460 /* Copy the key material from ipad to opad, flipping the requisite bits,
2461 * and filling the rest of opad with the requisite constant. */
2462 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002463 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2464 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002465
Gilles Peskine01126fa2018-07-12 17:04:55 +02002466 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002467 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002468 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002469
Gilles Peskine01126fa2018-07-12 17:04:55 +02002470 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002471
2472cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002473 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002474
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002475 return( status );
2476}
John Durkop6ba40d12020-11-10 08:50:04 -08002477#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002478
Gilles Peskine89167cb2018-07-08 20:12:23 +02002479static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002480 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002481 psa_algorithm_t alg,
2482 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002484 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002485 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002486 psa_key_slot_t *slot;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002487 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002488 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002490 /* A context must be freshly initialized before it can be set up. */
2491 if( operation->alg != 0 )
2492 {
2493 return( PSA_ERROR_BAD_STATE );
2494 }
2495
Steven Cooreman15472f82021-03-02 16:16:22 +01002496 status = psa_mac_init( operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002497 if( status != PSA_SUCCESS )
2498 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002499 if( is_sign )
2500 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501
Ronald Cron5c522922020-11-14 16:35:34 +01002502 status = psa_get_and_lock_transparent_key_slot_with_policy(
2503 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002504 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002505 goto exit;
Steven Cooreman15472f82021-03-02 16:16:22 +01002506
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002507 /* Validate the combination of key type and algorithm */
2508 status = psa_mac_key_can_do( alg, slot->attr.type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002509 if( status != PSA_SUCCESS )
2510 goto exit;
2511
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002512 /* Get the output length for the algorithm and key combination. None of the
2513 * currently supported algorithms have an output length dependent on actual
2514 * key size, so setting it to a bogus value is currently OK. */
2515 operation->mac_size = PSA_MAC_LENGTH( slot->attr.type, 0, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002516
2517 if( operation->mac_size < 4 )
2518 {
2519 /* A very short MAC is too short for security since it can be
2520 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2521 * so we make this our minimum, even though 32 bits is still
2522 * too small for security. */
2523 status = PSA_ERROR_NOT_SUPPORTED;
2524 goto exit;
2525 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002526
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002527 if( operation->mac_size >
2528 PSA_MAC_LENGTH( slot->attr.type, 0, PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
2529 {
2530 /* It's impossible to "truncate" to a larger length than the full length
2531 * of the algorithm. */
2532 status = PSA_ERROR_INVALID_ARGUMENT;
2533 goto exit;
2534 }
2535
Ronald Cron3d471812021-03-18 13:40:31 +01002536#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01002537 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002538 {
Steven Cooreman15472f82021-03-02 16:16:22 +01002539 status = psa_cmac_setup( operation, slot );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002540 }
2541 else
Ronald Cron3d471812021-03-18 13:40:31 +01002542#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
John Durkopd0321952020-10-29 21:37:36 -07002543#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01002544 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002545 {
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002546 /* Sanity check. This shouldn't fail on a valid configuration. */
Steven Cooreman1fb691a2021-03-08 12:01:55 +01002547 if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002548 {
2549 status = PSA_ERROR_NOT_SUPPORTED;
2550 goto exit;
2551 }
2552
Gilles Peskine8e338702019-07-30 20:06:31 +02002553 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002554 {
2555 status = PSA_ERROR_INVALID_ARGUMENT;
2556 goto exit;
2557 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002558
Gilles Peskine01126fa2018-07-12 17:04:55 +02002559 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002560 slot->key.data,
2561 slot->key.bytes,
Steven Cooreman15472f82021-03-02 16:16:22 +01002562 PSA_ALG_HMAC_GET_HASH( alg ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002563 }
2564 else
John Durkopd0321952020-10-29 21:37:36 -07002565#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002566 {
2567 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002569
Gilles Peskinefbfac682018-07-08 20:51:54 +02002570exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002571 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002573 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002574 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002575 else
2576 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002577 operation->key_set = 1;
2578 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002579
Ronald Cron5c522922020-11-14 16:35:34 +01002580 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002581
Ronald Cron5c522922020-11-14 16:35:34 +01002582 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583}
2584
Gilles Peskine89167cb2018-07-08 20:12:23 +02002585psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002586 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002587 psa_algorithm_t alg )
2588{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002589 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002590}
2591
2592psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002593 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002594 psa_algorithm_t alg )
2595{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002596 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002597}
2598
Gilles Peskine8c9def32018-02-08 10:02:12 +01002599psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2600 const uint8_t *input,
2601 size_t input_length )
2602{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002603 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002604 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002605 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002607 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608 operation->has_input = 1;
2609
Ronald Cron3d471812021-03-18 13:40:31 +01002610#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002611 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002612 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002613 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2614 input, input_length );
2615 status = mbedtls_to_psa_error( ret );
2616 }
2617 else
Ronald Cron3d471812021-03-18 13:40:31 +01002618#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
John Durkopd0321952020-10-29 21:37:36 -07002619#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002620 if( PSA_ALG_IS_HMAC( operation->alg ) )
2621 {
2622 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2623 input_length );
2624 }
2625 else
John Durkopd0321952020-10-29 21:37:36 -07002626#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002627 {
2628 /* This shouldn't happen if `operation` was initialized by
2629 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002630 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002631 }
2632
Gilles Peskinefbfac682018-07-08 20:51:54 +02002633 if( status != PSA_SUCCESS )
2634 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002635 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002636}
2637
John Durkop6ba40d12020-11-10 08:50:04 -08002638#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002639static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2640 uint8_t *mac,
2641 size_t mac_size )
2642{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002643 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Steven Cooreman0e307642021-02-18 16:18:32 +01002644 psa_algorithm_t hash_alg = hmac->alg;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002645 size_t hash_size = 0;
2646 size_t block_size = psa_get_hash_block_size( hash_alg );
2647 psa_status_t status;
2648
Gilles Peskine01126fa2018-07-12 17:04:55 +02002649 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2650 if( status != PSA_SUCCESS )
2651 return( status );
2652 /* From here on, tmp needs to be wiped. */
2653
2654 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2655 if( status != PSA_SUCCESS )
2656 goto exit;
2657
2658 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2659 if( status != PSA_SUCCESS )
2660 goto exit;
2661
2662 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2663 if( status != PSA_SUCCESS )
2664 goto exit;
2665
Gilles Peskined911eb72018-08-14 15:18:45 +02002666 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2667 if( status != PSA_SUCCESS )
2668 goto exit;
2669
2670 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002671
2672exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002673 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002674 return( status );
2675}
John Durkop6ba40d12020-11-10 08:50:04 -08002676#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002677
mohammad16036df908f2018-04-02 08:34:15 -07002678static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002679 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002680 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002682 if( ! operation->key_set )
2683 return( PSA_ERROR_BAD_STATE );
2684 if( operation->iv_required && ! operation->iv_set )
2685 return( PSA_ERROR_BAD_STATE );
2686
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687 if( mac_size < operation->mac_size )
2688 return( PSA_ERROR_BUFFER_TOO_SMALL );
2689
Ronald Cron3d471812021-03-18 13:40:31 +01002690#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002691 if( operation->alg == PSA_ALG_CMAC )
2692 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002693 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02002694 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2695 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002696 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002697 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002698 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002700 else
Ronald Cron3d471812021-03-18 13:40:31 +01002701#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
John Durkopd0321952020-10-29 21:37:36 -07002702#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002703 if( PSA_ALG_IS_HMAC( operation->alg ) )
2704 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002705 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002706 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002707 }
2708 else
John Durkopd0321952020-10-29 21:37:36 -07002709#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002710 {
2711 /* This shouldn't happen if `operation` was initialized by
2712 * a setup function. */
2713 return( PSA_ERROR_BAD_STATE );
2714 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715}
2716
Gilles Peskineacd4be32018-07-08 19:56:25 +02002717psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2718 uint8_t *mac,
2719 size_t mac_size,
2720 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002721{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002722 psa_status_t status;
2723
Jaeden Amero252ef282019-02-15 14:05:35 +00002724 if( operation->alg == 0 )
2725 {
2726 return( PSA_ERROR_BAD_STATE );
2727 }
2728
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002729 /* Fill the output buffer with something that isn't a valid mac
2730 * (barring an attack on the mac and deliberately-crafted input),
2731 * in case the caller doesn't check the return status properly. */
2732 *mac_length = mac_size;
2733 /* If mac_size is 0 then mac may be NULL and then the
2734 * call to memset would have undefined behavior. */
2735 if( mac_size != 0 )
2736 memset( mac, '!', mac_size );
2737
Gilles Peskine89167cb2018-07-08 20:12:23 +02002738 if( ! operation->is_sign )
2739 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002740 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002741 }
mohammad16036df908f2018-04-02 08:34:15 -07002742
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002743 status = psa_mac_finish_internal( operation, mac, mac_size );
2744
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002745 if( status == PSA_SUCCESS )
2746 {
2747 status = psa_mac_abort( operation );
2748 if( status == PSA_SUCCESS )
2749 *mac_length = operation->mac_size;
2750 else
2751 memset( mac, '!', mac_size );
2752 }
2753 else
2754 psa_mac_abort( operation );
2755 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002756}
2757
Gilles Peskineacd4be32018-07-08 19:56:25 +02002758psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2759 const uint8_t *mac,
2760 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002761{
Gilles Peskine828ed142018-06-18 23:25:51 +02002762 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002763 psa_status_t status;
2764
Jaeden Amero252ef282019-02-15 14:05:35 +00002765 if( operation->alg == 0 )
2766 {
2767 return( PSA_ERROR_BAD_STATE );
2768 }
2769
Gilles Peskine89167cb2018-07-08 20:12:23 +02002770 if( operation->is_sign )
2771 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002772 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002773 }
2774 if( operation->mac_size != mac_length )
2775 {
2776 status = PSA_ERROR_INVALID_SIGNATURE;
2777 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002778 }
mohammad16036df908f2018-04-02 08:34:15 -07002779
2780 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002781 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01002782 if( status != PSA_SUCCESS )
2783 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002784
2785 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2786 status = PSA_ERROR_INVALID_SIGNATURE;
2787
2788cleanup:
2789 if( status == PSA_SUCCESS )
2790 status = psa_mac_abort( operation );
2791 else
2792 psa_mac_abort( operation );
2793
Gilles Peskine3f108122018-12-07 18:14:53 +01002794 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002795
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002796 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797}
2798
2799
Gilles Peskine20035e32018-02-03 22:44:14 +01002800
Gilles Peskine20035e32018-02-03 22:44:14 +01002801/****************************************************************/
2802/* Asymmetric cryptography */
2803/****************************************************************/
2804
Ronald Cron67b1eb32020-12-08 17:37:27 +01002805psa_status_t psa_sign_hash_internal(
Ronald Cron18659932020-12-08 16:32:23 +01002806 const psa_key_attributes_t *attributes,
2807 const uint8_t *key_buffer, size_t key_buffer_size,
2808 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2809 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002810{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002811 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002812 {
Ronald Cron4501c982021-03-19 20:09:32 +01002813 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2814 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002815 {
Gilles Peskine20035e32018-02-03 22:44:14 +01002816#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2817 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron4501c982021-03-19 20:09:32 +01002818 return( mbedtls_psa_rsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002819 attributes,
2820 key_buffer, key_buffer_size,
2821 alg, hash, hash_length,
2822 signature, signature_size, signature_length ) );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002823#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002824 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002825 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002826 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002827 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002828 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002829 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002830 }
2831 else
Ronald Cron4501c982021-03-19 20:09:32 +01002832 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002833 {
Ronald Cron4501c982021-03-19 20:09:32 +01002834 if( PSA_ALG_IS_ECDSA( alg ) )
2835 {
2836#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2837 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2838 return( mbedtls_psa_ecdsa_sign_hash(
2839 attributes,
2840 key_buffer, key_buffer_size,
2841 alg, hash, hash_length,
2842 signature, signature_size, signature_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002843#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2844 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron4501c982021-03-19 20:09:32 +01002845 }
2846 else
2847 {
2848 return( PSA_ERROR_INVALID_ARGUMENT );
2849 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002850 }
Ronald Cron4501c982021-03-19 20:09:32 +01002851
2852 (void)key_buffer;
2853 (void)key_buffer_size;
2854 (void)hash;
2855 (void)hash_length;
2856 (void)signature;
2857 (void)signature_size;
2858 (void)signature_length;
2859
2860 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002861}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002862
2863psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
2864 psa_algorithm_t alg,
2865 const uint8_t *hash,
2866 size_t hash_length,
2867 uint8_t *signature,
2868 size_t signature_size,
2869 size_t *signature_length )
2870{
2871 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2872 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2873 psa_key_slot_t *slot;
2874
2875 *signature_length = signature_size;
2876 /* Immediately reject a zero-length signature buffer. This guarantees
2877 * that signature must be a valid pointer. (On the other hand, the hash
2878 * buffer can in principle be empty since it doesn't actually have
2879 * to be a hash.) */
2880 if( signature_size == 0 )
2881 return( PSA_ERROR_BUFFER_TOO_SMALL );
2882
2883 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
2884 PSA_KEY_USAGE_SIGN_HASH,
2885 alg );
2886 if( status != PSA_SUCCESS )
2887 goto exit;
2888 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
2889 {
2890 status = PSA_ERROR_INVALID_ARGUMENT;
2891 goto exit;
2892 }
2893
Ronald Cron9f17aa42020-12-08 17:07:25 +01002894 psa_key_attributes_t attributes = {
2895 .core = slot->attr
2896 };
Gilles Peskinee59236f2018-01-27 23:32:46 +01002897
Ronald Cron9f17aa42020-12-08 17:07:25 +01002898 status = psa_driver_wrapper_sign_hash(
2899 &attributes, slot->key.data, slot->key.bytes,
2900 alg, hash, hash_length,
2901 signature, signature_size, signature_length );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002902
2903exit:
2904 /* Fill the unused part of the output buffer (the whole buffer on error,
2905 * the trailing part on success) with something that isn't a valid mac
2906 * (barring an attack on the mac and deliberately-crafted input),
2907 * in case the caller doesn't check the return status properly. */
2908 if( status == PSA_SUCCESS )
2909 memset( signature + *signature_length, '!',
2910 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02002911 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002912 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002913 /* If signature_size is 0 then we have nothing to do. We must not call
2914 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02002915
Ronald Cron5c522922020-11-14 16:35:34 +01002916 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002917
Ronald Cron5c522922020-11-14 16:35:34 +01002918 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03002919}
2920
Ronald Cron67b1eb32020-12-08 17:37:27 +01002921psa_status_t psa_verify_hash_internal(
Ronald Cron18659932020-12-08 16:32:23 +01002922 const psa_key_attributes_t *attributes,
2923 const uint8_t *key_buffer, size_t key_buffer_size,
2924 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2925 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002926{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002927 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002928 {
Ronald Cron4501c982021-03-19 20:09:32 +01002929 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2930 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002931 {
Ronald Cron4501c982021-03-19 20:09:32 +01002932#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2933 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2934 return( mbedtls_psa_rsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002935 attributes,
2936 key_buffer, key_buffer_size,
2937 alg, hash, hash_length,
2938 signature, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002939#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2940 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002941 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002942 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002943 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002944 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002945 }
itayzafrir5c753392018-05-08 11:18:38 +03002946 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002947 else
Ronald Cron4501c982021-03-19 20:09:32 +01002948 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002949 {
Ronald Cron4501c982021-03-19 20:09:32 +01002950 if( PSA_ALG_IS_ECDSA( alg ) )
2951 {
2952#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2953 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2954 return( mbedtls_psa_ecdsa_verify_hash(
2955 attributes,
2956 key_buffer, key_buffer_size,
2957 alg, hash, hash_length,
2958 signature, signature_length ) );
2959#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2960 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
2961 }
2962 else
2963 {
2964 return( PSA_ERROR_INVALID_ARGUMENT );
2965 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002966 }
Ronald Cron4501c982021-03-19 20:09:32 +01002967
2968 (void)key_buffer;
2969 (void)key_buffer_size;
2970 (void)hash;
2971 (void)hash_length;
2972 (void)signature;
2973 (void)signature_length;
2974
2975 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002976}
2977
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002978psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
2979 psa_algorithm_t alg,
2980 const uint8_t *hash,
2981 size_t hash_length,
2982 const uint8_t *signature,
2983 size_t signature_length )
2984{
2985 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2986 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002987 psa_key_slot_t *slot;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988
2989 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002990 PSA_KEY_USAGE_VERIFY_HASH,
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002991 alg );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002992 if( status != PSA_SUCCESS )
2993 return( status );
2994
Ronald Cron9f17aa42020-12-08 17:07:25 +01002995 psa_key_attributes_t attributes = {
2996 .core = slot->attr
2997 };
Gilles Peskinee59236f2018-01-27 23:32:46 +01002998
Ronald Cron9f17aa42020-12-08 17:07:25 +01002999 status = psa_driver_wrapper_verify_hash(
3000 &attributes, slot->key.data, slot->key.bytes,
3001 alg, hash, hash_length,
3002 signature, signature_length );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003003
Ronald Cron5c522922020-11-14 16:35:34 +01003004 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003005
Ronald Cron5c522922020-11-14 16:35:34 +01003006 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003007}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003008
John Durkop0e005192020-10-31 22:06:54 -07003009#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003010static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3011 mbedtls_rsa_context *rsa )
3012{
3013 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3014 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3015 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3016 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3017}
John Durkop0e005192020-10-31 22:06:54 -07003018#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003019
Ronald Croncf56a0a2020-08-04 09:51:30 +02003020psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003021 psa_algorithm_t alg,
3022 const uint8_t *input,
3023 size_t input_length,
3024 const uint8_t *salt,
3025 size_t salt_length,
3026 uint8_t *output,
3027 size_t output_size,
3028 size_t *output_length )
3029{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003030 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003031 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003032 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003033
Darryl Green5cc689a2018-07-24 15:34:10 +01003034 (void) input;
3035 (void) input_length;
3036 (void) salt;
3037 (void) output;
3038 (void) output_size;
3039
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003040 *output_length = 0;
3041
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003042 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3043 return( PSA_ERROR_INVALID_ARGUMENT );
3044
Ronald Cron5c522922020-11-14 16:35:34 +01003045 status = psa_get_and_lock_transparent_key_slot_with_policy(
3046 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003047 if( status != PSA_SUCCESS )
3048 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003049 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3050 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003051 {
3052 status = PSA_ERROR_INVALID_ARGUMENT;
3053 goto exit;
3054 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003055
John Durkop6ba40d12020-11-10 08:50:04 -08003056#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3057 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003058 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003059 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003060 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003061 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3062 slot->key.data,
3063 slot->key.bytes,
3064 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003065 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003066 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003067
3068 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003069 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003070 status = PSA_ERROR_BUFFER_TOO_SMALL;
3071 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003072 }
John Durkop0e005192020-10-31 22:06:54 -07003073#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003074 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
3075 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003076 status = mbedtls_to_psa_error(
3077 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003078 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003079 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003080 MBEDTLS_RSA_PUBLIC,
3081 input_length,
3082 input,
3083 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003084 }
3085 else
John Durkop0e005192020-10-31 22:06:54 -07003086#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3087#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003088 if( PSA_ALG_IS_RSA_OAEP( alg ) )
3089 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003090 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003091 status = mbedtls_to_psa_error(
3092 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003093 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003094 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003095 MBEDTLS_RSA_PUBLIC,
3096 salt, salt_length,
3097 input_length,
3098 input,
3099 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003100 }
3101 else
John Durkop0e005192020-10-31 22:06:54 -07003102#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003103 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003104 status = PSA_ERROR_INVALID_ARGUMENT;
3105 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003106 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003107rsa_exit:
3108 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003109 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003110
Steven Cooremana2371e52020-07-28 14:30:39 +02003111 mbedtls_rsa_free( rsa );
3112 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113 }
3114 else
John Durkop9814fa22020-11-04 12:28:15 -08003115#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003116 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003118 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003119 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003120
3121exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003122 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003123
Ronald Cron5c522922020-11-14 16:35:34 +01003124 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003125}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126
Ronald Croncf56a0a2020-08-04 09:51:30 +02003127psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003128 psa_algorithm_t alg,
3129 const uint8_t *input,
3130 size_t input_length,
3131 const uint8_t *salt,
3132 size_t salt_length,
3133 uint8_t *output,
3134 size_t output_size,
3135 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003138 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003139 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003140
Darryl Green5cc689a2018-07-24 15:34:10 +01003141 (void) input;
3142 (void) input_length;
3143 (void) salt;
3144 (void) output;
3145 (void) output_size;
3146
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003147 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003148
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003149 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3150 return( PSA_ERROR_INVALID_ARGUMENT );
3151
Ronald Cron5c522922020-11-14 16:35:34 +01003152 status = psa_get_and_lock_transparent_key_slot_with_policy(
3153 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003154 if( status != PSA_SUCCESS )
3155 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003156 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003157 {
3158 status = PSA_ERROR_INVALID_ARGUMENT;
3159 goto exit;
3160 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161
John Durkop6ba40d12020-11-10 08:50:04 -08003162#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3163 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003164 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003165 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003166 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003167 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3168 slot->key.data,
3169 slot->key.bytes,
3170 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003171 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003172 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173
Steven Cooremana2371e52020-07-28 14:30:39 +02003174 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003175 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003176 status = PSA_ERROR_INVALID_ARGUMENT;
3177 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003178 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179
John Durkop0e005192020-10-31 22:06:54 -07003180#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003181 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003183 status = mbedtls_to_psa_error(
3184 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003185 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003186 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003187 MBEDTLS_RSA_PRIVATE,
3188 output_length,
3189 input,
3190 output,
3191 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192 }
3193 else
John Durkop0e005192020-10-31 22:06:54 -07003194#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3195#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003196 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003198 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003199 status = mbedtls_to_psa_error(
3200 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003201 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003202 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003203 MBEDTLS_RSA_PRIVATE,
3204 salt, salt_length,
3205 output_length,
3206 input,
3207 output,
3208 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003209 }
3210 else
John Durkop0e005192020-10-31 22:06:54 -07003211#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003213 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003214 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003215
Steven Cooreman4fed4552020-08-03 14:46:03 +02003216rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003217 mbedtls_rsa_free( rsa );
3218 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003220 else
John Durkop6ba40d12020-11-10 08:50:04 -08003221#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3222 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003223 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003224 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003225 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003226
3227exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003228 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003229
Ronald Cron5c522922020-11-14 16:35:34 +01003230 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003231}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003232
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003233
3234
mohammad1603503973b2018-03-12 15:59:30 +02003235/****************************************************************/
3236/* Symmetric cryptography */
3237/****************************************************************/
3238
Gilles Peskinee553c652018-06-04 16:22:46 +02003239static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003240 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003241 psa_algorithm_t alg,
3242 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003243{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003244 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003245 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003246 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003247 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3248 PSA_KEY_USAGE_ENCRYPT :
3249 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003250
Steven Cooremand3feccd2020-09-01 15:56:14 +02003251 /* A context must be freshly initialized before it can be set up. */
Ronald Cron49fafa92021-03-10 08:34:23 +01003252 if( operation->id != 0 )
Steven Cooremand3feccd2020-09-01 15:56:14 +02003253 return( PSA_ERROR_BAD_STATE );
3254
Steven Cooremana07b9972020-09-10 14:54:14 +02003255 /* The requested algorithm must be one that can be processed by cipher. */
3256 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003257 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003258
Steven Cooremanef8575e2020-09-11 11:44:50 +02003259 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003260 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003261 if( status != PSA_SUCCESS )
3262 goto exit;
3263
Ronald Cron49fafa92021-03-10 08:34:23 +01003264 /* Initialize the operation struct members, except for id. The id member
Steven Cooremanef8575e2020-09-11 11:44:50 +02003265 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003266 * so we only set it (in the driver wrapper) after resources have been
3267 * allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003268 operation->iv_set = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003269 if( alg == PSA_ALG_ECB_NO_PADDING )
3270 operation->iv_required = 0;
3271 else
3272 operation->iv_required = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003273 operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
Ronald Cronab99ac22020-10-01 16:38:28 +02003274
Ronald Crona4af55f2020-12-14 14:36:06 +01003275 psa_key_attributes_t attributes = {
3276 .core = slot->attr
3277 };
Steven Cooremana07b9972020-09-10 14:54:14 +02003278
Steven Cooremana07b9972020-09-10 14:54:14 +02003279 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003280 if( cipher_operation == MBEDTLS_ENCRYPT )
Ronald Crona4af55f2020-12-14 14:36:06 +01003281 status = psa_driver_wrapper_cipher_encrypt_setup( operation,
3282 &attributes,
3283 slot->key.data,
3284 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003285 alg );
3286 else
Ronald Crona4af55f2020-12-14 14:36:06 +01003287 status = psa_driver_wrapper_cipher_decrypt_setup( operation,
3288 &attributes,
3289 slot->key.data,
3290 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003291 alg );
3292
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003293exit:
Ronald Cron06aa4422021-03-09 17:32:57 +01003294 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003295 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003296
Ronald Cron5c522922020-11-14 16:35:34 +01003297 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003298
Ronald Cron5c522922020-11-14 16:35:34 +01003299 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003300}
3301
Gilles Peskinefe119512018-07-08 21:39:34 +02003302psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003303 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003304 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003305{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003306 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003307}
3308
Gilles Peskinefe119512018-07-08 21:39:34 +02003309psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003310 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003311 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003312{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003313 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003314}
3315
Gilles Peskinefe119512018-07-08 21:39:34 +02003316psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003317 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003318 size_t iv_size,
3319 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003320{
Ronald Cron6d051732020-10-01 14:10:20 +02003321 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003322
Ronald Cron5618a392021-03-26 09:52:26 +01003323 *iv_length = 0;
3324
Ronald Cron49fafa92021-03-10 08:34:23 +01003325 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003326 {
3327 return( PSA_ERROR_BAD_STATE );
3328 }
3329
Steven Cooreman7df02922020-09-09 15:28:49 +02003330 if( operation->iv_set || ! operation->iv_required )
3331 {
3332 return( PSA_ERROR_BAD_STATE );
3333 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003334
Ronald Cron5618a392021-03-26 09:52:26 +01003335 if( iv_size < operation->default_iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003336 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003337 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003338 goto exit;
3339 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003340
Ronald Cron5618a392021-03-26 09:52:26 +01003341 status = psa_generate_random( iv, operation->default_iv_length );
3342 if( status != PSA_SUCCESS )
3343 goto exit;
3344
3345 status = psa_driver_wrapper_cipher_set_iv( operation,
3346 iv,
3347 operation->default_iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003348
Moran Peker395db872018-05-31 14:07:14 +03003349exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003350 if( status == PSA_SUCCESS )
Ronald Cron5618a392021-03-26 09:52:26 +01003351 {
Steven Cooreman7df02922020-09-09 15:28:49 +02003352 operation->iv_set = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003353 *iv_length = operation->default_iv_length;
3354 }
Steven Cooreman7df02922020-09-09 15:28:49 +02003355 else
Moran Peker395db872018-05-31 14:07:14 +03003356 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003357
itayzafrir534bd7c2018-08-02 13:56:32 +03003358 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003359}
3360
Gilles Peskinefe119512018-07-08 21:39:34 +02003361psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003362 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003363 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003364{
Ronald Cron6d051732020-10-01 14:10:20 +02003365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003366
Ronald Cron49fafa92021-03-10 08:34:23 +01003367 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003368 return( PSA_ERROR_BAD_STATE );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003369
mohammad1603503973b2018-03-12 15:59:30 +02003370 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003371 return( PSA_ERROR_BAD_STATE );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003372
Ronald Crona0d68172021-03-26 10:15:08 +01003373 if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
3374 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad1603503973b2018-03-12 15:59:30 +02003375
Ronald Crondd24c9b2020-12-15 14:10:01 +01003376 status = psa_driver_wrapper_cipher_set_iv( operation,
3377 iv,
3378 iv_length );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003379
itayzafrir534bd7c2018-08-02 13:56:32 +03003380 if( status == PSA_SUCCESS )
3381 operation->iv_set = 1;
3382 else
mohammad1603503973b2018-03-12 15:59:30 +02003383 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003384 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003385}
3386
Gilles Peskinee553c652018-06-04 16:22:46 +02003387psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3388 const uint8_t *input,
3389 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003390 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003391 size_t output_size,
3392 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003393{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003395
Ronald Cron49fafa92021-03-10 08:34:23 +01003396 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003397 {
3398 return( PSA_ERROR_BAD_STATE );
3399 }
3400 if( operation->iv_required && ! operation->iv_set )
3401 {
3402 return( PSA_ERROR_BAD_STATE );
3403 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003404
Ronald Crondd24c9b2020-12-15 14:10:01 +01003405 status = psa_driver_wrapper_cipher_update( operation,
3406 input,
3407 input_length,
3408 output,
3409 output_size,
3410 output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003411 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003412 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003413
itayzafrir534bd7c2018-08-02 13:56:32 +03003414 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003415}
3416
Gilles Peskinee553c652018-06-04 16:22:46 +02003417psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3418 uint8_t *output,
3419 size_t output_size,
3420 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003421{
David Saadab4ecc272019-02-14 13:48:10 +02003422 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003423
Ronald Cron49fafa92021-03-10 08:34:23 +01003424 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003425 {
3426 return( PSA_ERROR_BAD_STATE );
3427 }
3428 if( operation->iv_required && ! operation->iv_set )
3429 {
3430 return( PSA_ERROR_BAD_STATE );
3431 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003432
Ronald Crondd24c9b2020-12-15 14:10:01 +01003433 status = psa_driver_wrapper_cipher_finish( operation,
3434 output,
3435 output_size,
3436 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003437 if( status == PSA_SUCCESS )
3438 return( psa_cipher_abort( operation ) );
3439 else
3440 {
3441 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003442 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003443
Steven Cooremanef8575e2020-09-11 11:44:50 +02003444 return( status );
3445 }
mohammad1603503973b2018-03-12 15:59:30 +02003446}
3447
Gilles Peskinee553c652018-06-04 16:22:46 +02003448psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3449{
Ronald Cron49fafa92021-03-10 08:34:23 +01003450 if( operation->id == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003451 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003452 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003453 * in use. It's ok to call abort on such an object, and there's
3454 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003455 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003456 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003457
Ronald Crondd24c9b2020-12-15 14:10:01 +01003458 psa_driver_wrapper_cipher_abort( operation );
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003459
Ronald Cron49fafa92021-03-10 08:34:23 +01003460 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003461 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003462 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003463
Moran Peker395db872018-05-31 14:07:14 +03003464 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003465}
3466
mohammad16035955c982018-04-26 00:53:03 +03003467/****************************************************************/
3468/* AEAD */
3469/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003470
Ronald Croncf56a0a2020-08-04 09:51:30 +02003471psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003472 psa_algorithm_t alg,
3473 const uint8_t *nonce,
3474 size_t nonce_length,
3475 const uint8_t *additional_data,
3476 size_t additional_data_length,
3477 const uint8_t *plaintext,
3478 size_t plaintext_length,
3479 uint8_t *ciphertext,
3480 size_t ciphertext_size,
3481 size_t *ciphertext_length )
3482{
Ronald Cron215633c2021-03-16 17:15:37 +01003483 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3484 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003485
mohammad1603f08a5502018-06-03 15:05:47 +03003486 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003487
Steven Cooremanea7ab132021-03-17 16:28:00 +01003488 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3489 return( PSA_ERROR_NOT_SUPPORTED );
3490
Ronald Cron9a986162021-03-26 12:40:07 +01003491 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003492 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003493 if( status != PSA_SUCCESS )
3494 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003495
Ronald Cron215633c2021-03-16 17:15:37 +01003496 psa_key_attributes_t attributes = {
3497 .core = slot->attr
3498 };
mohammad16035955c982018-04-26 00:53:03 +03003499
Ronald Cronde822812021-03-17 16:08:20 +01003500 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003501 &attributes, slot->key.data, slot->key.bytes,
3502 alg,
3503 nonce, nonce_length,
3504 additional_data, additional_data_length,
3505 plaintext, plaintext_length,
3506 ciphertext, ciphertext_size, ciphertext_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003507
Gilles Peskineedf9a652018-08-17 18:11:56 +02003508 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3509 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003510
Ronald Cron9f310172021-03-16 16:36:37 +01003511 psa_unlock_key_slot( slot );
mohammad16035955c982018-04-26 00:53:03 +03003512
mohammad16035955c982018-04-26 00:53:03 +03003513 return( status );
Gilles Peskineee652a32018-06-01 19:23:52 +02003514}
3515
Ronald Croncf56a0a2020-08-04 09:51:30 +02003516psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003517 psa_algorithm_t alg,
3518 const uint8_t *nonce,
3519 size_t nonce_length,
3520 const uint8_t *additional_data,
3521 size_t additional_data_length,
3522 const uint8_t *ciphertext,
3523 size_t ciphertext_length,
3524 uint8_t *plaintext,
3525 size_t plaintext_size,
3526 size_t *plaintext_length )
3527{
Ronald Cron215633c2021-03-16 17:15:37 +01003528 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3529 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003530
Gilles Peskineee652a32018-06-01 19:23:52 +02003531 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003532
Steven Cooremanea7ab132021-03-17 16:28:00 +01003533 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3534 return( PSA_ERROR_NOT_SUPPORTED );
3535
Ronald Cron9a986162021-03-26 12:40:07 +01003536 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003537 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003538 if( status != PSA_SUCCESS )
3539 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003540
Ronald Cron215633c2021-03-16 17:15:37 +01003541 psa_key_attributes_t attributes = {
3542 .core = slot->attr
3543 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003544
Ronald Cronde822812021-03-17 16:08:20 +01003545 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003546 &attributes, slot->key.data, slot->key.bytes,
3547 alg,
3548 nonce, nonce_length,
3549 additional_data, additional_data_length,
3550 ciphertext, ciphertext_length,
3551 plaintext, plaintext_size, plaintext_length );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003552
Gilles Peskineedf9a652018-08-17 18:11:56 +02003553 if( status != PSA_SUCCESS && plaintext_size != 0 )
3554 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003555
Ronald Cron215633c2021-03-16 17:15:37 +01003556 psa_unlock_key_slot( slot );
3557
Gilles Peskineedf9a652018-08-17 18:11:56 +02003558 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003559}
3560
Gilles Peskinee59236f2018-01-27 23:32:46 +01003561/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003562/* Generators */
3563/****************************************************************/
3564
John Durkop07cc04a2020-11-16 22:08:34 -08003565#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3566 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3567 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3568#define AT_LEAST_ONE_BUILTIN_KDF
3569#endif
3570
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003571#define HKDF_STATE_INIT 0 /* no input yet */
3572#define HKDF_STATE_STARTED 1 /* got salt */
3573#define HKDF_STATE_KEYED 2 /* got key */
3574#define HKDF_STATE_OUTPUT 3 /* output started */
3575
Gilles Peskinecbe66502019-05-16 16:59:18 +02003576static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003577 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003578{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003579 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3580 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003581 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003582 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003583}
3584
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003585psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003586{
3587 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003588 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003589 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003590 {
3591 /* The object has (apparently) been initialized but it is not
3592 * in use. It's ok to call abort on such an object, and there's
3593 * nothing to do. */
3594 }
3595 else
John Durkopd0321952020-10-29 21:37:36 -07003596#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003597 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003598 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003599 mbedtls_free( operation->ctx.hkdf.info );
3600 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003601 }
John Durkop07cc04a2020-11-16 22:08:34 -08003602 else
3603#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3604#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3605 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3606 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003607 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003608 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003609 {
Janos Follath6a1d2622019-06-11 10:37:28 +01003610 if( operation->ctx.tls12_prf.seed != NULL )
3611 {
3612 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
3613 operation->ctx.tls12_prf.seed_length );
3614 mbedtls_free( operation->ctx.tls12_prf.seed );
3615 }
3616
3617 if( operation->ctx.tls12_prf.label != NULL )
3618 {
3619 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
3620 operation->ctx.tls12_prf.label_length );
3621 mbedtls_free( operation->ctx.tls12_prf.label );
3622 }
3623
3624 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
3625
3626 /* We leave the fields Ai and output_block to be erased safely by the
3627 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003628 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003629 else
John Durkop07cc04a2020-11-16 22:08:34 -08003630#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3631 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003632 {
3633 status = PSA_ERROR_BAD_STATE;
3634 }
Janos Follath083036a2019-06-11 10:22:26 +01003635 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003636 return( status );
3637}
3638
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003639psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003640 size_t *capacity)
3641{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003642 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003643 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003644 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02003645 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003646 }
3647
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003648 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003649 return( PSA_SUCCESS );
3650}
3651
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003652psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003653 size_t capacity )
3654{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003655 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003656 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003657 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003658 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003659 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003660 return( PSA_SUCCESS );
3661}
3662
John Durkopd0321952020-10-29 21:37:36 -07003663#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003664/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003665 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003666static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003667 psa_algorithm_t hash_alg,
3668 uint8_t *output,
3669 size_t output_length )
3670{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003671 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003672 psa_status_t status;
3673
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003674 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3675 return( PSA_ERROR_BAD_STATE );
3676 hkdf->state = HKDF_STATE_OUTPUT;
3677
Gilles Peskinebef7f142018-07-12 17:22:21 +02003678 while( output_length != 0 )
3679 {
3680 /* Copy what remains of the current block */
3681 uint8_t n = hash_length - hkdf->offset_in_block;
3682 if( n > output_length )
3683 n = (uint8_t) output_length;
3684 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3685 output += n;
3686 output_length -= n;
3687 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003688 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003689 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003690 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003691 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003692 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003693 * object was corrupted or if this function is called directly
3694 * inside the library. */
3695 if( hkdf->block_number == 0xff )
3696 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003697
3698 /* We need a new block */
3699 ++hkdf->block_number;
3700 hkdf->offset_in_block = 0;
3701 status = psa_hmac_setup_internal( &hkdf->hmac,
3702 hkdf->prk, hash_length,
3703 hash_alg );
3704 if( status != PSA_SUCCESS )
3705 return( status );
3706 if( hkdf->block_number != 1 )
3707 {
3708 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3709 hkdf->output_block,
3710 hash_length );
3711 if( status != PSA_SUCCESS )
3712 return( status );
3713 }
3714 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3715 hkdf->info,
3716 hkdf->info_length );
3717 if( status != PSA_SUCCESS )
3718 return( status );
3719 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3720 &hkdf->block_number, 1 );
3721 if( status != PSA_SUCCESS )
3722 return( status );
3723 status = psa_hmac_finish_internal( &hkdf->hmac,
3724 hkdf->output_block,
3725 sizeof( hkdf->output_block ) );
3726 if( status != PSA_SUCCESS )
3727 return( status );
3728 }
3729
3730 return( PSA_SUCCESS );
3731}
John Durkop07cc04a2020-11-16 22:08:34 -08003732#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003733
John Durkop07cc04a2020-11-16 22:08:34 -08003734#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3735 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01003736static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
3737 psa_tls12_prf_key_derivation_t *tls12_prf,
3738 psa_algorithm_t alg )
3739{
3740 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003741 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01003742 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
3743 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003744
Janos Follath7742fee2019-06-17 12:58:10 +01003745 /* We can't be wanting more output after block 0xff, otherwise
3746 * the capacity check in psa_key_derivation_output_bytes() would have
3747 * prevented this call. It could happen only if the operation
3748 * object was corrupted or if this function is called directly
3749 * inside the library. */
3750 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01003751 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01003752
3753 /* We need a new block */
3754 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01003755 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01003756
3757 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3758 *
3759 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3760 *
3761 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3762 * HMAC_hash(secret, A(2) + seed) +
3763 * HMAC_hash(secret, A(3) + seed) + ...
3764 *
3765 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01003766 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01003767 *
Janos Follathea29bfb2019-06-19 12:21:20 +01003768 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01003769 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01003770 * is currently extracted as `output_block` and where i is
3771 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01003772 */
3773
Janos Follathea29bfb2019-06-19 12:21:20 +01003774 /* Save the hash context before using it, to preserve the hash state with
3775 * only the inner padding in it. We need this, because inner padding depends
3776 * on the key (secret in the RFC's terminology). */
3777 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
3778 if( status != PSA_SUCCESS )
3779 goto cleanup;
3780
3781 /* Calculate A(i) where i = tls12_prf->block_number. */
3782 if( tls12_prf->block_number == 1 )
3783 {
3784 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
3785 * the variable seed and in this instance means it in the context of the
3786 * P_hash function, where seed = label + seed.) */
3787 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3788 tls12_prf->label, tls12_prf->label_length );
3789 if( status != PSA_SUCCESS )
3790 goto cleanup;
3791 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3792 tls12_prf->seed, tls12_prf->seed_length );
3793 if( status != PSA_SUCCESS )
3794 goto cleanup;
3795 }
3796 else
3797 {
3798 /* A(i) = HMAC_hash(secret, A(i-1)) */
3799 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3800 tls12_prf->Ai, hash_length );
3801 if( status != PSA_SUCCESS )
3802 goto cleanup;
3803 }
3804
3805 status = psa_hmac_finish_internal( &tls12_prf->hmac,
3806 tls12_prf->Ai, hash_length );
3807 if( status != PSA_SUCCESS )
3808 goto cleanup;
3809 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
3810 if( status != PSA_SUCCESS )
3811 goto cleanup;
3812
3813 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
3814 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3815 tls12_prf->Ai, hash_length );
3816 if( status != PSA_SUCCESS )
3817 goto cleanup;
3818 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3819 tls12_prf->label, tls12_prf->label_length );
3820 if( status != PSA_SUCCESS )
3821 goto cleanup;
3822 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
3823 tls12_prf->seed, tls12_prf->seed_length );
3824 if( status != PSA_SUCCESS )
3825 goto cleanup;
3826 status = psa_hmac_finish_internal( &tls12_prf->hmac,
3827 tls12_prf->output_block, hash_length );
3828 if( status != PSA_SUCCESS )
3829 goto cleanup;
3830 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
3831 if( status != PSA_SUCCESS )
3832 goto cleanup;
3833
Janos Follath7742fee2019-06-17 12:58:10 +01003834
3835cleanup:
3836
Janos Follathea29bfb2019-06-19 12:21:20 +01003837 cleanup_status = psa_hash_abort( &backup );
3838 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3839 status = cleanup_status;
3840
3841 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01003842}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003843
Janos Follath844eb0e2019-06-19 12:10:49 +01003844static psa_status_t psa_key_derivation_tls12_prf_read(
3845 psa_tls12_prf_key_derivation_t *tls12_prf,
3846 psa_algorithm_t alg,
3847 uint8_t *output,
3848 size_t output_length )
3849{
3850 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003851 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01003852 psa_status_t status;
3853 uint8_t offset, length;
3854
3855 while( output_length != 0 )
3856 {
3857 /* Check if we have fully processed the current block. */
3858 if( tls12_prf->left_in_block == 0 )
3859 {
3860 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
3861 alg );
3862 if( status != PSA_SUCCESS )
3863 return( status );
3864
3865 continue;
3866 }
3867
3868 if( tls12_prf->left_in_block > output_length )
3869 length = (uint8_t) output_length;
3870 else
3871 length = tls12_prf->left_in_block;
3872
3873 offset = hash_length - tls12_prf->left_in_block;
3874 memcpy( output, tls12_prf->output_block + offset, length );
3875 output += length;
3876 output_length -= length;
3877 tls12_prf->left_in_block -= length;
3878 }
3879
3880 return( PSA_SUCCESS );
3881}
John Durkop07cc04a2020-11-16 22:08:34 -08003882#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
3883 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003884
Janos Follath6c6c8fc2019-06-17 12:38:20 +01003885psa_status_t psa_key_derivation_output_bytes(
3886 psa_key_derivation_operation_t *operation,
3887 uint8_t *output,
3888 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003889{
3890 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003891 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003892
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003893 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003894 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003895 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02003896 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003897 }
3898
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003899 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003900 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003901 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003902 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003903 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02003904 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003905 goto exit;
3906 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003907 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003908 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003909 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003910 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02003911 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3912 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003913 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02003914 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02003915 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003916 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003917 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003918
John Durkopd0321952020-10-29 21:37:36 -07003919#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003920 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003921 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01003922 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003923 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003924 output, output_length );
3925 }
Janos Follathadbec812019-06-14 11:05:39 +01003926 else
John Durkop07cc04a2020-11-16 22:08:34 -08003927#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3928#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3929 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01003930 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08003931 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003932 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003933 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003934 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003935 output_length );
3936 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003937 else
John Durkop07cc04a2020-11-16 22:08:34 -08003938#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
3939 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003940 {
Steven Cooreman74afe472021-02-10 17:19:22 +01003941 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003942 return( PSA_ERROR_BAD_STATE );
3943 }
3944
3945exit:
3946 if( status != PSA_SUCCESS )
3947 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003948 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003949 * This allows us to differentiate between exhausted operations and
3950 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
3951 * operations. */
3952 psa_algorithm_t alg = operation->alg;
3953 psa_key_derivation_abort( operation );
3954 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003955 memset( output, '!', output_length );
3956 }
3957 return( status );
3958}
3959
David Brown7807bf72021-02-09 16:28:23 -07003960#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02003961static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3962{
3963 if( data_size >= 8 )
3964 mbedtls_des_key_set_parity( data );
3965 if( data_size >= 16 )
3966 mbedtls_des_key_set_parity( data + 8 );
3967 if( data_size >= 24 )
3968 mbedtls_des_key_set_parity( data + 16 );
3969}
David Brown7807bf72021-02-09 16:28:23 -07003970#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02003971
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003972static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02003973 psa_key_slot_t *slot,
3974 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003975 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003976{
3977 uint8_t *data = NULL;
3978 size_t bytes = PSA_BITS_TO_BYTES( bits );
3979 psa_status_t status;
3980
Gilles Peskine8e338702019-07-30 20:06:31 +02003981 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003982 return( PSA_ERROR_INVALID_ARGUMENT );
3983 if( bits % 8 != 0 )
3984 return( PSA_ERROR_INVALID_ARGUMENT );
3985 data = mbedtls_calloc( 1, bytes );
3986 if( data == NULL )
3987 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3988
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003989 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003990 if( status != PSA_SUCCESS )
3991 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07003992#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02003993 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003994 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07003995#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01003996
3997 status = psa_allocate_buffer_to_slot( slot, bytes );
3998 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00003999 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01004000
Ronald Cronbf33c932020-11-28 18:06:53 +01004001 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004002 psa_key_attributes_t attributes = {
4003 .core = slot->attr
4004 };
4005
Ronald Cronbf33c932020-11-28 18:06:53 +01004006 status = psa_driver_wrapper_import_key( &attributes,
4007 data, bytes,
4008 slot->key.data,
4009 slot->key.bytes,
4010 &slot->key.bytes, &bits );
4011 if( bits != slot->attr.bits )
4012 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004013
4014exit:
4015 mbedtls_free( data );
4016 return( status );
4017}
4018
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004019psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004020 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004021 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004022{
4023 psa_status_t status;
4024 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004025 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004026
Ronald Cron81709fc2020-11-14 12:10:32 +01004027 *key = MBEDTLS_SVC_KEY_ID_INIT;
4028
Gilles Peskine0f84d622019-09-12 19:03:13 +02004029 /* Reject any attempt to create a zero-length key so that we don't
4030 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4031 if( psa_get_key_bits( attributes ) == 0 )
4032 return( PSA_ERROR_INVALID_ARGUMENT );
4033
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004034 if( ! operation->can_output_key )
4035 return( PSA_ERROR_NOT_PERMITTED );
4036
Ronald Cron81709fc2020-11-14 12:10:32 +01004037 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
4038 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004039#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4040 if( driver != NULL )
4041 {
4042 /* Deriving a key in a secure element is not implemented yet. */
4043 status = PSA_ERROR_NOT_SUPPORTED;
4044 }
4045#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004046 if( status == PSA_SUCCESS )
4047 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004048 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004049 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004050 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004051 }
4052 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01004053 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004054 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004055 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004056
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004057 return( status );
4058}
4059
Gilles Peskineeab56e42018-07-12 17:12:33 +02004060
4061
4062/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004063/* Key derivation */
4064/****************************************************************/
4065
Steven Cooreman932ffb72021-02-15 12:14:32 +01004066#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004067static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004068 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004069 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004070{
John Durkop07cc04a2020-11-16 22:08:34 -08004071 int is_kdf_alg_supported;
4072
Janos Follath5fe19732019-06-20 15:09:30 +01004073 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4074 * initialisers for this union leave some bytes unspecified.) */
4075 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4076
Gilles Peskine969c5d62019-01-16 15:53:06 +01004077 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07004078#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08004079 if( PSA_ALG_IS_HKDF( kdf_alg ) )
4080 is_kdf_alg_supported = 1;
4081 else
4082#endif
4083#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4084 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
4085 is_kdf_alg_supported = 1;
4086 else
4087#endif
4088#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4089 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
4090 is_kdf_alg_supported = 1;
4091 else
4092#endif
4093 is_kdf_alg_supported = 0;
4094
4095 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004096 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004097 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004098 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004099 if( hash_size == 0 )
4100 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004101 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4102 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004103 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004104 {
4105 return( PSA_ERROR_NOT_SUPPORTED );
4106 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004107 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004108 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004109 }
John Durkop07cc04a2020-11-16 22:08:34 -08004110
4111 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004112}
John Durkop07cc04a2020-11-16 22:08:34 -08004113#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004114
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004115psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004116 psa_algorithm_t alg )
4117{
4118 psa_status_t status;
4119
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004120 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004121 return( PSA_ERROR_BAD_STATE );
4122
Gilles Peskine6843c292019-01-18 16:44:49 +01004123 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4124 return( PSA_ERROR_INVALID_ARGUMENT );
4125 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004126 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004127#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004128 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004129 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004130#else
4131 return( PSA_ERROR_NOT_SUPPORTED );
4132#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004133 }
4134 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4135 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004136#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004137 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004138#else
4139 return( PSA_ERROR_NOT_SUPPORTED );
4140#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004141 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004142 else
4143 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004144
4145 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004146 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004147 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004148}
4149
John Durkopd0321952020-10-29 21:37:36 -07004150#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004151static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004152 psa_algorithm_t hash_alg,
4153 psa_key_derivation_step_t step,
4154 const uint8_t *data,
4155 size_t data_length )
4156{
4157 psa_status_t status;
4158 switch( step )
4159 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004160 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004161 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004162 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004163 status = psa_hmac_setup_internal( &hkdf->hmac,
4164 data, data_length,
4165 hash_alg );
4166 if( status != PSA_SUCCESS )
4167 return( status );
4168 hkdf->state = HKDF_STATE_STARTED;
4169 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004170 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004171 /* If no salt was provided, use an empty salt. */
4172 if( hkdf->state == HKDF_STATE_INIT )
4173 {
4174 status = psa_hmac_setup_internal( &hkdf->hmac,
4175 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004176 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004177 if( status != PSA_SUCCESS )
4178 return( status );
4179 hkdf->state = HKDF_STATE_STARTED;
4180 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004181 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004182 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004183 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4184 data, data_length );
4185 if( status != PSA_SUCCESS )
4186 return( status );
4187 status = psa_hmac_finish_internal( &hkdf->hmac,
4188 hkdf->prk,
4189 sizeof( hkdf->prk ) );
4190 if( status != PSA_SUCCESS )
4191 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004192 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004193 hkdf->block_number = 0;
4194 hkdf->state = HKDF_STATE_KEYED;
4195 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004196 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004197 if( hkdf->state == HKDF_STATE_OUTPUT )
4198 return( PSA_ERROR_BAD_STATE );
4199 if( hkdf->info_set )
4200 return( PSA_ERROR_BAD_STATE );
4201 hkdf->info_length = data_length;
4202 if( data_length != 0 )
4203 {
4204 hkdf->info = mbedtls_calloc( 1, data_length );
4205 if( hkdf->info == NULL )
4206 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4207 memcpy( hkdf->info, data, data_length );
4208 }
4209 hkdf->info_set = 1;
4210 return( PSA_SUCCESS );
4211 default:
4212 return( PSA_ERROR_INVALID_ARGUMENT );
4213 }
4214}
John Durkop07cc04a2020-11-16 22:08:34 -08004215#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004216
John Durkop07cc04a2020-11-16 22:08:34 -08004217#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4218 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01004219static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4220 const uint8_t *data,
4221 size_t data_length )
4222{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004223 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01004224 return( PSA_ERROR_BAD_STATE );
4225
Janos Follathd6dce9f2019-07-04 09:11:38 +01004226 if( data_length != 0 )
4227 {
4228 prf->seed = mbedtls_calloc( 1, data_length );
4229 if( prf->seed == NULL )
4230 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004231
Janos Follathd6dce9f2019-07-04 09:11:38 +01004232 memcpy( prf->seed, data, data_length );
4233 prf->seed_length = data_length;
4234 }
Janos Follathf08e2652019-06-13 09:05:41 +01004235
Gilles Peskine43f958b2020-12-13 14:55:14 +01004236 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004237
4238 return( PSA_SUCCESS );
4239}
4240
Janos Follath81550542019-06-13 14:26:34 +01004241static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4242 psa_algorithm_t hash_alg,
4243 const uint8_t *data,
4244 size_t data_length )
4245{
4246 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01004247 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01004248 return( PSA_ERROR_BAD_STATE );
4249
4250 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4251 if( status != PSA_SUCCESS )
4252 return( status );
4253
Gilles Peskine43f958b2020-12-13 14:55:14 +01004254 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004255
4256 return( PSA_SUCCESS );
4257}
4258
Janos Follath63028dd2019-06-13 09:15:47 +01004259static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
4260 const uint8_t *data,
4261 size_t data_length )
4262{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004263 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01004264 return( PSA_ERROR_BAD_STATE );
4265
Janos Follathd6dce9f2019-07-04 09:11:38 +01004266 if( data_length != 0 )
4267 {
4268 prf->label = mbedtls_calloc( 1, data_length );
4269 if( prf->label == NULL )
4270 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01004271
Janos Follathd6dce9f2019-07-04 09:11:38 +01004272 memcpy( prf->label, data, data_length );
4273 prf->label_length = data_length;
4274 }
Janos Follath63028dd2019-06-13 09:15:47 +01004275
Gilles Peskine43f958b2020-12-13 14:55:14 +01004276 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004277
4278 return( PSA_SUCCESS );
4279}
4280
Janos Follathb03233e2019-06-11 15:30:30 +01004281static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
4282 psa_algorithm_t hash_alg,
4283 psa_key_derivation_step_t step,
4284 const uint8_t *data,
4285 size_t data_length )
4286{
Janos Follathb03233e2019-06-11 15:30:30 +01004287 switch( step )
4288 {
Janos Follathf08e2652019-06-13 09:05:41 +01004289 case PSA_KEY_DERIVATION_INPUT_SEED:
4290 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01004291 case PSA_KEY_DERIVATION_INPUT_SECRET:
4292 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01004293 case PSA_KEY_DERIVATION_INPUT_LABEL:
4294 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01004295 default:
4296 return( PSA_ERROR_INVALID_ARGUMENT );
4297 }
4298}
John Durkop07cc04a2020-11-16 22:08:34 -08004299#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4300 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4301
4302#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4303static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4304 psa_tls12_prf_key_derivation_t *prf,
4305 psa_algorithm_t hash_alg,
4306 const uint8_t *data,
4307 size_t data_length )
4308{
4309 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004310 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08004311 uint8_t *cur = pms;
4312
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004313 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08004314 return( PSA_ERROR_INVALID_ARGUMENT );
4315
4316 /* Quoting RFC 4279, Section 2:
4317 *
4318 * The premaster secret is formed as follows: if the PSK is N octets
4319 * long, concatenate a uint16 with the value N, N zero octets, a second
4320 * uint16 with the value N, and the PSK itself.
4321 */
4322
4323 *cur++ = ( data_length >> 8 ) & 0xff;
4324 *cur++ = ( data_length >> 0 ) & 0xff;
4325 memset( cur, 0, data_length );
4326 cur += data_length;
4327 *cur++ = pms[0];
4328 *cur++ = pms[1];
4329 memcpy( cur, data, data_length );
4330 cur += data_length;
4331
4332 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
4333
4334 mbedtls_platform_zeroize( pms, sizeof( pms ) );
4335 return( status );
4336}
Janos Follath6660f0e2019-06-17 08:44:03 +01004337
4338static psa_status_t psa_tls12_prf_psk_to_ms_input(
4339 psa_tls12_prf_key_derivation_t *prf,
4340 psa_algorithm_t hash_alg,
4341 psa_key_derivation_step_t step,
4342 const uint8_t *data,
4343 size_t data_length )
4344{
4345 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01004346 {
Janos Follath6660f0e2019-06-17 08:44:03 +01004347 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
4348 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01004349 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004350
4351 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
4352}
John Durkop07cc04a2020-11-16 22:08:34 -08004353#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004354
Gilles Peskineb8965192019-09-24 16:21:10 +02004355/** Check whether the given key type is acceptable for the given
4356 * input step of a key derivation.
4357 *
4358 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4359 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4360 * Both secret and non-secret inputs can alternatively have the type
4361 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4362 * that the input was passed as a buffer rather than via a key object.
4363 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004364static int psa_key_derivation_check_input_type(
4365 psa_key_derivation_step_t step,
4366 psa_key_type_t key_type )
4367{
4368 switch( step )
4369 {
4370 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02004371 if( key_type == PSA_KEY_TYPE_DERIVE )
4372 return( PSA_SUCCESS );
4373 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004374 return( PSA_SUCCESS );
4375 break;
4376 case PSA_KEY_DERIVATION_INPUT_LABEL:
4377 case PSA_KEY_DERIVATION_INPUT_SALT:
4378 case PSA_KEY_DERIVATION_INPUT_INFO:
4379 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02004380 if( key_type == PSA_KEY_TYPE_RAW_DATA )
4381 return( PSA_SUCCESS );
4382 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004383 return( PSA_SUCCESS );
4384 break;
4385 }
4386 return( PSA_ERROR_INVALID_ARGUMENT );
4387}
4388
Janos Follathb80a94e2019-06-12 15:54:46 +01004389static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004390 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004391 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004392 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004393 const uint8_t *data,
4394 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004395{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004396 psa_status_t status;
4397 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
4398
4399 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02004400 if( status != PSA_SUCCESS )
4401 goto exit;
4402
John Durkopd0321952020-10-29 21:37:36 -07004403#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004404 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004405 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004407 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004408 step, data, data_length );
4409 }
John Durkop07cc04a2020-11-16 22:08:34 -08004410 else
4411#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4412#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4413 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004414 {
Janos Follathb03233e2019-06-11 15:30:30 +01004415 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
4416 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
4417 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01004418 }
John Durkop07cc04a2020-11-16 22:08:34 -08004419 else
4420#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4421#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4422 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01004423 {
4424 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
4425 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
4426 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004427 }
4428 else
John Durkop07cc04a2020-11-16 22:08:34 -08004429#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004430 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004432 (void) data;
4433 (void) data_length;
4434 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004435 return( PSA_ERROR_BAD_STATE );
4436 }
4437
Gilles Peskine224b0d62019-09-23 18:13:17 +02004438exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004439 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004440 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004441 return( status );
4442}
4443
Janos Follath51f4a0f2019-06-14 11:35:55 +01004444psa_status_t psa_key_derivation_input_bytes(
4445 psa_key_derivation_operation_t *operation,
4446 psa_key_derivation_step_t step,
4447 const uint8_t *data,
4448 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004449{
Gilles Peskineb8965192019-09-24 16:21:10 +02004450 return( psa_key_derivation_input_internal( operation, step,
4451 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01004452 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004453}
4454
Janos Follath51f4a0f2019-06-14 11:35:55 +01004455psa_status_t psa_key_derivation_input_key(
4456 psa_key_derivation_operation_t *operation,
4457 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004458 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004459{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004461 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004462 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004463
Ronald Cron5c522922020-11-14 16:35:34 +01004464 status = psa_get_and_lock_transparent_key_slot_with_policy(
4465 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004466 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02004467 {
4468 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004469 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02004470 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004471
4472 /* Passing a key object as a SECRET input unlocks the permission
4473 * to output to a key object. */
4474 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4475 operation->can_output_key = 1;
4476
Ronald Cronf95a2b12020-10-22 15:24:49 +02004477 status = psa_key_derivation_input_internal( operation,
4478 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004479 slot->key.data,
4480 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004481
Ronald Cron5c522922020-11-14 16:35:34 +01004482 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004483
Ronald Cron5c522922020-11-14 16:35:34 +01004484 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004485}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004486
4487
4488
4489/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004490/* Key agreement */
4491/****************************************************************/
4492
John Durkop6ba40d12020-11-10 08:50:04 -08004493#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004494static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4495 size_t peer_key_length,
4496 const mbedtls_ecp_keypair *our_key,
4497 uint8_t *shared_secret,
4498 size_t shared_secret_size,
4499 size_t *shared_secret_length )
4500{
Steven Cooremand4867872020-08-05 16:31:39 +02004501 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004502 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004503 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004504 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01004505 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004506 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004507
Ronald Cron90857082020-11-25 14:58:33 +01004508 status = mbedtls_psa_ecp_load_representation(
4509 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004510 bits,
Ronald Cron90857082020-11-25 14:58:33 +01004511 peer_key,
4512 peer_key_length,
4513 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004514 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004515 goto exit;
4516
Jaeden Amero97271b32019-01-10 19:38:51 +00004517 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02004518 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00004519 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004520 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004521 status = mbedtls_to_psa_error(
4522 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4523 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004524 goto exit;
4525
Jaeden Amero97271b32019-01-10 19:38:51 +00004526 status = mbedtls_to_psa_error(
4527 mbedtls_ecdh_calc_secret( &ecdh,
4528 shared_secret_length,
4529 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004530 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004531 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004532 if( status != PSA_SUCCESS )
4533 goto exit;
4534 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
4535 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004536
4537exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01004538 if( status != PSA_SUCCESS )
4539 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004540 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02004541 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02004542 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02004543
Jaeden Amero97271b32019-01-10 19:38:51 +00004544 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004545}
John Durkop6ba40d12020-11-10 08:50:04 -08004546#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004547
Gilles Peskine01d718c2018-09-18 12:01:02 +02004548#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4549
Gilles Peskine0216fe12019-04-11 21:23:21 +02004550static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4551 psa_key_slot_t *private_key,
4552 const uint8_t *peer_key,
4553 size_t peer_key_length,
4554 uint8_t *shared_secret,
4555 size_t shared_secret_size,
4556 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004557{
Gilles Peskine0216fe12019-04-11 21:23:21 +02004558 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004559 {
John Durkop6ba40d12020-11-10 08:50:04 -08004560#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004561 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02004562 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004563 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02004564 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004565 psa_status_t status = mbedtls_psa_ecp_load_representation(
4566 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004567 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01004568 private_key->key.data,
4569 private_key->key.bytes,
4570 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004571 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004572 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02004573 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02004574 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004575 shared_secret, shared_secret_size,
4576 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004577 mbedtls_ecp_keypair_free( ecp );
4578 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02004579 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08004580#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004581 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004582 (void) private_key;
4583 (void) peer_key;
4584 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004585 (void) shared_secret;
4586 (void) shared_secret_size;
4587 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004588 return( PSA_ERROR_NOT_SUPPORTED );
4589 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004590}
4591
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004592/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004593 * to potentially free embedded data structures and wipe confidential data.
4594 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004596 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004597 psa_key_slot_t *private_key,
4598 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004599 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004600{
4601 psa_status_t status;
4602 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4603 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004604 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004605
4606 /* Step 1: run the secret agreement algorithm to generate the shared
4607 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004608 status = psa_key_agreement_raw_internal( ka_alg,
4609 private_key,
4610 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03004611 shared_secret,
4612 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02004613 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004614 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004615 goto exit;
4616
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004617 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02004618 * the shared secret. A shared secret is permitted wherever a key
4619 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01004620 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004621 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01004622 shared_secret,
4623 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004624exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004625 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004626 return( status );
4627}
4628
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004629psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004630 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004631 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004632 const uint8_t *peer_key,
4633 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004634{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004635 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004636 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004637 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004638
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004639 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004640 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01004641 status = psa_get_and_lock_transparent_key_slot_with_policy(
4642 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004643 if( status != PSA_SUCCESS )
4644 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004645 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004646 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004647 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004648 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004649 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004650 else
4651 {
4652 /* If a private key has been added as SECRET, we allow the derived
4653 * key material to be used as a key in PSA Crypto. */
4654 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4655 operation->can_output_key = 1;
4656 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004657
Ronald Cron5c522922020-11-14 16:35:34 +01004658 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004659
Ronald Cron5c522922020-11-14 16:35:34 +01004660 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004661}
4662
Gilles Peskinebe697d82019-05-16 18:00:41 +02004663psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004664 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02004665 const uint8_t *peer_key,
4666 size_t peer_key_length,
4667 uint8_t *output,
4668 size_t output_size,
4669 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02004670{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004671 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004672 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004673 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004674
4675 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4676 {
4677 status = PSA_ERROR_INVALID_ARGUMENT;
4678 goto exit;
4679 }
Ronald Cron5c522922020-11-14 16:35:34 +01004680 status = psa_get_and_lock_transparent_key_slot_with_policy(
4681 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004682 if( status != PSA_SUCCESS )
4683 goto exit;
4684
4685 status = psa_key_agreement_raw_internal( alg, slot,
4686 peer_key, peer_key_length,
4687 output, output_size,
4688 output_length );
4689
4690exit:
4691 if( status != PSA_SUCCESS )
4692 {
4693 /* If an error happens and is not handled properly, the output
4694 * may be used as a key to protect sensitive data. Arrange for such
4695 * a key to be random, which is likely to result in decryption or
4696 * verification errors. This is better than filling the buffer with
4697 * some constant data such as zeros, which would result in the data
4698 * being protected with a reproducible, easily knowable key.
4699 */
4700 psa_generate_random( output, output_size );
4701 *output_length = output_size;
4702 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004703
Ronald Cron5c522922020-11-14 16:35:34 +01004704 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004705
Ronald Cron5c522922020-11-14 16:35:34 +01004706 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004707}
Gilles Peskine86a440b2018-11-12 18:39:40 +01004708
4709
Gilles Peskine30524eb2020-11-13 17:02:26 +01004710
Gilles Peskine86a440b2018-11-12 18:39:40 +01004711/****************************************************************/
4712/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02004713/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02004714
Gilles Peskine30524eb2020-11-13 17:02:26 +01004715/** Initialize the PSA random generator.
4716 */
4717static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
4718{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004719#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4720 memset( rng, 0, sizeof( *rng ) );
4721#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4722
Gilles Peskine30524eb2020-11-13 17:02:26 +01004723 /* Set default configuration if
4724 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4725 if( rng->entropy_init == NULL )
4726 rng->entropy_init = mbedtls_entropy_init;
4727 if( rng->entropy_free == NULL )
4728 rng->entropy_free = mbedtls_entropy_free;
4729
4730 rng->entropy_init( &rng->entropy );
4731#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
4732 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
4733 /* The PSA entropy injection feature depends on using NV seed as an entropy
4734 * source. Add NV seed as an entropy source for PSA entropy injection. */
4735 mbedtls_entropy_add_source( &rng->entropy,
4736 mbedtls_nv_seed_poll, NULL,
4737 MBEDTLS_ENTROPY_BLOCK_SIZE,
4738 MBEDTLS_ENTROPY_SOURCE_STRONG );
4739#endif
4740
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004741 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004742#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01004743}
4744
4745/** Deinitialize the PSA random generator.
4746 */
4747static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
4748{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004749#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4750 memset( rng, 0, sizeof( *rng ) );
4751#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004752 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01004753 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004754#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01004755}
4756
4757/** Seed the PSA random generator.
4758 */
4759static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
4760{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004761#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4762 /* Do nothing: the external RNG seeds itself. */
4763 (void) rng;
4764 return( PSA_SUCCESS );
4765#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01004766 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004767 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
4768 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01004769 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004770#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01004771}
4772
Gilles Peskinee59236f2018-01-27 23:32:46 +01004773psa_status_t psa_generate_random( uint8_t *output,
4774 size_t output_size )
4775{
Gilles Peskinee59236f2018-01-27 23:32:46 +01004776 GUARD_MODULE_INITIALIZED;
4777
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004778#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4779
4780 size_t output_length = 0;
4781 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
4782 output, output_size,
4783 &output_length );
4784 if( status != PSA_SUCCESS )
4785 return( status );
4786 /* Breaking up a request into smaller chunks is currently not supported
4787 * for the extrernal RNG interface. */
4788 if( output_length != output_size )
4789 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
4790 return( PSA_SUCCESS );
4791
4792#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4793
Gilles Peskine71ddab92021-01-04 21:01:07 +01004794 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02004795 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01004796 size_t request_size =
4797 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
4798 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
4799 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01004800 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
4801 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02004802 if( ret != 0 )
4803 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01004804 output_size -= request_size;
4805 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02004806 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01004807 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004808#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004809}
4810
Gilles Peskine8814fc42020-12-14 15:33:44 +01004811/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01004812 *
4813 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
4814 * `psa_generate_random(...)`. The state parameter is ignored since the
4815 * PSA API doesn't support passing an explicit state.
4816 *
4817 * In the non-external case, psa_generate_random() calls an
4818 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
4819 * and semantics as mbedtls_psa_get_random(). As an optimization,
4820 * instead of doing this back-and-forth between the PSA API and the
4821 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
4822 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01004823 */
4824#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4825int mbedtls_psa_get_random( void *p_rng,
4826 unsigned char *output,
4827 size_t output_size )
4828{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01004829 /* This function takes a pointer to the RNG state because that's what
4830 * classic mbedtls functions using an RNG expect. The PSA RNG manages
4831 * its own state internally and doesn't let the caller access that state.
4832 * So we just ignore the state parameter, and in practice we'll pass
4833 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01004834 (void) p_rng;
4835 psa_status_t status = psa_generate_random( output, output_size );
4836 if( status == PSA_SUCCESS )
4837 return( 0 );
4838 else
4839 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
4840}
4841#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4842
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004843#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00004844#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004845
Gilles Peskine7228da22019-07-15 11:06:15 +02004846psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004847 size_t seed_size )
4848{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004849 if( global_data.initialized )
4850 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004851
4852 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4853 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4854 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4855 return( PSA_ERROR_INVALID_ARGUMENT );
4856
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004857 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004858}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01004859#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004860
Ronald Cron3772afe2021-02-08 16:10:05 +01004861/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02004862 *
Ronald Cron3772afe2021-02-08 16:10:05 +01004863 * \param type The key type
4864 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02004865 *
4866 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01004867 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02004868 * \retval #PSA_ERROR_INVALID_ARGUMENT
4869 * The size in bits of the key is not valid.
4870 * \retval #PSA_ERROR_NOT_SUPPORTED
4871 * The type and/or the size in bits of the key or the combination of
4872 * the two is not supported.
4873 */
Ronald Cron3772afe2021-02-08 16:10:05 +01004874static psa_status_t psa_validate_key_type_and_size_for_key_generation(
4875 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004876{
Ronald Cronf3bb7612020-10-02 20:11:59 +02004877 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004878
Gilles Peskinee59236f2018-01-27 23:32:46 +01004879 if( key_type_is_raw_bytes( type ) )
4880 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02004881 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004882 if( status != PSA_SUCCESS )
4883 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02004884 }
4885 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01004886#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02004887 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
4888 {
4889 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4890 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02004891
Ronald Cron01b2aba2020-10-05 09:42:02 +02004892 /* Accept only byte-aligned keys, for the same reasons as
4893 * in psa_import_rsa_key(). */
4894 if( bits % 8 != 0 )
4895 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02004896 }
4897 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01004898#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02004899
Ronald Cron5c4d3862020-12-07 11:07:24 +01004900#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02004901 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
4902 {
Ronald Crond81ab562021-02-16 09:01:16 +01004903 /* To avoid empty block, return successfully here. */
4904 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02004905 }
4906 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01004907#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02004908 {
4909 return( PSA_ERROR_NOT_SUPPORTED );
4910 }
4911
4912 return( PSA_SUCCESS );
4913}
4914
Ronald Cron55ed0592020-10-05 10:30:40 +02004915psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004916 const psa_key_attributes_t *attributes,
4917 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02004918{
4919 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004920 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02004921
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004922 if( ( attributes->domain_parameters == NULL ) &&
4923 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02004924 return( PSA_ERROR_INVALID_ARGUMENT );
4925
Ronald Cron01b2aba2020-10-05 09:42:02 +02004926 if( key_type_is_raw_bytes( type ) )
4927 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004928 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004929 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004930 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02004931
David Brown12ca5032021-02-11 11:02:00 -07004932#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01004933 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004934 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07004935#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004936 }
4937 else
4938
John Durkop07cc04a2020-11-16 22:08:34 -08004939#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004940 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004941 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01004942 return( mbedtls_psa_rsa_generate_key( attributes,
4943 key_buffer,
4944 key_buffer_size,
4945 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004946 }
4947 else
John Durkop07cc04a2020-11-16 22:08:34 -08004948#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004949
John Durkop9814fa22020-11-04 12:28:15 -08004950#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004951 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004952 {
Ronald Cron7023db52020-11-20 18:17:42 +01004953 return( mbedtls_psa_ecp_generate_key( attributes,
4954 key_buffer,
4955 key_buffer_size,
4956 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004957 }
4958 else
John Durkop9814fa22020-11-04 12:28:15 -08004959#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02004960 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02004961 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004962 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02004963 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01004964
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004965 return( PSA_SUCCESS );
4966}
Darryl Green0c6575a2018-11-07 16:05:30 +00004967
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004968psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004969 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004970{
4971 psa_status_t status;
4972 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004973 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02004974 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02004975
Ronald Cron81709fc2020-11-14 12:10:32 +01004976 *key = MBEDTLS_SVC_KEY_ID_INIT;
4977
Gilles Peskine0f84d622019-09-12 19:03:13 +02004978 /* Reject any attempt to create a zero-length key so that we don't
4979 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4980 if( psa_get_key_bits( attributes ) == 0 )
4981 return( PSA_ERROR_INVALID_ARGUMENT );
4982
Ronald Cron81709fc2020-11-14 12:10:32 +01004983 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
4984 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02004985 if( status != PSA_SUCCESS )
4986 goto exit;
4987
Ronald Cron977c2472020-10-13 08:32:21 +02004988 /* In the case of a transparent key or an opaque key stored in local
4989 * storage (thus not in the case of generating a key in a secure element
4990 * or cryptoprocessor with storage), we have to allocate a buffer to
4991 * hold the generated key material. */
4992 if( slot->key.data == NULL )
4993 {
4994 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
4995 PSA_KEY_LOCATION_LOCAL_STORAGE )
4996 {
Ronald Cron3772afe2021-02-08 16:10:05 +01004997 status = psa_validate_key_type_and_size_for_key_generation(
4998 attributes->core.type, attributes->core.bits );
4999 if( status != PSA_SUCCESS )
5000 goto exit;
5001
5002 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
5003 attributes->core.type,
5004 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02005005 }
5006 else
5007 {
5008 status = psa_driver_wrapper_get_key_buffer_size(
5009 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01005010 if( status != PSA_SUCCESS )
5011 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02005012 }
Ronald Cron977c2472020-10-13 08:32:21 +02005013
5014 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
5015 if( status != PSA_SUCCESS )
5016 goto exit;
5017 }
5018
Steven Cooreman2a1664c2020-07-20 15:33:08 +02005019 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02005020 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02005021
Ronald Cron2b56bc82020-10-05 10:02:26 +02005022 if( status != PSA_SUCCESS )
5023 psa_remove_key_data_from_memory( slot );
5024
Gilles Peskine11792082019-08-06 18:36:36 +02005025exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005026 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005027 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005028 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005029 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005030
Darryl Green0c6575a2018-11-07 16:05:30 +00005031 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005032}
5033
Gilles Peskinee59236f2018-01-27 23:32:46 +01005034/****************************************************************/
5035/* Module setup */
5036/****************************************************************/
5037
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005038#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005039psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5040 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5041 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5042{
5043 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5044 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005045 global_data.rng.entropy_init = entropy_init;
5046 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01005047 return( PSA_SUCCESS );
5048}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005049#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005050
Gilles Peskinee59236f2018-01-27 23:32:46 +01005051void mbedtls_psa_crypto_free( void )
5052{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005053 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005054 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5055 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005056 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005057 }
5058 /* Wipe all remaining data, including configuration.
5059 * In particular, this sets all state indicator to the value
5060 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005061 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005062#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005063 /* Unregister all secure element drivers, so that we restart from
5064 * a pristine state. */
5065 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005066#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005067}
5068
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005069#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5070/** Recover a transaction that was interrupted by a power failure.
5071 *
5072 * This function is called during initialization, before psa_crypto_init()
5073 * returns. If this function returns a failure status, the initialization
5074 * fails.
5075 */
5076static psa_status_t psa_crypto_recover_transaction(
5077 const psa_crypto_transaction_t *transaction )
5078{
5079 switch( transaction->unknown.type )
5080 {
5081 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5082 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005083 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005084 * is implemented.
5085 * https://github.com/ARMmbed/mbed-crypto/issues/218
5086 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005087 default:
5088 /* We found an unsupported transaction in the storage.
5089 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01005090 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005091 }
5092}
5093#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5094
Gilles Peskinee59236f2018-01-27 23:32:46 +01005095psa_status_t psa_crypto_init( void )
5096{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005097 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005098
Gilles Peskinec6b69072018-11-20 21:42:52 +01005099 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005100 if( global_data.initialized != 0 )
5101 return( PSA_SUCCESS );
5102
Gilles Peskine30524eb2020-11-13 17:02:26 +01005103 /* Initialize and seed the random generator. */
5104 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005105 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01005106 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005107 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005108 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005109 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005110
Gilles Peskine66fb1262018-12-10 16:29:04 +01005111 status = psa_initialize_key_slots( );
5112 if( status != PSA_SUCCESS )
5113 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005114
Gilles Peskined9348f22019-10-01 15:22:29 +02005115#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5116 status = psa_init_all_se_drivers( );
5117 if( status != PSA_SUCCESS )
5118 goto exit;
5119#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5120
Gilles Peskine4b734222019-07-24 15:56:31 +02005121#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005122 status = psa_crypto_load_transaction( );
5123 if( status == PSA_SUCCESS )
5124 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005125 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5126 if( status != PSA_SUCCESS )
5127 goto exit;
5128 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005129 }
5130 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5131 {
5132 /* There's no transaction to complete. It's all good. */
5133 status = PSA_SUCCESS;
5134 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005135#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005136
Gilles Peskinec6b69072018-11-20 21:42:52 +01005137 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005138 global_data.initialized = 1;
5139
5140exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005141 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005142 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005143 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005144}
5145
5146#endif /* MBEDTLS_PSA_CRYPTO_C */