blob: c510e1b8cd857cd352e8777611890b841f59893c [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
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035#include "psa_crypto_ecp.h"
36#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010037#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020038#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020039#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020040#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010041#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010042/* Include internal declarations that are useful for implementing persistently
43 * stored keys. */
44#include "psa_crypto_storage.h"
45
Gilles Peskineb2b64d32020-12-14 16:43:58 +010046#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010047
Gilles Peskineb46bef22019-07-30 21:32:04 +020048#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010049#include <stdlib.h>
50#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020052#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020059#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000060#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020061#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020064#include "mbedtls/chacha20.h"
65#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/cipher.h"
67#include "mbedtls/ccm.h"
68#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020070#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/error.h"
74#include "mbedtls/gcm.h"
75#include "mbedtls/md2.h"
76#include "mbedtls/md4.h"
77#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010078#include "mbedtls/md.h"
79#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010080#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010082#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000083#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/sha1.h"
87#include "mbedtls/sha256.h"
88#include "mbedtls/sha512.h"
89#include "mbedtls/xtea.h"
90
Gilles Peskine996deb12018-08-01 15:45:45 +020091#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111static int key_type_is_raw_bytes( psa_key_type_t type )
112{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200113 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200114}
115
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100116/* Values for psa_global_data_t::rng_state */
117#define RNG_NOT_INITIALIZED 0
118#define RNG_INITIALIZED 1
119#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120
Gilles Peskine2d277862018-06-18 15:41:12 +0200121typedef struct
122{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100123 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100124 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100125 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126} psa_global_data_t;
127
128static psa_global_data_t global_data;
129
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100130#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
131mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
132 &global_data.rng.drbg;
133#endif
134
itayzafrir0adf0fc2018-09-06 16:24:41 +0300135#define GUARD_MODULE_INITIALIZED \
136 if( global_data.initialized == 0 ) \
137 return( PSA_ERROR_BAD_STATE );
138
Steven Cooreman01164162020-07-20 15:31:37 +0200139psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100140{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100141 /* Mbed TLS error codes can combine a high-level error code and a
142 * low-level error code. The low-level error usually reflects the
143 * root cause better, so dispatch on that preferably. */
144 int low_level_ret = - ( -ret & 0x007f );
145 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100146 {
147 case 0:
148 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100149
150 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
151 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskinea5905292018-02-07 20:59:33 +0100152 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100153
Gilles Peskine9a944802018-06-21 09:35:35 +0200154 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
155 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
156 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
157 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
158 case MBEDTLS_ERR_ASN1_INVALID_DATA:
159 return( PSA_ERROR_INVALID_ARGUMENT );
160 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
161 return( PSA_ERROR_INSUFFICIENT_MEMORY );
162 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
163 return( PSA_ERROR_BUFFER_TOO_SMALL );
164
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000166 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000167#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
169 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100176
177 case MBEDTLS_ERR_CCM_BAD_INPUT:
178 return( PSA_ERROR_INVALID_ARGUMENT );
179 case MBEDTLS_ERR_CCM_AUTH_FAILED:
180 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100181
Gilles Peskine26869f22019-05-06 15:25:00 +0200182 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184
185 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
186 return( PSA_ERROR_BAD_STATE );
187 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
188 return( PSA_ERROR_INVALID_SIGNATURE );
189
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
191 return( PSA_ERROR_NOT_SUPPORTED );
192 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
193 return( PSA_ERROR_INVALID_ARGUMENT );
194 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
195 return( PSA_ERROR_INSUFFICIENT_MEMORY );
196 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
197 return( PSA_ERROR_INVALID_PADDING );
198 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200199 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100200 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
201 return( PSA_ERROR_INVALID_SIGNATURE );
202 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200203 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100204
Gilles Peskine82e57d12020-11-13 21:31:17 +0100205#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
206 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100207 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
208 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100209 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
210 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
211 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
212 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
213 return( PSA_ERROR_NOT_SUPPORTED );
214 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100216#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100217
218 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
219 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220
Gilles Peskinee59236f2018-01-27 23:32:46 +0100221 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
222 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
223 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
224 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100225
226 case MBEDTLS_ERR_GCM_AUTH_FAILED:
227 return( PSA_ERROR_INVALID_SIGNATURE );
228 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200229 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
Gilles Peskine82e57d12020-11-13 21:31:17 +0100231#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
232 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100233 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
234 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100235 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
236 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
237 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
238 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
239 return( PSA_ERROR_NOT_SUPPORTED );
240 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
241 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
242#endif
243
Gilles Peskinea5905292018-02-07 20:59:33 +0100244 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MD_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
251 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100252
Gilles Peskinef76aa772018-10-29 19:24:33 +0100253 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
256 return( PSA_ERROR_INVALID_ARGUMENT );
257 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
260 return( PSA_ERROR_BUFFER_TOO_SMALL );
261 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
268 return( PSA_ERROR_INSUFFICIENT_MEMORY );
269
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
273 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100276 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
278 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
283 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
284 return( PSA_ERROR_NOT_PERMITTED );
285 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_PK_INVALID_ALG:
288 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
289 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
290 return( PSA_ERROR_NOT_SUPPORTED );
291 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
292 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100293
Gilles Peskineff2d2002019-05-06 15:26:23 +0200294 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
297 return( PSA_ERROR_NOT_SUPPORTED );
298
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
300 return( PSA_ERROR_INVALID_ARGUMENT );
301 case MBEDTLS_ERR_RSA_INVALID_PADDING:
302 return( PSA_ERROR_INVALID_PADDING );
303 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
304 return( PSA_ERROR_HARDWARE_FAILURE );
305 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
308 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200309 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
311 return( PSA_ERROR_INVALID_SIGNATURE );
312 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
313 return( PSA_ERROR_BUFFER_TOO_SMALL );
314 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100315 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100316
317 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
318 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100319
itayzafrir5c753392018-05-08 11:18:38 +0300320 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300321 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300322 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
324 return( PSA_ERROR_BUFFER_TOO_SMALL );
325 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
326 return( PSA_ERROR_NOT_SUPPORTED );
327 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
328 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
329 return( PSA_ERROR_INVALID_SIGNATURE );
330 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
331 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100332 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
333 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100334
Janos Follatha13b9052019-11-22 12:48:59 +0000335 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
336 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300337
Gilles Peskinee59236f2018-01-27 23:32:46 +0100338 default:
David Saadab4ecc272019-02-14 13:48:10 +0200339 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100340 }
341}
342
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200343
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200344
345
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346/****************************************************************/
347/* Key management */
348/****************************************************************/
349
Gilles Peskine73167e12019-07-12 23:44:37 +0200350#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
351static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
352{
Gilles Peskine8e338702019-07-30 20:06:31 +0200353 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200354}
355#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
356
John Durkop07cc04a2020-11-16 22:08:34 -0800357/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
358 * current test driver in key_management.c is using this function
359 * when accelerators are used for ECC key pair and public key.
360 * Once that dependency is resolved these guards can be removed.
361 */
John Durkop9814fa22020-11-04 12:28:15 -0800362#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800363 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
364 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
365 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100366mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100367 size_t bits,
368 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200369{
370 switch( curve )
371 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100372 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100373 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100374 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100375 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100376 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100377 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100378 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100379 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100381 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100382 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100383 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100384 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100385 case 528:
386 if( bits_is_sloppy )
387 return( MBEDTLS_ECP_DP_SECP521R1 );
388 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100389 }
390 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100391
Paul Elliott8ff510a2020-06-02 17:19:28 +0100392 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100393 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100394 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100396 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100397 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100398 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100400 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100401 }
402 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100403
Paul Elliott8ff510a2020-06-02 17:19:28 +0100404 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100405 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100406 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100408 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100409 case 256:
410 if( bits_is_sloppy )
411 return( MBEDTLS_ECP_DP_CURVE25519 );
412 break;
413 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100414 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100415 }
416 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100417
Paul Elliott8ff510a2020-06-02 17:19:28 +0100418 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100419 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100420 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100422 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100423 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100427 }
428 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200429 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100430
431 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200432}
John Durkop9814fa22020-11-04 12:28:15 -0800433#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800434 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
435 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
436 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200437
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200438static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
439 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200440{
441 /* Check that the bit size is acceptable for the key type */
442 switch( type )
443 {
444 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200446 case PSA_KEY_TYPE_DERIVE:
447 break;
David Browne04acc22021-01-26 11:39:16 -0700448#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200449 case PSA_KEY_TYPE_AES:
450 if( bits != 128 && bits != 192 && bits != 256 )
451 return( PSA_ERROR_INVALID_ARGUMENT );
452 break;
453#endif
David Browne04acc22021-01-26 11:39:16 -0700454#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200455 case PSA_KEY_TYPE_CAMELLIA:
456 if( bits != 128 && bits != 192 && bits != 256 )
457 return( PSA_ERROR_INVALID_ARGUMENT );
458 break;
459#endif
David Browne04acc22021-01-26 11:39:16 -0700460#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461 case PSA_KEY_TYPE_DES:
462 if( bits != 64 && bits != 128 && bits != 192 )
463 return( PSA_ERROR_INVALID_ARGUMENT );
464 break;
465#endif
David Browne04acc22021-01-26 11:39:16 -0700466#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200467 case PSA_KEY_TYPE_ARC4:
468 if( bits < 8 || bits > 2048 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
David Brown1bfe4d72021-02-16 12:54:35 -0700472#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200473 case PSA_KEY_TYPE_CHACHA20:
474 if( bits != 256 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478 default:
479 return( PSA_ERROR_NOT_SUPPORTED );
480 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200481 if( bits % 8 != 0 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200483
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484 return( PSA_SUCCESS );
485}
486
Gilles Peskineb46bef22019-07-30 21:32:04 +0200487/** Return the size of the key in the given slot, in bits.
488 *
489 * \param[in] slot A key slot.
490 *
491 * \return The key size in bits, read from the metadata in the slot.
492 */
493static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
494{
495 return( slot->attr.bits );
496}
497
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100498/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100499 *
Steven Cooremancd640932021-03-08 12:00:27 +0100500 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
501 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100502 *
503 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100504 * \param[in] key_type The key type of the key to be used with the
505 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100506 *
507 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100508 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100509 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100510 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100511 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100512MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100513 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100514 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100515{
Steven Cooremancd640932021-03-08 12:00:27 +0100516 if( PSA_ALG_IS_HMAC( algorithm ) )
517 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100518 if( key_type == PSA_KEY_TYPE_HMAC )
519 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100520 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100521
522 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100523 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100524 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
525 * key. */
526 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
527 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
528 {
529 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
530 * the block length (larger than 1) for block ciphers. */
531 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
532 return( PSA_SUCCESS );
533 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100534 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100535
536 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100537}
538
Steven Cooreman75b74362020-07-28 14:30:13 +0200539/** Try to allocate a buffer to an empty key slot.
540 *
541 * \param[in,out] slot Key slot to attach buffer to.
542 * \param[in] buffer_length Requested size of the buffer.
543 *
544 * \retval #PSA_SUCCESS
545 * The buffer has been successfully allocated.
546 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
547 * Not enough memory was available for allocation.
548 * \retval #PSA_ERROR_ALREADY_EXISTS
549 * Trying to allocate a buffer to a non-empty key slot.
550 */
551static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
552 size_t buffer_length )
553{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100554 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200555 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200556
Ronald Cronea0f8a62020-11-25 17:52:23 +0100557 slot->key.data = mbedtls_calloc( 1, buffer_length );
558 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200559 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200560
Ronald Cronea0f8a62020-11-25 17:52:23 +0100561 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200562 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200563}
564
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200565psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
566 const uint8_t* data,
567 size_t data_length )
568{
569 psa_status_t status = psa_allocate_buffer_to_slot( slot,
570 data_length );
571 if( status != PSA_SUCCESS )
572 return( status );
573
Ronald Cronea0f8a62020-11-25 17:52:23 +0100574 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200575 return( PSA_SUCCESS );
576}
577
Ronald Crona0fe59f2020-11-29 11:14:53 +0100578psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100579 const psa_key_attributes_t *attributes,
580 const uint8_t *data, size_t data_length,
581 uint8_t *key_buffer, size_t key_buffer_size,
582 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100584 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
585 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200587 /* zero-length keys are never supported. */
588 if( data_length == 0 )
589 return( PSA_ERROR_NOT_SUPPORTED );
590
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100591 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100593 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200594
Steven Cooreman75b74362020-07-28 14:30:13 +0200595 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
596 if( data_length > SIZE_MAX / 8 )
597 return( PSA_ERROR_NOT_SUPPORTED );
598
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200599 /* Enforce a size limit, and in particular ensure that the bit
600 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100601 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200602 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200603
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100604 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200605 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200606 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200607
Ronald Crondd04d422020-11-28 15:14:42 +0100608 /* Copy the key material. */
609 memcpy( key_buffer, data, data_length );
610 *key_buffer_length = data_length;
611 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200612
Steven Cooreman40120f62020-10-29 11:42:22 +0100613 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100615 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200616 {
John Durkop9814fa22020-11-04 12:28:15 -0800617#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
618 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100619 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200620 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100621 return( mbedtls_psa_ecp_import_key( attributes,
622 data, data_length,
623 key_buffer, key_buffer_size,
624 key_buffer_length,
625 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100626 }
John Durkop9814fa22020-11-04 12:28:15 -0800627#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
628 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700629#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
630 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100631 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200632 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100633 return( mbedtls_psa_rsa_import_key( attributes,
634 data, data_length,
635 key_buffer, key_buffer_size,
636 key_buffer_length,
637 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200638 }
John Durkop9814fa22020-11-04 12:28:15 -0800639#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
640 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100641 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100642
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100643 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100644}
645
Gilles Peskinef603c712019-01-19 13:40:11 +0100646/** Calculate the intersection of two algorithm usage policies.
647 *
648 * Return 0 (which allows no operation) on incompatibility.
649 */
650static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100651 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100652 psa_algorithm_t alg1,
653 psa_algorithm_t alg2 )
654{
Gilles Peskine549ea862019-05-22 11:45:59 +0200655 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100656 if( alg1 == alg2 )
657 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100658 /* If the policies are from the same hash-and-sign family, check
659 * if one is a wildcard. If so the other has the specific algorithm. */
660 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
661 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
662 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100663 {
Steven Cooreman03488022021-02-18 12:07:20 +0100664 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
665 return( alg2 );
666 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
667 return( alg1 );
668 }
669 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100670 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100671 * restrictive tag length. */
672 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
673 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
674 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
675 {
676 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
677 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100678 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100679
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100680 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100681 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
682 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100683 {
Steven Cooremancd640932021-03-08 12:00:27 +0100684 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
685 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100686 }
687 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100688 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100689 ( alg1_len <= alg2_len ) )
690 {
691 return( alg2 );
692 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100693 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100694 ( alg2_len <= alg1_len ) )
695 {
696 return( alg1 );
697 }
698 }
699 /* If the policies are from the same MAC family, check whether one
700 * of them is a minimum-MAC-length policy. Calculate the most
701 * restrictive tag length. */
702 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
703 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
704 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
705 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100706 /* Validate the combination of key type and algorithm. Since the base
707 * algorithm of alg1 and alg2 are the same, we only need this once. */
708 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100709 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100710
Steven Cooreman31a876d2021-03-03 20:47:40 +0100711 /* Get the (exact or at-least) output lengths for both sides of the
712 * requested intersection. None of the currently supported algorithms
713 * have an output length dependent on the actual key size, so setting it
714 * to a bogus value of 0 is currently OK.
715 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100716 * Note that for at-least-this-length wildcard algorithms, the output
717 * length is set to the shortest allowed length, which allows us to
718 * calculate the most restrictive tag length for the intersection. */
719 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
720 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100721 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100722
Steven Cooremana1d83222021-02-25 10:20:29 +0100723 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100724 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
725 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100726 {
Steven Cooremancd640932021-03-08 12:00:27 +0100727 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100728 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100729
730 /* If only one is an at-least-this-length policy, the intersection would
731 * be the other (fixed-length) policy as long as said fixed length is
732 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100733 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100734 {
Steven Cooremancd640932021-03-08 12:00:27 +0100735 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100736 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100737 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100738 {
Steven Cooremancd640932021-03-08 12:00:27 +0100739 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100740 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100741
Steven Cooremancd640932021-03-08 12:00:27 +0100742 /* If none of them are wildcards, check whether they define the same tag
743 * length. This is still possible here when one is default-length and
744 * the other specific-length. Ensure to always return the
745 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100746 if( alg1_len == alg2_len )
747 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100748 }
749 /* If the policies are incompatible, allow nothing. */
750 return( 0 );
751}
752
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100753static int psa_key_algorithm_permits( psa_key_type_t key_type,
754 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200755 psa_algorithm_t requested_alg )
756{
Gilles Peskine549ea862019-05-22 11:45:59 +0200757 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200758 if( requested_alg == policy_alg )
759 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100760 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
761 * and requested_alg is the same hash-and-sign family with any hash,
762 * then requested_alg is compliant with policy_alg. */
763 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
764 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200765 {
Steven Cooreman03488022021-02-18 12:07:20 +0100766 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
767 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
768 }
769 /* If policy_alg is a wildcard AEAD algorithm of the same base as
770 * the requested algorithm, check the requested tag length to be
771 * equal-length or longer than the wildcard-specified length. */
772 if( PSA_ALG_IS_AEAD( policy_alg ) &&
773 PSA_ALG_IS_AEAD( requested_alg ) &&
774 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
775 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100776 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100777 {
778 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
779 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
780 }
Steven Cooremancd640932021-03-08 12:00:27 +0100781 /* If policy_alg is a MAC algorithm of the same base as the requested
782 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100783 if( PSA_ALG_IS_MAC( policy_alg ) &&
784 PSA_ALG_IS_MAC( requested_alg ) &&
785 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100786 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100787 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100788 /* Validate the combination of key type and algorithm. Since the policy
789 * and requested algorithms are the same, we only need this once. */
790 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100791 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100792
Steven Cooreman31a876d2021-03-03 20:47:40 +0100793 /* Get both the requested output length for the algorithm which is to be
794 * verified, and the default output length for the base algorithm.
795 * Note that none of the currently supported algorithms have an output
796 * length dependent on actual key size, so setting it to a bogus value
797 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100798 size_t requested_output_length = PSA_MAC_LENGTH(
799 key_type, 0, requested_alg );
800 size_t default_output_length = PSA_MAC_LENGTH(
801 key_type, 0,
802 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100803
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100804 /* If the policy is default-length, only allow an algorithm with
805 * a declared exact-length matching the default. */
806 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100807 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100808
809 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100810 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100811 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
812 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
813 {
814 return( 1 );
815 }
816
Steven Cooremancd640932021-03-08 12:00:27 +0100817 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
818 * check for the requested MAC length to be equal to or longer than the
819 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100820 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
821 {
822 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100823 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100824 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200825 }
Steven Cooremance48e852020-10-05 16:02:45 +0200826 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200827 * a key derivation with that key agreement should also be allowed. This
828 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200829 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
830 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
831 {
832 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
833 policy_alg );
834 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100835 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200836 return( 0 );
837}
838
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100839/** Test whether a policy permits an algorithm.
840 *
841 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100842 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100843 * \note This function requires providing the key type for which the policy is
844 * being validated, since some algorithm policy definitions (e.g. MAC)
845 * have different properties depending on what kind of cipher it is
846 * combined with.
847 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100848 * \retval PSA_SUCCESS When \p alg is a specific algorithm
849 * allowed by the \p policy.
850 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
851 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
852 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100853 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100854static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100855 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100856 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857{
Steven Cooremand788fab2021-02-25 11:29:17 +0100858 /* '0' is not a valid algorithm */
859 if( alg == 0 )
860 return( PSA_ERROR_INVALID_ARGUMENT );
861
Steven Cooreman7e39f052021-02-23 14:18:32 +0100862 /* A requested algorithm cannot be a wildcard. */
863 if( PSA_ALG_IS_WILDCARD( alg ) )
864 return( PSA_ERROR_INVALID_ARGUMENT );
865
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100866 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
867 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100868 return( PSA_SUCCESS );
869 else
870 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100871}
872
Gilles Peskinef603c712019-01-19 13:40:11 +0100873/** Restrict a key policy based on a constraint.
874 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100875 * \note This function requires providing the key type for which the policy is
876 * being restricted, since some algorithm policy definitions (e.g. MAC)
877 * have different properties depending on what kind of cipher it is
878 * combined with.
879 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100880 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100881 * \param[in,out] policy The policy to restrict.
882 * \param[in] constraint The policy constraint to apply.
883 *
884 * \retval #PSA_SUCCESS
885 * \c *policy contains the intersection of the original value of
886 * \c *policy and \c *constraint.
887 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100888 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100889 * \c *policy is unchanged.
890 */
891static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100892 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100893 psa_key_policy_t *policy,
894 const psa_key_policy_t *constraint )
895{
896 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100897 psa_key_policy_algorithm_intersection( key_type, policy->alg,
898 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200899 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100900 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
901 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100902 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
903 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200904 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
905 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100906 policy->usage &= constraint->usage;
907 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200908 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100909 return( PSA_SUCCESS );
910}
911
Ronald Cron5c522922020-11-14 16:35:34 +0100912/** Get the description of a key given its identifier and policy constraints
913 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200914 *
Ronald Cron5c522922020-11-14 16:35:34 +0100915 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100916 * nonzero, the key must allow operations with this algorithm. If \p alg is
917 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100918 *
Ronald Cron5c522922020-11-14 16:35:34 +0100919 * In case of a persistent key, the function loads the description of the key
920 * into a key slot if not already done.
921 *
922 * On success, the returned key slot is locked. It is the responsibility of
923 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200924 */
Ronald Cron5c522922020-11-14 16:35:34 +0100925static psa_status_t psa_get_and_lock_key_slot_with_policy(
926 mbedtls_svc_key_id_t key,
927 psa_key_slot_t **p_slot,
928 psa_key_usage_t usage,
929 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100930{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200931 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
932 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100933
Ronald Cron5c522922020-11-14 16:35:34 +0100934 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100935 if( status != PSA_SUCCESS )
936 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200937 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100938
939 /* Enforce that usage policy for the key slot contains all the flags
940 * required by the usage parameter. There is one exception: public
941 * keys can always be exported, so we treat public key objects as
942 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200943 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100944 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200945
Gilles Peskine8e338702019-07-30 20:06:31 +0200946 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100947 {
948 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200949 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100950 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100951
952 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100953 if( alg != 0 )
954 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100955 status = psa_key_policy_permits( &slot->attr.policy,
956 slot->attr.type,
957 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +0100958 if( status != PSA_SUCCESS )
959 goto error;
960 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100961
Darryl Green06fd18d2018-07-16 11:21:11 +0100962 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200963
964error:
965 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100966 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200967
968 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100969}
Darryl Green940d72c2018-07-13 13:18:51 +0100970
Ronald Cron5c522922020-11-14 16:35:34 +0100971/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200972 *
973 * A transparent key is a key for which the key material is directly
974 * available, as opposed to a key in a secure element.
975 *
Ronald Cron5c522922020-11-14 16:35:34 +0100976 * This is a temporary function to use instead of
977 * psa_get_and_lock_key_slot_with_policy() until secure element support is
978 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200979 *
Ronald Cron5c522922020-11-14 16:35:34 +0100980 * On success, the returned key slot is locked. It is the responsibility of the
981 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200982 */
983#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +0100984static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
985 mbedtls_svc_key_id_t key,
986 psa_key_slot_t **p_slot,
987 psa_key_usage_t usage,
988 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200989{
Ronald Cron5c522922020-11-14 16:35:34 +0100990 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
991 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200992 if( status != PSA_SUCCESS )
993 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200994
Gilles Peskineadad8132019-07-25 11:31:23 +0200995 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200996 {
Ronald Cron5c522922020-11-14 16:35:34 +0100997 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200998 *p_slot = NULL;
999 return( PSA_ERROR_NOT_SUPPORTED );
1000 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001
Gilles Peskine28f8f302019-07-24 13:30:31 +02001002 return( PSA_SUCCESS );
1003}
1004#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1005/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001006#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1007 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001008#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1009
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001010/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001011static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001012{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001013 /* Data pointer will always be either a valid pointer or NULL in an
1014 * initialized slot, so we can just free it. */
1015 if( slot->key.data != NULL )
1016 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1017
1018 mbedtls_free( slot->key.data );
1019 slot->key.data = NULL;
1020 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001021
1022 return( PSA_SUCCESS );
1023}
1024
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001025/** Completely wipe a slot in memory, including its policy.
1026 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001027psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001028{
1029 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001030
1031 /*
1032 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001033 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001034 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1035 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001036 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001037 */
Ronald Cron5c522922020-11-14 16:35:34 +01001038 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001039 {
1040#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001041 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001042#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001043 status = PSA_ERROR_CORRUPTION_DETECTED;
1044 }
1045
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001046 /* Multipart operations may still be using the key. This is safe
1047 * because all multipart operation objects are independent from
1048 * the key slot: if they need to access the key after the setup
1049 * phase, they have a copy of the key. Note that this means that
1050 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001051 /* At this point, key material and other type-specific content has
1052 * been wiped. Clear remaining metadata. We can call memset and not
1053 * zeroize because the metadata is not particularly sensitive. */
1054 memset( slot, 0, sizeof( *slot ) );
1055 return( status );
1056}
1057
Ronald Croncf56a0a2020-08-04 09:51:30 +02001058psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001059{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001060 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001061 psa_status_t status; /* status of the last operation */
1062 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001063#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1064 psa_se_drv_table_entry_t *driver;
1065#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066
Ronald Croncf56a0a2020-08-04 09:51:30 +02001067 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001068 return( PSA_SUCCESS );
1069
Ronald Cronf2911112020-10-29 17:51:10 +01001070 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001071 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001072 * key, this will load the key description from persistent memory if not
1073 * done yet. We cannot avoid this loading as without it we don't know if
1074 * the key is operated by an SE or not and this information is needed by
1075 * the current implementation.
1076 */
Ronald Cron5c522922020-11-14 16:35:34 +01001077 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001078 if( status != PSA_SUCCESS )
1079 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001080
Ronald Cronf2911112020-10-29 17:51:10 +01001081 /*
1082 * If the key slot containing the key description is under access by the
1083 * library (apart from the present access), the key cannot be destroyed
1084 * yet. For the time being, just return in error. Eventually (to be
1085 * implemented), the key should be destroyed when all accesses have
1086 * stopped.
1087 */
Ronald Cron5c522922020-11-14 16:35:34 +01001088 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001089 {
Ronald Cron5c522922020-11-14 16:35:34 +01001090 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001091 return( PSA_ERROR_GENERIC_ERROR );
1092 }
1093
Gilles Peskine354f7672019-07-12 23:46:38 +02001094#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001095 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001096 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001097 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001098 /* For a key in a secure element, we need to do three things:
1099 * remove the key file in internal storage, destroy the
1100 * key inside the secure element, and update the driver's
1101 * persistent data. Start a transaction that will encompass these
1102 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001103 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001104 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001105 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001106 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001107 status = psa_crypto_save_transaction( );
1108 if( status != PSA_SUCCESS )
1109 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001110 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001111 /* We should still try to destroy the key in the secure
1112 * element and the key metadata in storage. This is especially
1113 * important if the error is that the storage is full.
1114 * But how to do it exactly without risking an inconsistent
1115 * state after a reset?
1116 * https://github.com/ARMmbed/mbed-crypto/issues/215
1117 */
1118 overall_status = status;
1119 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001120 }
1121
Ronald Cronea0f8a62020-11-25 17:52:23 +01001122 status = psa_destroy_se_key( driver,
1123 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001124 if( overall_status == PSA_SUCCESS )
1125 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001126 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001127#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1128
Darryl Greend49a4992018-06-18 17:27:26 +01001129#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001130 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001131 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001132 status = psa_destroy_persistent_key( slot->attr.id );
1133 if( overall_status == PSA_SUCCESS )
1134 overall_status = status;
1135
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001136 /* TODO: other slots may have a copy of the same key. We should
1137 * invalidate them.
1138 * https://github.com/ARMmbed/mbed-crypto/issues/214
1139 */
Darryl Greend49a4992018-06-18 17:27:26 +01001140 }
1141#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001142
Gilles Peskinefc762652019-07-22 19:30:34 +02001143#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1144 if( driver != NULL )
1145 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001146 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001147 if( overall_status == PSA_SUCCESS )
1148 overall_status = status;
1149 status = psa_crypto_stop_transaction( );
1150 if( overall_status == PSA_SUCCESS )
1151 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001152 }
1153#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1154
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001155#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1156exit:
1157#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001158 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001159 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1160 if( overall_status == PSA_SUCCESS )
1161 overall_status = status;
1162 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001163}
1164
John Durkop07cc04a2020-11-16 22:08:34 -08001165#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001166 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001167static psa_status_t psa_get_rsa_public_exponent(
1168 const mbedtls_rsa_context *rsa,
1169 psa_key_attributes_t *attributes )
1170{
1171 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001173 uint8_t *buffer = NULL;
1174 size_t buflen;
1175 mbedtls_mpi_init( &mpi );
1176
1177 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1178 if( ret != 0 )
1179 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001180 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1181 {
1182 /* It's the default value, which is reported as an empty string,
1183 * so there's nothing to do. */
1184 goto exit;
1185 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001186
1187 buflen = mbedtls_mpi_size( &mpi );
1188 buffer = mbedtls_calloc( 1, buflen );
1189 if( buffer == NULL )
1190 {
1191 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1192 goto exit;
1193 }
1194 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1195 if( ret != 0 )
1196 goto exit;
1197 attributes->domain_parameters = buffer;
1198 attributes->domain_parameters_size = buflen;
1199
1200exit:
1201 mbedtls_mpi_free( &mpi );
1202 if( ret != 0 )
1203 mbedtls_free( buffer );
1204 return( mbedtls_to_psa_error( ret ) );
1205}
John Durkop07cc04a2020-11-16 22:08:34 -08001206#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001207 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001208
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001209/** Retrieve all the publicly-accessible attributes of a key.
1210 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001211psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001212 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001213{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001215 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001216 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001217
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001218 psa_reset_key_attributes( attributes );
1219
Ronald Cron5c522922020-11-14 16:35:34 +01001220 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001221 if( status != PSA_SUCCESS )
1222 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001223
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001224 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001225 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1226 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1227
1228#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1229 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001230 psa_set_key_slot_number( attributes,
1231 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001232#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001233
Gilles Peskine8e338702019-07-30 20:06:31 +02001234 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001235 {
John Durkop07cc04a2020-11-16 22:08:34 -08001236#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001237 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001238 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001239 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001240#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001241 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001242 * is not yet implemented.
1243 * https://github.com/ARMmbed/mbed-crypto/issues/216
1244 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001245 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001246 break;
1247#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001248 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001249 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001250
Ronald Cron90857082020-11-25 14:58:33 +01001251 status = mbedtls_psa_rsa_load_representation(
1252 slot->attr.type,
1253 slot->key.data,
1254 slot->key.bytes,
1255 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001256 if( status != PSA_SUCCESS )
1257 break;
1258
Steven Cooremana2371e52020-07-28 14:30:39 +02001259 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001260 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001261 mbedtls_rsa_free( rsa );
1262 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001263 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001264 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001265#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001266 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001267 default:
1268 /* Nothing else to do. */
1269 break;
1270 }
1271
1272 if( status != PSA_SUCCESS )
1273 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001274
Ronald Cron5c522922020-11-14 16:35:34 +01001275 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001276
Ronald Cron5c522922020-11-14 16:35:34 +01001277 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001278}
1279
Gilles Peskinec8000c02019-08-02 20:15:51 +02001280#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1281psa_status_t psa_get_key_slot_number(
1282 const psa_key_attributes_t *attributes,
1283 psa_key_slot_number_t *slot_number )
1284{
1285 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1286 {
1287 *slot_number = attributes->slot_number;
1288 return( PSA_SUCCESS );
1289 }
1290 else
1291 return( PSA_ERROR_INVALID_ARGUMENT );
1292}
1293#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1294
Ronald Crond18b5f82020-11-25 16:46:05 +01001295static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1296 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001297 uint8_t *data,
1298 size_t data_size,
1299 size_t *data_length )
1300{
Ronald Crond18b5f82020-11-25 16:46:05 +01001301 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001302 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001303 memcpy( data, key_buffer, key_buffer_size );
1304 memset( data + key_buffer_size, 0,
1305 data_size - key_buffer_size );
1306 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001307 return( PSA_SUCCESS );
1308}
1309
Ronald Cron7285cda2020-11-26 14:46:37 +01001310psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001311 const psa_key_attributes_t *attributes,
1312 const uint8_t *key_buffer, size_t key_buffer_size,
1313 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001314{
Ronald Crond18b5f82020-11-25 16:46:05 +01001315 psa_key_type_t type = attributes->core.type;
1316
Ronald Crond18b5f82020-11-25 16:46:05 +01001317 if( key_type_is_raw_bytes( type ) ||
1318 PSA_KEY_TYPE_IS_RSA( type ) ||
1319 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001320 {
1321 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001322 key_buffer, key_buffer_size,
1323 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001324 }
1325 else
1326 {
1327 /* This shouldn't happen in the reference implementation, but
1328 it is valid for a special-purpose implementation to omit
1329 support for exporting certain key types. */
1330 return( PSA_ERROR_NOT_SUPPORTED );
1331 }
1332}
1333
1334psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1335 uint8_t *data,
1336 size_t data_size,
1337 size_t *data_length )
1338{
1339 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1340 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1341 psa_key_slot_t *slot;
1342
Ronald Crone907e552021-01-18 13:22:38 +01001343 /* Reject a zero-length output buffer now, since this can never be a
1344 * valid key representation. This way we know that data must be a valid
1345 * pointer and we can do things like memset(data, ..., data_size). */
1346 if( data_size == 0 )
1347 return( PSA_ERROR_BUFFER_TOO_SMALL );
1348
Ronald Cron9486f9d2020-11-26 13:36:23 +01001349 /* Set the key to empty now, so that even when there are errors, we always
1350 * set data_length to a value between 0 and data_size. On error, setting
1351 * the key to empty is a good choice because an empty key representation is
1352 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001353 *data_length = 0;
1354
Ronald Cron9486f9d2020-11-26 13:36:23 +01001355 /* Export requires the EXPORT flag. There is an exception for public keys,
1356 * which don't require any flag, but
1357 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1358 */
1359 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1360 PSA_KEY_USAGE_EXPORT, 0 );
1361 if( status != PSA_SUCCESS )
1362 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001363
Ronald Crond18b5f82020-11-25 16:46:05 +01001364 psa_key_attributes_t attributes = {
1365 .core = slot->attr
1366 };
Ronald Cron67227982020-11-26 15:16:05 +01001367 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001368 slot->key.data, slot->key.bytes,
1369 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001370
Ronald Cron9486f9d2020-11-26 13:36:23 +01001371 unlock_status = psa_unlock_key_slot( slot );
1372
1373 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1374}
1375
Ronald Cron7285cda2020-11-26 14:46:37 +01001376psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001377 const psa_key_attributes_t *attributes,
1378 const uint8_t *key_buffer,
1379 size_t key_buffer_size,
1380 uint8_t *data,
1381 size_t data_size,
1382 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001383{
Ronald Crond18b5f82020-11-25 16:46:05 +01001384 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001385
Ronald Crond18b5f82020-11-25 16:46:05 +01001386 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001387 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001388 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001389 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001390 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001391 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001392 key_buffer, key_buffer_size,
1393 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001394 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001395
Ronald Crond18b5f82020-11-25 16:46:05 +01001396 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001397 {
John Durkop0e005192020-10-31 22:06:54 -07001398#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1399 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001400 return( mbedtls_psa_rsa_export_public_key( attributes,
1401 key_buffer,
1402 key_buffer_size,
1403 data,
1404 data_size,
1405 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001406#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001407 /* We don't know how to convert a private RSA key to public. */
1408 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001409#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1410 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001411 }
1412 else
Moran Pekera998bc62018-04-16 18:16:20 +03001413 {
John Durkop6ba40d12020-11-10 08:50:04 -08001414#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1415 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001416 return( mbedtls_psa_ecp_export_public_key( attributes,
1417 key_buffer,
1418 key_buffer_size,
1419 data,
1420 data_size,
1421 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001422#else
1423 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001424 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001425#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1426 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001427 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001428 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001429 else
1430 {
1431 /* This shouldn't happen in the reference implementation, but
1432 it is valid for a special-purpose implementation to omit
1433 support for exporting certain key types. */
1434 return( PSA_ERROR_NOT_SUPPORTED );
1435 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001436}
Ronald Crond18b5f82020-11-25 16:46:05 +01001437
Ronald Croncf56a0a2020-08-04 09:51:30 +02001438psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001439 uint8_t *data,
1440 size_t data_size,
1441 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001442{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001443 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001444 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001445 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001446
Ronald Crone907e552021-01-18 13:22:38 +01001447 /* Reject a zero-length output buffer now, since this can never be a
1448 * valid key representation. This way we know that data must be a valid
1449 * pointer and we can do things like memset(data, ..., data_size). */
1450 if( data_size == 0 )
1451 return( PSA_ERROR_BUFFER_TOO_SMALL );
1452
Darryl Greendd8fb772018-11-07 16:00:44 +00001453 /* Set the key to empty now, so that even when there are errors, we always
1454 * set data_length to a value between 0 and data_size. On error, setting
1455 * the key to empty is a good choice because an empty key representation is
1456 * unlikely to be accepted anywhere. */
1457 *data_length = 0;
1458
1459 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001460 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001461 if( status != PSA_SUCCESS )
1462 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001463
Ronald Cron9486f9d2020-11-26 13:36:23 +01001464 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1465 {
1466 status = PSA_ERROR_INVALID_ARGUMENT;
1467 goto exit;
1468 }
1469
Ronald Crond18b5f82020-11-25 16:46:05 +01001470 psa_key_attributes_t attributes = {
1471 .core = slot->attr
1472 };
Ronald Cron67227982020-11-26 15:16:05 +01001473 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001474 &attributes, slot->key.data, slot->key.bytes,
1475 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001476
1477exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001478 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001479
Ronald Cron5c522922020-11-14 16:35:34 +01001480 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001481}
1482
Gilles Peskine91e8c332019-08-02 19:19:39 +02001483#if defined(static_assert)
1484static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1485 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001486static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001487 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001488static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001489 "One or more key attribute flag is listed as both internal-only and external-only" );
1490#endif
1491
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001492/** Validate that a key policy is internally well-formed.
1493 *
1494 * This function only rejects invalid policies. It does not validate the
1495 * consistency of the policy with respect to other attributes of the key
1496 * such as the key type.
1497 */
1498static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001499{
1500 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001501 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001502 PSA_KEY_USAGE_ENCRYPT |
1503 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001504 PSA_KEY_USAGE_SIGN_HASH |
1505 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001506 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1507 return( PSA_ERROR_INVALID_ARGUMENT );
1508
Gilles Peskine4747d192019-04-17 15:05:45 +02001509 return( PSA_SUCCESS );
1510}
1511
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001512/** Validate the internal consistency of key attributes.
1513 *
1514 * This function only rejects invalid attribute values. If does not
1515 * validate the consistency of the attributes with any key data that may
1516 * be involved in the creation of the key.
1517 *
1518 * Call this function early in the key creation process.
1519 *
1520 * \param[in] attributes Key attributes for the new key.
1521 * \param[out] p_drv On any return, the driver for the key, if any.
1522 * NULL for a transparent key.
1523 *
1524 */
1525static psa_status_t psa_validate_key_attributes(
1526 const psa_key_attributes_t *attributes,
1527 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001528{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001529 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001530 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001531 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001532
Ronald Cron54b90082020-10-29 15:26:43 +01001533 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001534 if( status != PSA_SUCCESS )
1535 return( status );
1536
Ronald Crond2ed4812020-07-17 16:11:30 +02001537 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001538 if( status != PSA_SUCCESS )
1539 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001540
Ronald Cron65f38a32020-10-23 17:11:13 +02001541 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1542 {
1543 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1544 return( PSA_ERROR_INVALID_ARGUMENT );
1545 }
1546 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001547 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001548 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001549 if( status != PSA_SUCCESS )
1550 return( status );
1551 }
1552
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001553 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001554 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001555 return( status );
1556
1557 /* Refuse to create overly large keys.
1558 * Note that this doesn't trigger on import if the attributes don't
1559 * explicitly specify a size (so psa_get_key_bits returns 0), so
1560 * psa_import_key() needs its own checks. */
1561 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1562 return( PSA_ERROR_NOT_SUPPORTED );
1563
Gilles Peskine91e8c332019-08-02 19:19:39 +02001564 /* Reject invalid flags. These should not be reachable through the API. */
1565 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1566 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1567 return( PSA_ERROR_INVALID_ARGUMENT );
1568
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001569 return( PSA_SUCCESS );
1570}
1571
Gilles Peskine4747d192019-04-17 15:05:45 +02001572/** Prepare a key slot to receive key material.
1573 *
1574 * This function allocates a key slot and sets its metadata.
1575 *
1576 * If this function fails, call psa_fail_key_creation().
1577 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001578 * This function is intended to be used as follows:
1579 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001580 * it with the specified attributes, and in case of a volatile key assign it
1581 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001582 * -# Populate the slot with the key material.
1583 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1584 * In case of failure at any step, stop the sequence and call
1585 * psa_fail_key_creation().
1586 *
Ronald Cron5c522922020-11-14 16:35:34 +01001587 * On success, the key slot is locked. It is the responsibility of the caller
1588 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001589 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001590 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001591 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001592 * \param[out] p_slot On success, a pointer to the prepared slot.
1593 * \param[out] p_drv On any return, the driver for the key, if any.
1594 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001595 *
1596 * \retval #PSA_SUCCESS
1597 * The key slot is ready to receive key material.
1598 * \return If this function fails, the key slot is an invalid state.
1599 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001600 */
1601static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001602 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001603 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001604 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001605 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001606{
1607 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001608 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001609 psa_key_slot_t *slot;
1610
Gilles Peskinedf179142019-07-15 22:02:14 +02001611 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001612 *p_drv = NULL;
1613
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001614 status = psa_validate_key_attributes( attributes, p_drv );
1615 if( status != PSA_SUCCESS )
1616 return( status );
1617
Ronald Cronc4d1b512020-07-31 11:26:37 +02001618 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001619 if( status != PSA_SUCCESS )
1620 return( status );
1621 slot = *p_slot;
1622
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001623 /* We're storing the declared bit-size of the key. It's up to each
1624 * creation mechanism to verify that this information is correct.
1625 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001626 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001627 * is optional (import, copy). In case of a volatile key, assign it the
1628 * volatile key identifier associated to the slot returned to contain its
1629 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001630
1631 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001632 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1633 {
1634#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1635 slot->attr.id = volatile_key_id;
1636#else
1637 slot->attr.id.key_id = volatile_key_id;
1638#endif
1639 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001640
Gilles Peskine91e8c332019-08-02 19:19:39 +02001641 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001642 * external-only flags, query `attributes`. Thanks to the check
1643 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001644 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001645 * may have set. */
1646 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001647
Gilles Peskinecbaff462019-07-12 23:46:04 +02001648#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001649 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001650 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001651 * create the key file in internal storage, create the
1652 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001653 * persistent data. This is done by starting a transaction that will
1654 * encompass these three actions.
1655 * For registering a volatile key, we just need to find an appropriate
1656 * slot number inside the SE. Since the key is designated volatile, creating
1657 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001658 /* The first thing to do is to find a slot number for the new key.
1659 * We save the slot number in persistent storage as part of the
1660 * transaction data. It will be needed to recover if the power
1661 * fails during the key creation process, to clean up on the secure
1662 * element side after restarting. Obtaining a slot number from the
1663 * secure element driver updates its persistent state, but we do not yet
1664 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001665 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001666 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001667 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001668 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001669 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001670 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001671 if( status != PSA_SUCCESS )
1672 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001673
1674 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001675 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001676 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1677 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001678 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001679 psa_crypto_transaction.key.id = slot->attr.id;
1680 status = psa_crypto_save_transaction( );
1681 if( status != PSA_SUCCESS )
1682 {
1683 (void) psa_crypto_stop_transaction( );
1684 return( status );
1685 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001686 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001687
1688 status = psa_copy_key_material_into_slot(
1689 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001690 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001691
1692 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1693 {
1694 /* Key registration only makes sense with a secure element. */
1695 return( PSA_ERROR_INVALID_ARGUMENT );
1696 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001697#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1698
Ronald Cronc4d1b512020-07-31 11:26:37 +02001699 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001700}
Gilles Peskine4747d192019-04-17 15:05:45 +02001701
1702/** Finalize the creation of a key once its key material has been set.
1703 *
1704 * This entails writing the key to persistent storage.
1705 *
1706 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001707 * See the documentation of psa_start_key_creation() for the intended use
1708 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001709 *
Ronald Cron5c522922020-11-14 16:35:34 +01001710 * If the finalization succeeds, the function unlocks the key slot (it was
1711 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1712 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001713 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001714 * \param[in,out] slot Pointer to the slot with key material.
1715 * \param[in] driver The secure element driver for the key,
1716 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001717 * \param[out] key On success, identifier of the key. Note that the
1718 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001719 *
1720 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001721 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001722 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1723 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1724 * \retval #PSA_ERROR_ALREADY_EXISTS
1725 * \retval #PSA_ERROR_DATA_INVALID
1726 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001727 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001728 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001729 * \return If this function fails, the key slot is an invalid state.
1730 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001731 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001732static psa_status_t psa_finish_key_creation(
1733 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001734 psa_se_drv_table_entry_t *driver,
1735 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001736{
1737 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001738 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001739 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001740
1741#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001742 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001743 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001744#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1745 if( driver != NULL )
1746 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001747 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001748 psa_key_slot_number_t slot_number =
1749 psa_key_slot_get_slot_number( slot ) ;
1750
Gilles Peskineb46bef22019-07-30 21:32:04 +02001751#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001752 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001753 sizeof( data.slot_number ),
1754 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001755#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001756 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001757 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001758 (uint8_t*) &data,
1759 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001760 }
1761 else
1762#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1763 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001764 /* Key material is saved in export representation in the slot, so
1765 * just pass the slot buffer for storage. */
1766 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001767 slot->key.data,
1768 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001769 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001771#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1772
Gilles Peskinecbaff462019-07-12 23:46:04 +02001773#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001774 /* Finish the transaction for a key creation. This does not
1775 * happen when registering an existing key. Detect this case
1776 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001777 * creation of a persistent key in a secure element requires a transaction,
1778 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001779 if( driver != NULL &&
1780 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001781 {
1782 status = psa_save_se_persistent_data( driver );
1783 if( status != PSA_SUCCESS )
1784 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001785 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001786 return( status );
1787 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001788 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001789 }
1790#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1791
Ronald Cron50972942020-11-14 11:28:25 +01001792 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001793 {
1794 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001795 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001796 if( status != PSA_SUCCESS )
1797 *key = MBEDTLS_SVC_KEY_ID_INIT;
1798 }
Ronald Cron50972942020-11-14 11:28:25 +01001799
Gilles Peskine4747d192019-04-17 15:05:45 +02001800 return( status );
1801}
1802
1803/** Abort the creation of a key.
1804 *
1805 * You may call this function after calling psa_start_key_creation(),
1806 * or after psa_finish_key_creation() fails. In other circumstances, this
1807 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001808 * See the documentation of psa_start_key_creation() for the intended use
1809 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001810 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001811 * \param[in,out] slot Pointer to the slot with key material.
1812 * \param[in] driver The secure element driver for the key,
1813 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001814 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001815static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001816 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001817{
Gilles Peskine011e4282019-06-26 18:34:38 +02001818 (void) driver;
1819
Gilles Peskine4747d192019-04-17 15:05:45 +02001820 if( slot == NULL )
1821 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001822
1823#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001824 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001825 * element, and the failure happened later (when saving metadata
1826 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001827 * element.
1828 * https://github.com/ARMmbed/mbed-crypto/issues/217
1829 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001830
Gilles Peskined7729582019-08-05 15:55:54 +02001831 /* Abort the ongoing transaction if any (there may not be one if
1832 * the creation process failed before starting one, or if the
1833 * key creation is a registration of a key in a secure element).
1834 * Earlier functions must already have done what it takes to undo any
1835 * partial creation. All that's left is to update the transaction data
1836 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001837 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001838#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1839
Gilles Peskine4747d192019-04-17 15:05:45 +02001840 psa_wipe_key_slot( slot );
1841}
1842
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001843/** Validate optional attributes during key creation.
1844 *
1845 * Some key attributes are optional during key creation. If they are
1846 * specified in the attributes structure, check that they are consistent
1847 * with the data in the slot.
1848 *
1849 * This function should be called near the end of key creation, after
1850 * the slot in memory is fully populated but before saving persistent data.
1851 */
1852static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001853 const psa_key_slot_t *slot,
1854 const psa_key_attributes_t *attributes )
1855{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001856 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001857 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001858 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001859 return( PSA_ERROR_INVALID_ARGUMENT );
1860 }
1861
1862 if( attributes->domain_parameters_size != 0 )
1863 {
John Durkop0e005192020-10-31 22:06:54 -07001864#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1865 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001866 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001867 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001868 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001869 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001871
Ronald Cron90857082020-11-25 14:58:33 +01001872 psa_status_t status = mbedtls_psa_rsa_load_representation(
1873 slot->attr.type,
1874 slot->key.data,
1875 slot->key.bytes,
1876 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001877 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001878 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001879
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001880 mbedtls_mpi_init( &actual );
1881 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001882 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001883 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001884 mbedtls_rsa_free( rsa );
1885 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001886 if( ret != 0 )
1887 goto rsa_exit;
1888 ret = mbedtls_mpi_read_binary( &required,
1889 attributes->domain_parameters,
1890 attributes->domain_parameters_size );
1891 if( ret != 0 )
1892 goto rsa_exit;
1893 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1894 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1895 rsa_exit:
1896 mbedtls_mpi_free( &actual );
1897 mbedtls_mpi_free( &required );
1898 if( ret != 0)
1899 return( mbedtls_to_psa_error( ret ) );
1900 }
1901 else
John Durkop9814fa22020-11-04 12:28:15 -08001902#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1903 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001904 {
1905 return( PSA_ERROR_INVALID_ARGUMENT );
1906 }
1907 }
1908
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001909 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001910 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001911 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001912 return( PSA_ERROR_INVALID_ARGUMENT );
1913 }
1914
1915 return( PSA_SUCCESS );
1916}
1917
Gilles Peskine4747d192019-04-17 15:05:45 +02001918psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001919 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001920 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001921 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001922{
1923 psa_status_t status;
1924 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001925 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001926 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001927
Ronald Cron81709fc2020-11-14 12:10:32 +01001928 *key = MBEDTLS_SVC_KEY_ID_INIT;
1929
Gilles Peskine0f84d622019-09-12 19:03:13 +02001930 /* Reject zero-length symmetric keys (including raw data key objects).
1931 * This also rejects any key which might be encoded as an empty string,
1932 * which is never valid. */
1933 if( data_length == 0 )
1934 return( PSA_ERROR_INVALID_ARGUMENT );
1935
Gilles Peskinedf179142019-07-15 22:02:14 +02001936 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001937 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001938 if( status != PSA_SUCCESS )
1939 goto exit;
1940
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001941 /* In the case of a transparent key or an opaque key stored in local
1942 * storage (thus not in the case of generating a key in a secure element
1943 * or cryptoprocessor with storage), we have to allocate a buffer to
1944 * hold the generated key material. */
1945 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001946 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001947 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001948 if( status != PSA_SUCCESS )
1949 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001950 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001951
1952 bits = slot->attr.bits;
1953 status = psa_driver_wrapper_import_key( attributes,
1954 data, data_length,
1955 slot->key.data,
1956 slot->key.bytes,
1957 &slot->key.bytes, &bits );
1958 if( status != PSA_SUCCESS )
1959 goto exit;
1960
1961 if( slot->attr.bits == 0 )
1962 slot->attr.bits = (psa_key_bits_t) bits;
1963 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001964 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001965 status = PSA_ERROR_INVALID_ARGUMENT;
1966 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001967 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001968
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001969 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001970 if( status != PSA_SUCCESS )
1971 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001972
Ronald Cron81709fc2020-11-14 12:10:32 +01001973 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001974exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001975 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001976 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001977
Gilles Peskine4747d192019-04-17 15:05:45 +02001978 return( status );
1979}
1980
Gilles Peskined7729582019-08-05 15:55:54 +02001981#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1982psa_status_t mbedtls_psa_register_se_key(
1983 const psa_key_attributes_t *attributes )
1984{
1985 psa_status_t status;
1986 psa_key_slot_t *slot = NULL;
1987 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001988 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001989
1990 /* Leaving attributes unspecified is not currently supported.
1991 * It could make sense to query the key type and size from the
1992 * secure element, but not all secure elements support this
1993 * and the driver HAL doesn't currently support it. */
1994 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1995 return( PSA_ERROR_NOT_SUPPORTED );
1996 if( psa_get_key_bits( attributes ) == 0 )
1997 return( PSA_ERROR_NOT_SUPPORTED );
1998
1999 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002000 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002001 if( status != PSA_SUCCESS )
2002 goto exit;
2003
Ronald Cron81709fc2020-11-14 12:10:32 +01002004 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002005
2006exit:
2007 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002008 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002009
Gilles Peskined7729582019-08-05 15:55:54 +02002010 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002011 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002012 return( status );
2013}
2014#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2015
Gilles Peskinef603c712019-01-19 13:40:11 +01002016static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002017 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002018{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002019 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002020 source->key.data,
2021 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002022 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002023 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002024
Steven Cooreman398aee52020-10-13 14:35:45 +02002025 target->attr.type = source->attr.type;
2026 target->attr.bits = source->attr.bits;
2027
2028 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002029}
2030
Ronald Croncf56a0a2020-08-04 09:51:30 +02002031psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002032 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002033 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002034{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002035 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002036 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002037 psa_key_slot_t *source_slot = NULL;
2038 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002039 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002040 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002041
Ronald Cron81709fc2020-11-14 12:10:32 +01002042 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2043
Ronald Cron5c522922020-11-14 16:35:34 +01002044 status = psa_get_and_lock_transparent_key_slot_with_policy(
2045 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002046 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002047 goto exit;
2048
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002049 status = psa_validate_optional_attributes( source_slot,
2050 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002051 if( status != PSA_SUCCESS )
2052 goto exit;
2053
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002054 status = psa_restrict_key_policy( source_slot->attr.type,
2055 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002056 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002057 if( status != PSA_SUCCESS )
2058 goto exit;
2059
Ronald Cron81709fc2020-11-14 12:10:32 +01002060 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2061 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002062 if( status != PSA_SUCCESS )
2063 goto exit;
2064
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002065#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2066 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002067 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002068 /* Copying to a secure element is not implemented yet. */
2069 status = PSA_ERROR_NOT_SUPPORTED;
2070 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002071 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002072#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002073
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002074 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002075 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002076 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002077
Ronald Cron81709fc2020-11-14 12:10:32 +01002078 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002079exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002080 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002081 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002082
Ronald Cron5c522922020-11-14 16:35:34 +01002083 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002084
Ronald Cron5c522922020-11-14 16:35:34 +01002085 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002086}
2087
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002088
2089
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002090/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002091/* Message digests */
2092/****************************************************************/
2093
John Durkop07cc04a2020-11-16 22:08:34 -08002094#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002095 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2096 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002097 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Ronald Cron36f641b2021-02-16 17:20:43 +01002098const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002099{
2100 switch( alg )
2101 {
John Durkopee4e6602020-11-27 08:48:46 -08002102#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002103 case PSA_ALG_MD2:
2104 return( &mbedtls_md2_info );
2105#endif
John Durkopee4e6602020-11-27 08:48:46 -08002106#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002107 case PSA_ALG_MD4:
2108 return( &mbedtls_md4_info );
2109#endif
John Durkopee4e6602020-11-27 08:48:46 -08002110#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002111 case PSA_ALG_MD5:
2112 return( &mbedtls_md5_info );
2113#endif
John Durkopee4e6602020-11-27 08:48:46 -08002114#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002115 case PSA_ALG_RIPEMD160:
2116 return( &mbedtls_ripemd160_info );
2117#endif
John Durkopee4e6602020-11-27 08:48:46 -08002118#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002119 case PSA_ALG_SHA_1:
2120 return( &mbedtls_sha1_info );
2121#endif
John Durkopee4e6602020-11-27 08:48:46 -08002122#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002123 case PSA_ALG_SHA_224:
2124 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002125#endif
2126#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002127 case PSA_ALG_SHA_256:
2128 return( &mbedtls_sha256_info );
2129#endif
John Durkopee4e6602020-11-27 08:48:46 -08002130#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002131 case PSA_ALG_SHA_384:
2132 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002133#endif
John Durkopee4e6602020-11-27 08:48:46 -08002134#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002135 case PSA_ALG_SHA_512:
2136 return( &mbedtls_sha512_info );
2137#endif
2138 default:
2139 return( NULL );
2140 }
2141}
John Durkop07cc04a2020-11-16 22:08:34 -08002142#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002143 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2144 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2145 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002146
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002147psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2148{
2149 switch( operation->alg )
2150 {
Gilles Peskine81736312018-06-26 15:04:31 +02002151 case 0:
2152 /* The object has (apparently) been initialized but it is not
2153 * in use. It's ok to call abort on such an object, and there's
2154 * nothing to do. */
2155 break;
John Durkopee4e6602020-11-27 08:48:46 -08002156#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002157 case PSA_ALG_MD2:
2158 mbedtls_md2_free( &operation->ctx.md2 );
2159 break;
2160#endif
John Durkopee4e6602020-11-27 08:48:46 -08002161#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002162 case PSA_ALG_MD4:
2163 mbedtls_md4_free( &operation->ctx.md4 );
2164 break;
2165#endif
John Durkopee4e6602020-11-27 08:48:46 -08002166#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002167 case PSA_ALG_MD5:
2168 mbedtls_md5_free( &operation->ctx.md5 );
2169 break;
2170#endif
John Durkopee4e6602020-11-27 08:48:46 -08002171#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002172 case PSA_ALG_RIPEMD160:
2173 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2174 break;
2175#endif
John Durkopee4e6602020-11-27 08:48:46 -08002176#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002177 case PSA_ALG_SHA_1:
2178 mbedtls_sha1_free( &operation->ctx.sha1 );
2179 break;
2180#endif
John Durkop6ca23272020-12-03 06:01:32 -08002181#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002182 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002183 mbedtls_sha256_free( &operation->ctx.sha256 );
2184 break;
2185#endif
2186#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002187 case PSA_ALG_SHA_256:
2188 mbedtls_sha256_free( &operation->ctx.sha256 );
2189 break;
2190#endif
John Durkop6ca23272020-12-03 06:01:32 -08002191#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002192 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002193 mbedtls_sha512_free( &operation->ctx.sha512 );
2194 break;
2195#endif
2196#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002197 case PSA_ALG_SHA_512:
2198 mbedtls_sha512_free( &operation->ctx.sha512 );
2199 break;
2200#endif
2201 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002202 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002203 }
2204 operation->alg = 0;
2205 return( PSA_SUCCESS );
2206}
2207
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002208psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002209 psa_algorithm_t alg )
2210{
Janos Follath24eed8d2019-11-22 13:21:35 +00002211 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002212
2213 /* A context must be freshly initialized before it can be set up. */
2214 if( operation->alg != 0 )
2215 {
2216 return( PSA_ERROR_BAD_STATE );
2217 }
2218
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002219 switch( alg )
2220 {
John Durkopee4e6602020-11-27 08:48:46 -08002221#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222 case PSA_ALG_MD2:
2223 mbedtls_md2_init( &operation->ctx.md2 );
2224 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2225 break;
2226#endif
John Durkopee4e6602020-11-27 08:48:46 -08002227#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002228 case PSA_ALG_MD4:
2229 mbedtls_md4_init( &operation->ctx.md4 );
2230 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2231 break;
2232#endif
John Durkopee4e6602020-11-27 08:48:46 -08002233#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002234 case PSA_ALG_MD5:
2235 mbedtls_md5_init( &operation->ctx.md5 );
2236 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2237 break;
2238#endif
John Durkopee4e6602020-11-27 08:48:46 -08002239#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002240 case PSA_ALG_RIPEMD160:
2241 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2242 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2243 break;
2244#endif
John Durkopee4e6602020-11-27 08:48:46 -08002245#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002246 case PSA_ALG_SHA_1:
2247 mbedtls_sha1_init( &operation->ctx.sha1 );
2248 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2249 break;
2250#endif
John Durkopee4e6602020-11-27 08:48:46 -08002251#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002252 case PSA_ALG_SHA_224:
2253 mbedtls_sha256_init( &operation->ctx.sha256 );
2254 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2255 break;
John Durkopee4e6602020-11-27 08:48:46 -08002256#endif
2257#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258 case PSA_ALG_SHA_256:
2259 mbedtls_sha256_init( &operation->ctx.sha256 );
2260 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2261 break;
2262#endif
John Durkopee4e6602020-11-27 08:48:46 -08002263#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264 case PSA_ALG_SHA_384:
2265 mbedtls_sha512_init( &operation->ctx.sha512 );
2266 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2267 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002268#endif
John Durkopee4e6602020-11-27 08:48:46 -08002269#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002270 case PSA_ALG_SHA_512:
2271 mbedtls_sha512_init( &operation->ctx.sha512 );
2272 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2273 break;
2274#endif
2275 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002276 return( PSA_ALG_IS_HASH( alg ) ?
2277 PSA_ERROR_NOT_SUPPORTED :
2278 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279 }
2280 if( ret == 0 )
2281 operation->alg = alg;
2282 else
2283 psa_hash_abort( operation );
2284 return( mbedtls_to_psa_error( ret ) );
2285}
2286
2287psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2288 const uint8_t *input,
2289 size_t input_length )
2290{
Janos Follath24eed8d2019-11-22 13:21:35 +00002291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002292
2293 /* Don't require hash implementations to behave correctly on a
2294 * zero-length input, which may have an invalid pointer. */
2295 if( input_length == 0 )
2296 return( PSA_SUCCESS );
2297
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002298 switch( operation->alg )
2299 {
John Durkopee4e6602020-11-27 08:48:46 -08002300#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002301 case PSA_ALG_MD2:
2302 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2303 input, input_length );
2304 break;
2305#endif
John Durkopee4e6602020-11-27 08:48:46 -08002306#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002307 case PSA_ALG_MD4:
2308 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2309 input, input_length );
2310 break;
2311#endif
John Durkopee4e6602020-11-27 08:48:46 -08002312#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002313 case PSA_ALG_MD5:
2314 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2315 input, input_length );
2316 break;
2317#endif
John Durkopee4e6602020-11-27 08:48:46 -08002318#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319 case PSA_ALG_RIPEMD160:
2320 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2321 input, input_length );
2322 break;
2323#endif
John Durkopee4e6602020-11-27 08:48:46 -08002324#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325 case PSA_ALG_SHA_1:
2326 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2327 input, input_length );
2328 break;
2329#endif
John Durkop6ca23272020-12-03 06:01:32 -08002330#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002332 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2333 input, input_length );
2334 break;
2335#endif
2336#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337 case PSA_ALG_SHA_256:
2338 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2339 input, input_length );
2340 break;
2341#endif
John Durkop6ca23272020-12-03 06:01:32 -08002342#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002344 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2345 input, input_length );
2346 break;
2347#endif
2348#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349 case PSA_ALG_SHA_512:
2350 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2351 input, input_length );
2352 break;
2353#endif
2354 default:
John Durkopee4e6602020-11-27 08:48:46 -08002355 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002356 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002357 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002358
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002359 if( ret != 0 )
2360 psa_hash_abort( operation );
2361 return( mbedtls_to_psa_error( ret ) );
2362}
2363
2364psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2365 uint8_t *hash,
2366 size_t hash_size,
2367 size_t *hash_length )
2368{
itayzafrir40835d42018-08-02 13:14:17 +03002369 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002371 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002372
2373 /* Fill the output buffer with something that isn't a valid hash
2374 * (barring an attack on the hash and deliberately-crafted input),
2375 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002376 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002377 /* If hash_size is 0 then hash may be NULL and then the
2378 * call to memset would have undefined behavior. */
2379 if( hash_size != 0 )
2380 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381
2382 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002383 {
2384 status = PSA_ERROR_BUFFER_TOO_SMALL;
2385 goto exit;
2386 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387
2388 switch( operation->alg )
2389 {
John Durkopee4e6602020-11-27 08:48:46 -08002390#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002391 case PSA_ALG_MD2:
2392 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2393 break;
2394#endif
John Durkopee4e6602020-11-27 08:48:46 -08002395#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002396 case PSA_ALG_MD4:
2397 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2398 break;
2399#endif
John Durkopee4e6602020-11-27 08:48:46 -08002400#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401 case PSA_ALG_MD5:
2402 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2403 break;
2404#endif
John Durkopee4e6602020-11-27 08:48:46 -08002405#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002406 case PSA_ALG_RIPEMD160:
2407 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2408 break;
2409#endif
John Durkopee4e6602020-11-27 08:48:46 -08002410#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411 case PSA_ALG_SHA_1:
2412 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2413 break;
2414#endif
John Durkop6ca23272020-12-03 06:01:32 -08002415#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002416 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002417 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2418 break;
2419#endif
2420#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002421 case PSA_ALG_SHA_256:
2422 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2423 break;
2424#endif
John Durkop6ca23272020-12-03 06:01:32 -08002425#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002426 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002427 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2428 break;
2429#endif
2430#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002431 case PSA_ALG_SHA_512:
2432 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2433 break;
2434#endif
2435 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002436 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002437 }
itayzafrir40835d42018-08-02 13:14:17 +03002438 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002439
itayzafrir40835d42018-08-02 13:14:17 +03002440exit:
2441 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002442 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002443 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002444 return( psa_hash_abort( operation ) );
2445 }
2446 else
2447 {
2448 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002449 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002450 }
2451}
2452
Gilles Peskine2d277862018-06-18 15:41:12 +02002453psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2454 const uint8_t *hash,
2455 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002456{
2457 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2458 size_t actual_hash_length;
2459 psa_status_t status = psa_hash_finish( operation,
2460 actual_hash, sizeof( actual_hash ),
2461 &actual_hash_length );
2462 if( status != PSA_SUCCESS )
2463 return( status );
2464 if( actual_hash_length != hash_length )
2465 return( PSA_ERROR_INVALID_SIGNATURE );
2466 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2467 return( PSA_ERROR_INVALID_SIGNATURE );
2468 return( PSA_SUCCESS );
2469}
2470
Gilles Peskine0a749c82019-11-28 19:33:58 +01002471psa_status_t psa_hash_compute( psa_algorithm_t alg,
2472 const uint8_t *input, size_t input_length,
2473 uint8_t *hash, size_t hash_size,
2474 size_t *hash_length )
2475{
2476 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2478
2479 *hash_length = hash_size;
2480 status = psa_hash_setup( &operation, alg );
2481 if( status != PSA_SUCCESS )
2482 goto exit;
2483 status = psa_hash_update( &operation, input, input_length );
2484 if( status != PSA_SUCCESS )
2485 goto exit;
2486 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2487 if( status != PSA_SUCCESS )
2488 goto exit;
2489
2490exit:
2491 if( status == PSA_SUCCESS )
2492 status = psa_hash_abort( &operation );
2493 else
2494 psa_hash_abort( &operation );
2495 return( status );
2496}
2497
2498psa_status_t psa_hash_compare( psa_algorithm_t alg,
2499 const uint8_t *input, size_t input_length,
2500 const uint8_t *hash, size_t hash_length )
2501{
2502 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2503 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2504
2505 status = psa_hash_setup( &operation, alg );
2506 if( status != PSA_SUCCESS )
2507 goto exit;
2508 status = psa_hash_update( &operation, input, input_length );
2509 if( status != PSA_SUCCESS )
2510 goto exit;
2511 status = psa_hash_verify( &operation, hash, hash_length );
2512 if( status != PSA_SUCCESS )
2513 goto exit;
2514
2515exit:
2516 if( status == PSA_SUCCESS )
2517 status = psa_hash_abort( &operation );
2518 else
2519 psa_hash_abort( &operation );
2520 return( status );
2521}
2522
Gilles Peskineeb35d782019-01-22 17:56:16 +01002523psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2524 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002525{
2526 if( target_operation->alg != 0 )
2527 return( PSA_ERROR_BAD_STATE );
2528
2529 switch( source_operation->alg )
2530 {
2531 case 0:
2532 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002533#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002534 case PSA_ALG_MD2:
2535 mbedtls_md2_clone( &target_operation->ctx.md2,
2536 &source_operation->ctx.md2 );
2537 break;
2538#endif
John Durkopee4e6602020-11-27 08:48:46 -08002539#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002540 case PSA_ALG_MD4:
2541 mbedtls_md4_clone( &target_operation->ctx.md4,
2542 &source_operation->ctx.md4 );
2543 break;
2544#endif
John Durkopee4e6602020-11-27 08:48:46 -08002545#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002546 case PSA_ALG_MD5:
2547 mbedtls_md5_clone( &target_operation->ctx.md5,
2548 &source_operation->ctx.md5 );
2549 break;
2550#endif
John Durkopee4e6602020-11-27 08:48:46 -08002551#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002552 case PSA_ALG_RIPEMD160:
2553 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2554 &source_operation->ctx.ripemd160 );
2555 break;
2556#endif
John Durkopee4e6602020-11-27 08:48:46 -08002557#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002558 case PSA_ALG_SHA_1:
2559 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2560 &source_operation->ctx.sha1 );
2561 break;
2562#endif
John Durkop6ca23272020-12-03 06:01:32 -08002563#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002564 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002565 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2566 &source_operation->ctx.sha256 );
2567 break;
2568#endif
2569#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002570 case PSA_ALG_SHA_256:
2571 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2572 &source_operation->ctx.sha256 );
2573 break;
2574#endif
John Durkop6ca23272020-12-03 06:01:32 -08002575#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002576 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002577 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2578 &source_operation->ctx.sha512 );
2579 break;
2580#endif
2581#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002582 case PSA_ALG_SHA_512:
2583 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2584 &source_operation->ctx.sha512 );
2585 break;
2586#endif
2587 default:
2588 return( PSA_ERROR_NOT_SUPPORTED );
2589 }
2590
2591 target_operation->alg = source_operation->alg;
2592 return( PSA_SUCCESS );
2593}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594
2595
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002596/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597/* MAC */
2598/****************************************************************/
2599
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002600static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601 psa_algorithm_t alg,
2602 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002603 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002604 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002607 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002609 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002610 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002611
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2613 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002614 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002615 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002616 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617 mode = MBEDTLS_MODE_STREAM;
2618 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002619 case PSA_ALG_CTR:
2620 mode = MBEDTLS_MODE_CTR;
2621 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002622 case PSA_ALG_CFB:
2623 mode = MBEDTLS_MODE_CFB;
2624 break;
2625 case PSA_ALG_OFB:
2626 mode = MBEDTLS_MODE_OFB;
2627 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002628 case PSA_ALG_ECB_NO_PADDING:
2629 mode = MBEDTLS_MODE_ECB;
2630 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002631 case PSA_ALG_CBC_NO_PADDING:
2632 mode = MBEDTLS_MODE_CBC;
2633 break;
2634 case PSA_ALG_CBC_PKCS7:
2635 mode = MBEDTLS_MODE_CBC;
2636 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002637 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002638 mode = MBEDTLS_MODE_CCM;
2639 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002640 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002641 mode = MBEDTLS_MODE_GCM;
2642 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002643 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002644 mode = MBEDTLS_MODE_CHACHAPOLY;
2645 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002646 default:
2647 return( NULL );
2648 }
2649 }
2650 else if( alg == PSA_ALG_CMAC )
2651 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002652 else
2653 return( NULL );
2654
2655 switch( key_type )
2656 {
2657 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002658 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002659 break;
2660 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002661 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2662 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002663 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002664 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002665 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002666 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002667 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2668 * but two-key Triple-DES is functionally three-key Triple-DES
2669 * with K1=K3, so that's how we present it to mbedtls. */
2670 if( key_bits == 128 )
2671 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672 break;
2673 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002674 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675 break;
2676 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002677 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002679 case PSA_KEY_TYPE_CHACHA20:
2680 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2681 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002682 default:
2683 return( NULL );
2684 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002685 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002686 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687
Jaeden Amero23bbb752018-06-26 14:16:54 +01002688 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2689 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002690}
2691
John Durkop6ba40d12020-11-10 08:50:04 -08002692#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002693static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002694{
Gilles Peskine2d277862018-06-18 15:41:12 +02002695 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002696 {
2697 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002698 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002699 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002700 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002701 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002702 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002703 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002704 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002705 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002706 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002707 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002708 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002709 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002710 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002711 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002712 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002713 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002714 return( 128 );
2715 default:
2716 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002717 }
2718}
John Durkop07cc04a2020-11-16 22:08:34 -08002719#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002720
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002721/* Initialize the MAC operation structure. Once this function has been
2722 * called, psa_mac_abort can run and will do the right thing. */
2723static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2724 psa_algorithm_t alg )
2725{
2726 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2727
Steven Cooreman15472f82021-03-02 16:16:22 +01002728 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002729 operation->key_set = 0;
2730 operation->iv_set = 0;
2731 operation->iv_required = 0;
2732 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002733 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002734
2735#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002736 if( operation->alg == PSA_ALG_CMAC )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002737 {
2738 operation->iv_required = 0;
2739 mbedtls_cipher_init( &operation->ctx.cmac );
2740 status = PSA_SUCCESS;
2741 }
2742 else
2743#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002744#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002745 if( PSA_ALG_IS_HMAC( operation->alg ) )
2746 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002747 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2748 operation->ctx.hmac.hash_ctx.alg = 0;
2749 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002750 }
2751 else
John Durkopd0321952020-10-29 21:37:36 -07002752#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002753 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002754 if( ! PSA_ALG_IS_MAC( alg ) )
2755 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002756 }
2757
2758 if( status != PSA_SUCCESS )
2759 memset( operation, 0, sizeof( *operation ) );
2760 return( status );
2761}
2762
John Durkop6ba40d12020-11-10 08:50:04 -08002763#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002764static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2765{
Gilles Peskine3f108122018-12-07 18:14:53 +01002766 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002767 return( psa_hash_abort( &hmac->hash_ctx ) );
2768}
John Durkop6ba40d12020-11-10 08:50:04 -08002769#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002770
Gilles Peskine8c9def32018-02-08 10:02:12 +01002771psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2772{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002773 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002774 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002775 /* The object has (apparently) been initialized but it is not
2776 * in use. It's ok to call abort on such an object, and there's
2777 * nothing to do. */
2778 return( PSA_SUCCESS );
2779 }
2780 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002781#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002782 if( operation->alg == PSA_ALG_CMAC )
2783 {
2784 mbedtls_cipher_free( &operation->ctx.cmac );
2785 }
2786 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002788#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002789 if( PSA_ALG_IS_HMAC( operation->alg ) )
2790 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002791 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002792 }
2793 else
John Durkopd0321952020-10-29 21:37:36 -07002794#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002795 {
2796 /* Sanity check (shouldn't happen: operation->alg should
2797 * always have been initialized to a valid value). */
2798 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799 }
Moran Peker41deec42018-04-04 15:43:05 +03002800
Gilles Peskine8c9def32018-02-08 10:02:12 +01002801 operation->alg = 0;
2802 operation->key_set = 0;
2803 operation->iv_set = 0;
2804 operation->iv_required = 0;
2805 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002806 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002807
Gilles Peskine8c9def32018-02-08 10:02:12 +01002808 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002809
2810bad_state:
2811 /* If abort is called on an uninitialized object, we can't trust
2812 * anything. Wipe the object in case it contains confidential data.
2813 * This may result in a memory leak if a pointer gets overwritten,
2814 * but it's too late to do anything about this. */
2815 memset( operation, 0, sizeof( *operation ) );
2816 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002817}
2818
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002819#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002820static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation,
2821 psa_key_slot_t *slot )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002822{
Janos Follath24eed8d2019-11-22 13:21:35 +00002823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002824 const mbedtls_cipher_info_t *cipher_info =
2825 mbedtls_cipher_info_from_psa( PSA_ALG_CMAC,
2826 slot->attr.type, slot->attr.bits,
2827 NULL );
2828 if( cipher_info == NULL )
2829 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002830
2831 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2832 if( ret != 0 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002833 goto exit;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002834
2835 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002836 slot->key.data,
Steven Cooreman15472f82021-03-02 16:16:22 +01002837 slot->attr.bits );
2838exit:
2839 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002840}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002841#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002842
John Durkop6ba40d12020-11-10 08:50:04 -08002843#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002844static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2845 const uint8_t *key,
2846 size_t key_length,
2847 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002848{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002849 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002850 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002851 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002852 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002853 psa_status_t status;
2854
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002855 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2856 * overflow below. This should never trigger if the hash algorithm
2857 * is implemented correctly. */
2858 /* The size checks against the ipad and opad buffers cannot be written
2859 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2860 * because that triggers -Wlogical-op on GCC 7.3. */
2861 if( block_size > sizeof( ipad ) )
2862 return( PSA_ERROR_NOT_SUPPORTED );
2863 if( block_size > sizeof( hmac->opad ) )
2864 return( PSA_ERROR_NOT_SUPPORTED );
2865 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002866 return( PSA_ERROR_NOT_SUPPORTED );
2867
Gilles Peskined223b522018-06-11 18:12:58 +02002868 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002869 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002870 status = psa_hash_compute( hash_alg, key, key_length,
2871 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002872 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002873 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002874 }
Gilles Peskine96889972018-07-12 17:07:03 +02002875 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2876 * but it is permitted. It is common when HMAC is used in HKDF, for
2877 * example. Don't call `memcpy` in the 0-length because `key` could be
2878 * an invalid pointer which would make the behavior undefined. */
2879 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002880 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002881
Gilles Peskined223b522018-06-11 18:12:58 +02002882 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2883 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002884 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002885 ipad[i] ^= 0x36;
2886 memset( ipad + key_length, 0x36, block_size - key_length );
2887
2888 /* Copy the key material from ipad to opad, flipping the requisite bits,
2889 * and filling the rest of opad with the requisite constant. */
2890 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002891 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2892 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002893
Gilles Peskine01126fa2018-07-12 17:04:55 +02002894 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002895 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002896 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002897
Gilles Peskine01126fa2018-07-12 17:04:55 +02002898 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002899
2900cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002901 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002902
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002903 return( status );
2904}
John Durkop6ba40d12020-11-10 08:50:04 -08002905#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002906
Gilles Peskine89167cb2018-07-08 20:12:23 +02002907static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002908 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002909 psa_algorithm_t alg,
2910 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002911{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002913 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002914 psa_key_slot_t *slot;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002915 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002916 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002917
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002918 /* A context must be freshly initialized before it can be set up. */
2919 if( operation->alg != 0 )
2920 {
2921 return( PSA_ERROR_BAD_STATE );
2922 }
2923
Steven Cooreman15472f82021-03-02 16:16:22 +01002924 status = psa_mac_init( operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002925 if( status != PSA_SUCCESS )
2926 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002927 if( is_sign )
2928 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002929
Ronald Cron5c522922020-11-14 16:35:34 +01002930 status = psa_get_and_lock_transparent_key_slot_with_policy(
2931 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002932 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002933 goto exit;
Steven Cooreman15472f82021-03-02 16:16:22 +01002934
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002935 /* Validate the combination of key type and algorithm */
2936 status = psa_mac_key_can_do( alg, slot->attr.type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002937 if( status != PSA_SUCCESS )
2938 goto exit;
2939
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002940 /* Get the output length for the algorithm and key combination. None of the
2941 * currently supported algorithms have an output length dependent on actual
2942 * key size, so setting it to a bogus value is currently OK. */
2943 operation->mac_size = PSA_MAC_LENGTH( slot->attr.type, 0, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002944
2945 if( operation->mac_size < 4 )
2946 {
2947 /* A very short MAC is too short for security since it can be
2948 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2949 * so we make this our minimum, even though 32 bits is still
2950 * too small for security. */
2951 status = PSA_ERROR_NOT_SUPPORTED;
2952 goto exit;
2953 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002954
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002955 if( operation->mac_size >
2956 PSA_MAC_LENGTH( slot->attr.type, 0, PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
2957 {
2958 /* It's impossible to "truncate" to a larger length than the full length
2959 * of the algorithm. */
2960 status = PSA_ERROR_INVALID_ARGUMENT;
2961 goto exit;
2962 }
2963
Gilles Peskine8c9def32018-02-08 10:02:12 +01002964#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002965 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002966 {
Steven Cooreman15472f82021-03-02 16:16:22 +01002967 status = psa_cmac_setup( operation, slot );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002968 }
2969 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002970#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002971#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01002972 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002973 {
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002974 /* Sanity check. This shouldn't fail on a valid configuration. */
Steven Cooreman1fb691a2021-03-08 12:01:55 +01002975 if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002976 {
2977 status = PSA_ERROR_NOT_SUPPORTED;
2978 goto exit;
2979 }
2980
Gilles Peskine8e338702019-07-30 20:06:31 +02002981 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002982 {
2983 status = PSA_ERROR_INVALID_ARGUMENT;
2984 goto exit;
2985 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002986
Gilles Peskine01126fa2018-07-12 17:04:55 +02002987 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002988 slot->key.data,
2989 slot->key.bytes,
Steven Cooreman15472f82021-03-02 16:16:22 +01002990 PSA_ALG_HMAC_GET_HASH( alg ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002991 }
2992 else
John Durkopd0321952020-10-29 21:37:36 -07002993#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002994 {
2995 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002996 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002997
Gilles Peskinefbfac682018-07-08 20:51:54 +02002998exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002999 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003000 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003001 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003003 else
3004 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003005 operation->key_set = 1;
3006 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003007
Ronald Cron5c522922020-11-14 16:35:34 +01003008 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003009
Ronald Cron5c522922020-11-14 16:35:34 +01003010 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011}
3012
Gilles Peskine89167cb2018-07-08 20:12:23 +02003013psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003014 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003015 psa_algorithm_t alg )
3016{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003017 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003018}
3019
3020psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003021 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003022 psa_algorithm_t alg )
3023{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003024 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003025}
3026
Gilles Peskine8c9def32018-02-08 10:02:12 +01003027psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3028 const uint8_t *input,
3029 size_t input_length )
3030{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003031 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003033 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003035 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003036 operation->has_input = 1;
3037
Gilles Peskine8c9def32018-02-08 10:02:12 +01003038#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003039 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003040 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003041 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3042 input, input_length );
3043 status = mbedtls_to_psa_error( ret );
3044 }
3045 else
3046#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003047#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003048 if( PSA_ALG_IS_HMAC( operation->alg ) )
3049 {
3050 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3051 input_length );
3052 }
3053 else
John Durkopd0321952020-10-29 21:37:36 -07003054#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003055 {
3056 /* This shouldn't happen if `operation` was initialized by
3057 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003058 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003059 }
3060
Gilles Peskinefbfac682018-07-08 20:51:54 +02003061 if( status != PSA_SUCCESS )
3062 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003063 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003064}
3065
John Durkop6ba40d12020-11-10 08:50:04 -08003066#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003067static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3068 uint8_t *mac,
3069 size_t mac_size )
3070{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003071 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003072 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3073 size_t hash_size = 0;
3074 size_t block_size = psa_get_hash_block_size( hash_alg );
3075 psa_status_t status;
3076
Gilles Peskine01126fa2018-07-12 17:04:55 +02003077 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3078 if( status != PSA_SUCCESS )
3079 return( status );
3080 /* From here on, tmp needs to be wiped. */
3081
3082 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3083 if( status != PSA_SUCCESS )
3084 goto exit;
3085
3086 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3087 if( status != PSA_SUCCESS )
3088 goto exit;
3089
3090 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3091 if( status != PSA_SUCCESS )
3092 goto exit;
3093
Gilles Peskined911eb72018-08-14 15:18:45 +02003094 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3095 if( status != PSA_SUCCESS )
3096 goto exit;
3097
3098 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003099
3100exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003101 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003102 return( status );
3103}
John Durkop6ba40d12020-11-10 08:50:04 -08003104#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003105
mohammad16036df908f2018-04-02 08:34:15 -07003106static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003107 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003108 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003109{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003110 if( ! operation->key_set )
3111 return( PSA_ERROR_BAD_STATE );
3112 if( operation->iv_required && ! operation->iv_set )
3113 return( PSA_ERROR_BAD_STATE );
3114
Gilles Peskine8c9def32018-02-08 10:02:12 +01003115 if( mac_size < operation->mac_size )
3116 return( PSA_ERROR_BUFFER_TOO_SMALL );
3117
Gilles Peskine8c9def32018-02-08 10:02:12 +01003118#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003119 if( operation->alg == PSA_ALG_CMAC )
3120 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003121 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003122 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3123 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003124 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003125 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003126 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003128 else
3129#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003130#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003131 if( PSA_ALG_IS_HMAC( operation->alg ) )
3132 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003133 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003134 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003135 }
3136 else
John Durkopd0321952020-10-29 21:37:36 -07003137#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003138 {
3139 /* This shouldn't happen if `operation` was initialized by
3140 * a setup function. */
3141 return( PSA_ERROR_BAD_STATE );
3142 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003143}
3144
Gilles Peskineacd4be32018-07-08 19:56:25 +02003145psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3146 uint8_t *mac,
3147 size_t mac_size,
3148 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003149{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003150 psa_status_t status;
3151
Jaeden Amero252ef282019-02-15 14:05:35 +00003152 if( operation->alg == 0 )
3153 {
3154 return( PSA_ERROR_BAD_STATE );
3155 }
3156
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003157 /* Fill the output buffer with something that isn't a valid mac
3158 * (barring an attack on the mac and deliberately-crafted input),
3159 * in case the caller doesn't check the return status properly. */
3160 *mac_length = mac_size;
3161 /* If mac_size is 0 then mac may be NULL and then the
3162 * call to memset would have undefined behavior. */
3163 if( mac_size != 0 )
3164 memset( mac, '!', mac_size );
3165
Gilles Peskine89167cb2018-07-08 20:12:23 +02003166 if( ! operation->is_sign )
3167 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003168 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003169 }
mohammad16036df908f2018-04-02 08:34:15 -07003170
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003171 status = psa_mac_finish_internal( operation, mac, mac_size );
3172
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003173 if( status == PSA_SUCCESS )
3174 {
3175 status = psa_mac_abort( operation );
3176 if( status == PSA_SUCCESS )
3177 *mac_length = operation->mac_size;
3178 else
3179 memset( mac, '!', mac_size );
3180 }
3181 else
3182 psa_mac_abort( operation );
3183 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003184}
3185
Gilles Peskineacd4be32018-07-08 19:56:25 +02003186psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3187 const uint8_t *mac,
3188 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003189{
Gilles Peskine828ed142018-06-18 23:25:51 +02003190 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003191 psa_status_t status;
3192
Jaeden Amero252ef282019-02-15 14:05:35 +00003193 if( operation->alg == 0 )
3194 {
3195 return( PSA_ERROR_BAD_STATE );
3196 }
3197
Gilles Peskine89167cb2018-07-08 20:12:23 +02003198 if( operation->is_sign )
3199 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003200 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003201 }
3202 if( operation->mac_size != mac_length )
3203 {
3204 status = PSA_ERROR_INVALID_SIGNATURE;
3205 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003206 }
mohammad16036df908f2018-04-02 08:34:15 -07003207
3208 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003209 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003210 if( status != PSA_SUCCESS )
3211 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003212
3213 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3214 status = PSA_ERROR_INVALID_SIGNATURE;
3215
3216cleanup:
3217 if( status == PSA_SUCCESS )
3218 status = psa_mac_abort( operation );
3219 else
3220 psa_mac_abort( operation );
3221
Gilles Peskine3f108122018-12-07 18:14:53 +01003222 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003223
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003224 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003225}
3226
3227
Gilles Peskine20035e32018-02-03 22:44:14 +01003228
Gilles Peskine20035e32018-02-03 22:44:14 +01003229/****************************************************************/
3230/* Asymmetric cryptography */
3231/****************************************************************/
3232
Ronald Cron67b1eb32020-12-08 17:37:27 +01003233psa_status_t psa_sign_hash_internal(
Ronald Cron18659932020-12-08 16:32:23 +01003234 const psa_key_attributes_t *attributes,
3235 const uint8_t *key_buffer, size_t key_buffer_size,
3236 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
3237 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003238{
Gilles Peskine20035e32018-02-03 22:44:14 +01003239#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3240 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron99b8ed72021-02-17 10:33:32 +01003241 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003242 {
Ronald Cron7bdbca32020-12-09 13:34:54 +01003243 return( mbedtls_psa_rsa_sign_hash(
3244 attributes,
3245 key_buffer, key_buffer_size,
3246 alg, hash, hash_length,
3247 signature, signature_size, signature_length ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003248 }
3249 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003250#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3251 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine20035e32018-02-03 22:44:14 +01003252#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3253 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Ronald Cronbb9cbc72021-03-04 17:09:00 +01003254 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003255 {
Ronald Cron9103d492021-03-04 11:26:03 +01003256 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003257 {
Ronald Cron072722c2020-12-09 16:36:19 +01003258 return( mbedtls_psa_ecdsa_sign_hash(
3259 attributes,
3260 key_buffer, key_buffer_size,
3261 alg, hash, hash_length,
3262 signature, signature_size, signature_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003263 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003264 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003265 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003266 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003267 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01003268 }
3269 else
Gilles Peskinee59236f2018-01-27 23:32:46 +01003270#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3271 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
itayzafrir5c753392018-05-08 11:18:38 +03003272 {
Ronald Cronbb9cbc72021-03-04 17:09:00 +01003273 (void)attributes;
Ronald Cron8a494f32021-02-17 09:49:51 +01003274 (void)key_buffer;
3275 (void)key_buffer_size;
3276 (void)alg;
3277 (void)hash;
3278 (void)hash_length;
3279 (void)signature;
3280 (void)signature_size;
3281 (void)signature_length;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003282
Ronald Cron8a494f32021-02-17 09:49:51 +01003283 return( PSA_ERROR_NOT_SUPPORTED );
3284 }
Ronald Cron18659932020-12-08 16:32:23 +01003285}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003286
3287psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
3288 psa_algorithm_t alg,
3289 const uint8_t *hash,
3290 size_t hash_length,
3291 uint8_t *signature,
3292 size_t signature_size,
3293 size_t *signature_length )
3294{
3295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3296 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3297 psa_key_slot_t *slot;
3298
3299 *signature_length = signature_size;
3300 /* Immediately reject a zero-length signature buffer. This guarantees
3301 * that signature must be a valid pointer. (On the other hand, the hash
3302 * buffer can in principle be empty since it doesn't actually have
3303 * to be a hash.) */
3304 if( signature_size == 0 )
3305 return( PSA_ERROR_BUFFER_TOO_SMALL );
3306
3307 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3308 PSA_KEY_USAGE_SIGN_HASH,
3309 alg );
3310 if( status != PSA_SUCCESS )
3311 goto exit;
3312 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
3313 {
3314 status = PSA_ERROR_INVALID_ARGUMENT;
3315 goto exit;
3316 }
3317
Ronald Cron9f17aa42020-12-08 17:07:25 +01003318 psa_key_attributes_t attributes = {
3319 .core = slot->attr
3320 };
Gilles Peskinee59236f2018-01-27 23:32:46 +01003321
Ronald Cron9f17aa42020-12-08 17:07:25 +01003322 status = psa_driver_wrapper_sign_hash(
3323 &attributes, slot->key.data, slot->key.bytes,
3324 alg, hash, hash_length,
3325 signature, signature_size, signature_length );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003326
3327exit:
3328 /* Fill the unused part of the output buffer (the whole buffer on error,
3329 * the trailing part on success) with something that isn't a valid mac
3330 * (barring an attack on the mac and deliberately-crafted input),
3331 * in case the caller doesn't check the return status properly. */
3332 if( status == PSA_SUCCESS )
3333 memset( signature + *signature_length, '!',
3334 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003335 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003336 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003337 /* If signature_size is 0 then we have nothing to do. We must not call
3338 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003339
Ronald Cron5c522922020-11-14 16:35:34 +01003340 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003341
Ronald Cron5c522922020-11-14 16:35:34 +01003342 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003343}
3344
Ronald Cron67b1eb32020-12-08 17:37:27 +01003345psa_status_t psa_verify_hash_internal(
Ronald Cron18659932020-12-08 16:32:23 +01003346 const psa_key_attributes_t *attributes,
3347 const uint8_t *key_buffer, size_t key_buffer_size,
3348 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
3349 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003350{
John Durkop6ba40d12020-11-10 08:50:04 -08003351#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3352 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron99b8ed72021-02-17 10:33:32 +01003353 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003354 {
Ronald Cron7bdbca32020-12-09 13:34:54 +01003355 return( mbedtls_psa_rsa_verify_hash(
3356 attributes,
3357 key_buffer, key_buffer_size,
3358 alg, hash, hash_length,
3359 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003360 }
3361 else
John Durkop6ba40d12020-11-10 08:50:04 -08003362#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3363 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Ronald Cron99b8ed72021-02-17 10:33:32 +01003364 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003365 {
John Durkop9814fa22020-11-04 12:28:15 -08003366#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3367 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003368 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003369 {
Ronald Cron072722c2020-12-09 16:36:19 +01003370 return( mbedtls_psa_ecdsa_verify_hash(
3371 attributes,
3372 key_buffer, key_buffer_size,
3373 alg, hash, hash_length,
3374 signature, signature_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003375 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003376 else
John Durkop9814fa22020-11-04 12:28:15 -08003377#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3378 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003379 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003380 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003381 }
itayzafrir5c753392018-05-08 11:18:38 +03003382 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01003383 else
Gilles Peskinee59236f2018-01-27 23:32:46 +01003384 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003385 (void)key_buffer;
3386 (void)key_buffer_size;
3387 (void)alg;
3388 (void)hash;
3389 (void)hash_length;
3390 (void)signature;
3391 (void)signature_length;
Ronald Cronf95a2b12020-10-22 15:24:49 +02003392
Ronald Cron8a494f32021-02-17 09:49:51 +01003393 return( PSA_ERROR_NOT_SUPPORTED );
3394 }
Ronald Cron18659932020-12-08 16:32:23 +01003395}
3396
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003397psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3398 psa_algorithm_t alg,
3399 const uint8_t *hash,
3400 size_t hash_length,
3401 const uint8_t *signature,
3402 size_t signature_length )
3403{
3404 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3405 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003406 psa_key_slot_t *slot;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003407
3408 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003409 PSA_KEY_USAGE_VERIFY_HASH,
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003410 alg );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003411 if( status != PSA_SUCCESS )
3412 return( status );
3413
Ronald Cron9f17aa42020-12-08 17:07:25 +01003414 psa_key_attributes_t attributes = {
3415 .core = slot->attr
3416 };
Gilles Peskinee59236f2018-01-27 23:32:46 +01003417
Ronald Cron9f17aa42020-12-08 17:07:25 +01003418 status = psa_driver_wrapper_verify_hash(
3419 &attributes, slot->key.data, slot->key.bytes,
3420 alg, hash, hash_length,
3421 signature, signature_length );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003422
Ronald Cron5c522922020-11-14 16:35:34 +01003423 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003424
Ronald Cron5c522922020-11-14 16:35:34 +01003425 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003426}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003427
John Durkop0e005192020-10-31 22:06:54 -07003428#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003429static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3430 mbedtls_rsa_context *rsa )
3431{
3432 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3433 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3434 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3435 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3436}
John Durkop0e005192020-10-31 22:06:54 -07003437#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003438
Ronald Croncf56a0a2020-08-04 09:51:30 +02003439psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003440 psa_algorithm_t alg,
3441 const uint8_t *input,
3442 size_t input_length,
3443 const uint8_t *salt,
3444 size_t salt_length,
3445 uint8_t *output,
3446 size_t output_size,
3447 size_t *output_length )
3448{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003450 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003451 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003452
Darryl Green5cc689a2018-07-24 15:34:10 +01003453 (void) input;
3454 (void) input_length;
3455 (void) salt;
3456 (void) output;
3457 (void) output_size;
3458
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003459 *output_length = 0;
3460
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003461 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3462 return( PSA_ERROR_INVALID_ARGUMENT );
3463
Ronald Cron5c522922020-11-14 16:35:34 +01003464 status = psa_get_and_lock_transparent_key_slot_with_policy(
3465 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003466 if( status != PSA_SUCCESS )
3467 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003468 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3469 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003470 {
3471 status = PSA_ERROR_INVALID_ARGUMENT;
3472 goto exit;
3473 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003474
John Durkop6ba40d12020-11-10 08:50:04 -08003475#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3476 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003477 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003478 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003479 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003480 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3481 slot->key.data,
3482 slot->key.bytes,
3483 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003484 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003485 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003486
3487 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003488 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003489 status = PSA_ERROR_BUFFER_TOO_SMALL;
3490 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003491 }
John Durkop0e005192020-10-31 22:06:54 -07003492#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003493 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
3494 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003495 status = mbedtls_to_psa_error(
3496 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003497 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003498 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003499 MBEDTLS_RSA_PUBLIC,
3500 input_length,
3501 input,
3502 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003503 }
3504 else
John Durkop0e005192020-10-31 22:06:54 -07003505#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3506#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003507 if( PSA_ALG_IS_RSA_OAEP( alg ) )
3508 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003509 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003510 status = mbedtls_to_psa_error(
3511 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003512 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003513 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003514 MBEDTLS_RSA_PUBLIC,
3515 salt, salt_length,
3516 input_length,
3517 input,
3518 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003519 }
3520 else
John Durkop0e005192020-10-31 22:06:54 -07003521#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003522 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003523 status = PSA_ERROR_INVALID_ARGUMENT;
3524 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003525 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003526rsa_exit:
3527 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003528 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003529
Steven Cooremana2371e52020-07-28 14:30:39 +02003530 mbedtls_rsa_free( rsa );
3531 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003532 }
3533 else
John Durkop9814fa22020-11-04 12:28:15 -08003534#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003535 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003536 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003537 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003538 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003539
3540exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003541 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003542
Ronald Cron5c522922020-11-14 16:35:34 +01003543 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003544}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003545
Ronald Croncf56a0a2020-08-04 09:51:30 +02003546psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003547 psa_algorithm_t alg,
3548 const uint8_t *input,
3549 size_t input_length,
3550 const uint8_t *salt,
3551 size_t salt_length,
3552 uint8_t *output,
3553 size_t output_size,
3554 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003555{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003556 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003557 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003558 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003559
Darryl Green5cc689a2018-07-24 15:34:10 +01003560 (void) input;
3561 (void) input_length;
3562 (void) salt;
3563 (void) output;
3564 (void) output_size;
3565
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003566 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003567
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003568 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3569 return( PSA_ERROR_INVALID_ARGUMENT );
3570
Ronald Cron5c522922020-11-14 16:35:34 +01003571 status = psa_get_and_lock_transparent_key_slot_with_policy(
3572 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003573 if( status != PSA_SUCCESS )
3574 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003575 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003576 {
3577 status = PSA_ERROR_INVALID_ARGUMENT;
3578 goto exit;
3579 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003580
John Durkop6ba40d12020-11-10 08:50:04 -08003581#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3582 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003583 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003584 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003585 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003586 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3587 slot->key.data,
3588 slot->key.bytes,
3589 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003590 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003591 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003592
Steven Cooremana2371e52020-07-28 14:30:39 +02003593 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003594 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003595 status = PSA_ERROR_INVALID_ARGUMENT;
3596 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003597 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003598
John Durkop0e005192020-10-31 22:06:54 -07003599#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003600 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003601 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003602 status = mbedtls_to_psa_error(
3603 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003604 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003605 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003606 MBEDTLS_RSA_PRIVATE,
3607 output_length,
3608 input,
3609 output,
3610 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003611 }
3612 else
John Durkop0e005192020-10-31 22:06:54 -07003613#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3614#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003615 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003616 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003617 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003618 status = mbedtls_to_psa_error(
3619 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003620 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003621 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003622 MBEDTLS_RSA_PRIVATE,
3623 salt, salt_length,
3624 output_length,
3625 input,
3626 output,
3627 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003628 }
3629 else
John Durkop0e005192020-10-31 22:06:54 -07003630#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003631 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003632 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003634
Steven Cooreman4fed4552020-08-03 14:46:03 +02003635rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003636 mbedtls_rsa_free( rsa );
3637 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003638 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003639 else
John Durkop6ba40d12020-11-10 08:50:04 -08003640#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3641 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003643 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003645
3646exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003647 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003648
Ronald Cron5c522922020-11-14 16:35:34 +01003649 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003650}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003651
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003652
3653
mohammad1603503973b2018-03-12 15:59:30 +02003654/****************************************************************/
3655/* Symmetric cryptography */
3656/****************************************************************/
3657
Gilles Peskinee553c652018-06-04 16:22:46 +02003658static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003659 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003660 psa_algorithm_t alg,
3661 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003662{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003663 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003664 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003665 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003666 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003667 size_t key_bits;
3668 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003669 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3670 PSA_KEY_USAGE_ENCRYPT :
3671 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003672
Steven Cooremand3feccd2020-09-01 15:56:14 +02003673 /* A context must be freshly initialized before it can be set up. */
3674 if( operation->alg != 0 )
3675 return( PSA_ERROR_BAD_STATE );
3676
Steven Cooremana07b9972020-09-10 14:54:14 +02003677 /* The requested algorithm must be one that can be processed by cipher. */
3678 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003679 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003680
Steven Cooremanef8575e2020-09-11 11:44:50 +02003681 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003682 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003683 if( status != PSA_SUCCESS )
3684 goto exit;
3685
3686 /* Initialize the operation struct members, except for alg. The alg member
3687 * is used to indicate to psa_cipher_abort that there are resources to free,
3688 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003689 operation->key_set = 0;
3690 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003691 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003692 operation->iv_size = 0;
3693 operation->block_size = 0;
3694 if( alg == PSA_ALG_ECB_NO_PADDING )
3695 operation->iv_required = 0;
3696 else
3697 operation->iv_required = 1;
3698
Steven Cooremana07b9972020-09-10 14:54:14 +02003699 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003700 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003701 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003702 slot,
3703 alg );
3704 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003705 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003706 slot,
3707 alg );
3708
Steven Cooremanef8575e2020-09-11 11:44:50 +02003709 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003710 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003711 /* Once the driver context is initialised, it needs to be freed using
3712 * psa_cipher_abort. Indicate this through setting alg. */
3713 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003714 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02003715
Steven Cooremand3feccd2020-09-01 15:56:14 +02003716 if( status != PSA_ERROR_NOT_SUPPORTED ||
3717 psa_key_lifetime_is_external( slot->attr.lifetime ) )
3718 goto exit;
3719
Steven Cooremana07b9972020-09-10 14:54:14 +02003720 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02003721 * available for the given algorithm & key. */
3722 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02003723
Steven Cooremana07b9972020-09-10 14:54:14 +02003724 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02003725 * psa_cipher_abort. Indicate there is something to be freed through setting
3726 * alg, and indicate the operation is being done using mbedtls crypto through
3727 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003728 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003729 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02003730
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003731 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02003732 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003733 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003734 {
3735 status = PSA_ERROR_NOT_SUPPORTED;
3736 goto exit;
3737 }
mohammad1603503973b2018-03-12 15:59:30 +02003738
mohammad1603503973b2018-03-12 15:59:30 +02003739 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003740 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003741 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003742
David Brown12ca5032021-02-11 11:02:00 -07003743#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02003744 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003745 {
3746 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003747 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01003748 memcpy( keys, slot->key.data, 16 );
3749 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003750 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3751 keys,
3752 192, cipher_operation );
3753 }
3754 else
3755#endif
3756 {
3757 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003758 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003759 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003760 }
Moran Peker41deec42018-04-04 15:43:05 +03003761 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003762 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003763
David Brown63ca2602021-01-26 11:51:12 -07003764#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
3765 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003766 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003767 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003768 case PSA_ALG_CBC_NO_PADDING:
3769 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3770 MBEDTLS_PADDING_NONE );
3771 break;
3772 case PSA_ALG_CBC_PKCS7:
3773 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3774 MBEDTLS_PADDING_PKCS7 );
3775 break;
3776 default:
3777 /* The algorithm doesn't involve padding. */
3778 ret = 0;
3779 break;
3780 }
3781 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003782 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07003783#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02003784
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003785 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003786 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02003787 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
3788 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02003789 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003790 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003791 }
David Brown1bfe4d72021-02-16 12:54:35 -07003792#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02003793 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01003794 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02003795 operation->iv_size = 12;
3796#endif
mohammad1603503973b2018-03-12 15:59:30 +02003797
Steven Cooreman7df02922020-09-09 15:28:49 +02003798 status = PSA_SUCCESS;
3799
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003800exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003801 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003802 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02003803 if( status == PSA_SUCCESS )
3804 {
3805 /* Update operation flags for both driver and software implementations */
3806 operation->key_set = 1;
3807 }
3808 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003809 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003810
Ronald Cron5c522922020-11-14 16:35:34 +01003811 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003812
Ronald Cron5c522922020-11-14 16:35:34 +01003813 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003814}
3815
Gilles Peskinefe119512018-07-08 21:39:34 +02003816psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003817 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003818 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003819{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003820 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003821}
3822
Gilles Peskinefe119512018-07-08 21:39:34 +02003823psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003824 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003825 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003826{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003827 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003828}
3829
Gilles Peskinefe119512018-07-08 21:39:34 +02003830psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003831 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003832 size_t iv_size,
3833 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003834{
itayzafrir534bd7c2018-08-02 13:56:32 +03003835 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02003837 if( operation->iv_set || ! operation->iv_required )
3838 {
3839 return( PSA_ERROR_BAD_STATE );
3840 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003841
Steven Cooremanef8575e2020-09-11 11:44:50 +02003842 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02003843 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003844 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02003845 iv,
3846 iv_size,
3847 iv_length );
3848 goto exit;
3849 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003850
Moran Peker41deec42018-04-04 15:43:05 +03003851 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003852 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003853 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003854 goto exit;
3855 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003856 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003857 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003858 if( ret != 0 )
3859 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003860 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003861 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003862 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003863
mohammad16038481e742018-03-18 13:57:31 +02003864 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003865 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003866
Moran Peker395db872018-05-31 14:07:14 +03003867exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003868 if( status == PSA_SUCCESS )
3869 operation->iv_set = 1;
3870 else
Moran Peker395db872018-05-31 14:07:14 +03003871 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003872 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003873}
3874
Gilles Peskinefe119512018-07-08 21:39:34 +02003875psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003876 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003877 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003878{
itayzafrir534bd7c2018-08-02 13:56:32 +03003879 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02003881 if( operation->iv_set || ! operation->iv_required )
3882 {
3883 return( PSA_ERROR_BAD_STATE );
3884 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003885
Steven Cooremanef8575e2020-09-11 11:44:50 +02003886 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02003887 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003888 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02003889 iv,
3890 iv_length );
3891 goto exit;
3892 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003893
Moran Pekera28258c2018-05-29 16:25:04 +03003894 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003895 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003896 status = PSA_ERROR_INVALID_ARGUMENT;
3897 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003898 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003899 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3900 status = mbedtls_to_psa_error( ret );
3901exit:
3902 if( status == PSA_SUCCESS )
3903 operation->iv_set = 1;
3904 else
mohammad1603503973b2018-03-12 15:59:30 +02003905 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003906 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003907}
3908
Steven Cooreman177deba2020-09-07 17:14:14 +02003909/* Process input for which the algorithm is set to ECB mode. This requires
3910 * manual processing, since the PSA API is defined as being able to process
3911 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
3912 * underlying mbedtls_cipher_update only takes full blocks. */
3913static psa_status_t psa_cipher_update_ecb_internal(
3914 mbedtls_cipher_context_t *ctx,
3915 const uint8_t *input,
3916 size_t input_length,
3917 uint8_t *output,
3918 size_t output_size,
3919 size_t *output_length )
3920{
3921 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3922 size_t block_size = ctx->cipher_info->block_size;
3923 size_t internal_output_length = 0;
3924 *output_length = 0;
3925
3926 if( input_length == 0 )
3927 {
3928 status = PSA_SUCCESS;
3929 goto exit;
3930 }
3931
3932 if( ctx->unprocessed_len > 0 )
3933 {
3934 /* Fill up to block size, and run the block if there's a full one. */
3935 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
3936
3937 if( input_length < bytes_to_copy )
3938 bytes_to_copy = input_length;
3939
3940 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
3941 input, bytes_to_copy );
3942 input_length -= bytes_to_copy;
3943 input += bytes_to_copy;
3944 ctx->unprocessed_len += bytes_to_copy;
3945
3946 if( ctx->unprocessed_len == block_size )
3947 {
3948 status = mbedtls_to_psa_error(
3949 mbedtls_cipher_update( ctx,
3950 ctx->unprocessed_data,
3951 block_size,
3952 output, &internal_output_length ) );
3953
3954 if( status != PSA_SUCCESS )
3955 goto exit;
3956
3957 output += internal_output_length;
3958 output_size -= internal_output_length;
3959 *output_length += internal_output_length;
3960 ctx->unprocessed_len = 0;
3961 }
3962 }
3963
3964 while( input_length >= block_size )
3965 {
3966 /* Run all full blocks we have, one by one */
3967 status = mbedtls_to_psa_error(
3968 mbedtls_cipher_update( ctx, input,
3969 block_size,
3970 output, &internal_output_length ) );
3971
3972 if( status != PSA_SUCCESS )
3973 goto exit;
3974
3975 input_length -= block_size;
3976 input += block_size;
3977
3978 output += internal_output_length;
3979 output_size -= internal_output_length;
3980 *output_length += internal_output_length;
3981 }
3982
3983 if( input_length > 0 )
3984 {
3985 /* Save unprocessed bytes for later processing */
3986 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
3987 input, input_length );
3988 ctx->unprocessed_len += input_length;
3989 }
3990
3991 status = PSA_SUCCESS;
3992
3993exit:
3994 return( status );
3995}
3996
Gilles Peskinee553c652018-06-04 16:22:46 +02003997psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3998 const uint8_t *input,
3999 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004000 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004001 size_t output_size,
4002 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004003{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004004 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004005 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004006 if( operation->alg == 0 )
4007 {
4008 return( PSA_ERROR_BAD_STATE );
4009 }
4010 if( operation->iv_required && ! operation->iv_set )
4011 {
4012 return( PSA_ERROR_BAD_STATE );
4013 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004014
Steven Cooremanef8575e2020-09-11 11:44:50 +02004015 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004016 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004017 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004018 input,
4019 input_length,
4020 output,
4021 output_size,
4022 output_length );
4023 goto exit;
4024 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004025
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004026 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004027 {
4028 /* Take the unprocessed partial block left over from previous
4029 * update calls, if any, plus the input to this call. Remove
4030 * the last partial block, if any. You get the data that will be
4031 * output in this call. */
4032 expected_output_size =
4033 ( operation->ctx.cipher.unprocessed_len + input_length )
4034 / operation->block_size * operation->block_size;
4035 }
4036 else
4037 {
4038 expected_output_size = input_length;
4039 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004040
Gilles Peskine89d789c2018-06-04 17:17:16 +02004041 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004042 {
4043 status = PSA_ERROR_BUFFER_TOO_SMALL;
4044 goto exit;
4045 }
mohammad160382759612018-03-12 18:16:40 +02004046
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004047 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4048 {
4049 /* mbedtls_cipher_update has an API inconsistency: it will only
4050 * process a single block at a time in ECB mode. Abstract away that
4051 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004052 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4053 input,
4054 input_length,
4055 output,
4056 output_size,
4057 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004058 }
4059 else
4060 {
4061 status = mbedtls_to_psa_error(
4062 mbedtls_cipher_update( &operation->ctx.cipher, input,
4063 input_length, output, output_length ) );
4064 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004065exit:
4066 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004067 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004068 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004069}
4070
Gilles Peskinee553c652018-06-04 16:22:46 +02004071psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4072 uint8_t *output,
4073 size_t output_size,
4074 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004075{
David Saadab4ecc272019-02-14 13:48:10 +02004076 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004077 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004078 if( operation->alg == 0 )
4079 {
4080 return( PSA_ERROR_BAD_STATE );
4081 }
4082 if( operation->iv_required && ! operation->iv_set )
4083 {
4084 return( PSA_ERROR_BAD_STATE );
4085 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004086
Steven Cooremanef8575e2020-09-11 11:44:50 +02004087 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004088 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004089 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004090 output,
4091 output_size,
4092 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004093 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004094 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004095
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004096 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004097 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004098 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004099 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004100 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004101 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004102 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004103 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004104 }
4105
Steven Cooremanef8575e2020-09-11 11:44:50 +02004106 status = mbedtls_to_psa_error(
4107 mbedtls_cipher_finish( &operation->ctx.cipher,
4108 temp_output_buffer,
4109 output_length ) );
4110 if( status != PSA_SUCCESS )
4111 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004112
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004113 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004114 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004115 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004116 memcpy( output, temp_output_buffer, *output_length );
4117 else
Janos Follath315b51c2018-07-09 16:04:51 +01004118 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004119
Steven Cooremanef8575e2020-09-11 11:44:50 +02004120exit:
4121 if( operation->mbedtls_in_use == 1 )
4122 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004123
Steven Cooremanef8575e2020-09-11 11:44:50 +02004124 if( status == PSA_SUCCESS )
4125 return( psa_cipher_abort( operation ) );
4126 else
4127 {
4128 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004129 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004130
Steven Cooremanef8575e2020-09-11 11:44:50 +02004131 return( status );
4132 }
mohammad1603503973b2018-03-12 15:59:30 +02004133}
4134
Gilles Peskinee553c652018-06-04 16:22:46 +02004135psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4136{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004137 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004138 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004139 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004140 * in use. It's ok to call abort on such an object, and there's
4141 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004142 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004143 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004144
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004145 /* Sanity check (shouldn't happen: operation->alg should
4146 * always have been initialized to a valid value). */
4147 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4148 return( PSA_ERROR_BAD_STATE );
4149
Steven Cooremanef8575e2020-09-11 11:44:50 +02004150 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004151 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004152 else
4153 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004154
Moran Peker41deec42018-04-04 15:43:05 +03004155 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004156 operation->key_set = 0;
4157 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004158 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004159 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004160 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004161 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004162
Moran Peker395db872018-05-31 14:07:14 +03004163 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004164}
4165
Gilles Peskinea0655c32018-04-30 17:06:50 +02004166
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004167
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004168
mohammad16035955c982018-04-26 00:53:03 +03004169/****************************************************************/
4170/* AEAD */
4171/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004172
Gilles Peskineedf9a652018-08-17 18:11:56 +02004173typedef struct
4174{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004175 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004176 const mbedtls_cipher_info_t *cipher_info;
4177 union
4178 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004179 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004180#if defined(MBEDTLS_CCM_C)
4181 mbedtls_ccm_context ccm;
4182#endif /* MBEDTLS_CCM_C */
4183#if defined(MBEDTLS_GCM_C)
4184 mbedtls_gcm_context gcm;
4185#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004186#if defined(MBEDTLS_CHACHAPOLY_C)
4187 mbedtls_chachapoly_context chachapoly;
4188#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004189 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004190 psa_algorithm_t core_alg;
4191 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004192 uint8_t tag_length;
4193} aead_operation_t;
4194
Ronald Cronf95a2b12020-10-22 15:24:49 +02004195#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4196
Gilles Peskine30a9e412019-01-14 18:36:12 +01004197static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004198{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004199 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004200 {
4201#if defined(MBEDTLS_CCM_C)
4202 case PSA_ALG_CCM:
4203 mbedtls_ccm_free( &operation->ctx.ccm );
4204 break;
4205#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004206#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004207 case PSA_ALG_GCM:
4208 mbedtls_gcm_free( &operation->ctx.gcm );
4209 break;
4210#endif /* MBEDTLS_GCM_C */
4211 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004212
Ronald Cron5c522922020-11-14 16:35:34 +01004213 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004214}
4215
4216static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004217 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004218 psa_key_usage_t usage,
4219 psa_algorithm_t alg )
4220{
4221 psa_status_t status;
4222 size_t key_bits;
4223 mbedtls_cipher_id_t cipher_id;
4224
Ronald Cron5c522922020-11-14 16:35:34 +01004225 status = psa_get_and_lock_transparent_key_slot_with_policy(
4226 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004227 if( status != PSA_SUCCESS )
4228 return( status );
4229
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004230 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004231
4232 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004233 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004234 &cipher_id );
4235 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004236 {
4237 status = PSA_ERROR_NOT_SUPPORTED;
4238 goto cleanup;
4239 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004240
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004241 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004242 {
4243#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004244 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004245 operation->core_alg = PSA_ALG_CCM;
4246 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004247 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4248 * The call to mbedtls_ccm_encrypt_and_tag or
4249 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004250 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004251 {
4252 status = PSA_ERROR_INVALID_ARGUMENT;
4253 goto cleanup;
4254 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004255 mbedtls_ccm_init( &operation->ctx.ccm );
4256 status = mbedtls_to_psa_error(
4257 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004258 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004259 (unsigned int) key_bits ) );
4260 if( status != 0 )
4261 goto cleanup;
4262 break;
4263#endif /* MBEDTLS_CCM_C */
4264
4265#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004266 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004267 operation->core_alg = PSA_ALG_GCM;
4268 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004269 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4270 * The call to mbedtls_gcm_crypt_and_tag or
4271 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004272 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004273 {
4274 status = PSA_ERROR_INVALID_ARGUMENT;
4275 goto cleanup;
4276 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004277 mbedtls_gcm_init( &operation->ctx.gcm );
4278 status = mbedtls_to_psa_error(
4279 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004280 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004281 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004282 if( status != 0 )
4283 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004284 break;
4285#endif /* MBEDTLS_GCM_C */
4286
Gilles Peskine26869f22019-05-06 15:25:00 +02004287#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004288 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004289 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4290 operation->full_tag_length = 16;
4291 /* We only support the default tag length. */
4292 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004293 {
4294 status = PSA_ERROR_NOT_SUPPORTED;
4295 goto cleanup;
4296 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004297 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4298 status = mbedtls_to_psa_error(
4299 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004300 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004301 if( status != 0 )
4302 goto cleanup;
4303 break;
4304#endif /* MBEDTLS_CHACHAPOLY_C */
4305
Gilles Peskineedf9a652018-08-17 18:11:56 +02004306 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004307 status = PSA_ERROR_NOT_SUPPORTED;
4308 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004309 }
4310
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004311 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4312 {
4313 status = PSA_ERROR_INVALID_ARGUMENT;
4314 goto cleanup;
4315 }
4316 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004317
Gilles Peskineedf9a652018-08-17 18:11:56 +02004318 return( PSA_SUCCESS );
4319
4320cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004321 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004322 return( status );
4323}
4324
Ronald Croncf56a0a2020-08-04 09:51:30 +02004325psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004326 psa_algorithm_t alg,
4327 const uint8_t *nonce,
4328 size_t nonce_length,
4329 const uint8_t *additional_data,
4330 size_t additional_data_length,
4331 const uint8_t *plaintext,
4332 size_t plaintext_length,
4333 uint8_t *ciphertext,
4334 size_t ciphertext_size,
4335 size_t *ciphertext_length )
4336{
mohammad16035955c982018-04-26 00:53:03 +03004337 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004338 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004339 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004340
mohammad1603f08a5502018-06-03 15:05:47 +03004341 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004342
Ronald Croncf56a0a2020-08-04 09:51:30 +02004343 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004344 if( status != PSA_SUCCESS )
4345 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004346
Gilles Peskineedf9a652018-08-17 18:11:56 +02004347 /* For all currently supported modes, the tag is at the end of the
4348 * ciphertext. */
4349 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4350 {
4351 status = PSA_ERROR_BUFFER_TOO_SMALL;
4352 goto exit;
4353 }
4354 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004355
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004356#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004357 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004358 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004359 status = mbedtls_to_psa_error(
4360 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4361 MBEDTLS_GCM_ENCRYPT,
4362 plaintext_length,
4363 nonce, nonce_length,
4364 additional_data, additional_data_length,
4365 plaintext, ciphertext,
4366 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004367 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004368 else
4369#endif /* MBEDTLS_GCM_C */
4370#if defined(MBEDTLS_CCM_C)
4371 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004372 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004373 status = mbedtls_to_psa_error(
4374 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4375 plaintext_length,
4376 nonce, nonce_length,
4377 additional_data,
4378 additional_data_length,
4379 plaintext, ciphertext,
4380 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004381 }
mohammad16035c8845f2018-05-09 05:40:09 -07004382 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004383#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004384#if defined(MBEDTLS_CHACHAPOLY_C)
4385 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4386 {
4387 if( nonce_length != 12 || operation.tag_length != 16 )
4388 {
4389 status = PSA_ERROR_NOT_SUPPORTED;
4390 goto exit;
4391 }
4392 status = mbedtls_to_psa_error(
4393 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4394 plaintext_length,
4395 nonce,
4396 additional_data,
4397 additional_data_length,
4398 plaintext,
4399 ciphertext,
4400 tag ) );
4401 }
4402 else
4403#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004404 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004405 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004406 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004407 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004408
Gilles Peskineedf9a652018-08-17 18:11:56 +02004409 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4410 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004411
Gilles Peskineedf9a652018-08-17 18:11:56 +02004412exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004413 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004414 if( status == PSA_SUCCESS )
4415 *ciphertext_length = plaintext_length + operation.tag_length;
4416 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004417}
4418
Gilles Peskineee652a32018-06-01 19:23:52 +02004419/* Locate the tag in a ciphertext buffer containing the encrypted data
4420 * followed by the tag. Return the length of the part preceding the tag in
4421 * *plaintext_length. This is the size of the plaintext in modes where
4422 * the encrypted data has the same size as the plaintext, such as
4423 * CCM and GCM. */
4424static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4425 const uint8_t *ciphertext,
4426 size_t ciphertext_length,
4427 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004428 const uint8_t **p_tag )
4429{
4430 size_t payload_length;
4431 if( tag_length > ciphertext_length )
4432 return( PSA_ERROR_INVALID_ARGUMENT );
4433 payload_length = ciphertext_length - tag_length;
4434 if( payload_length > plaintext_size )
4435 return( PSA_ERROR_BUFFER_TOO_SMALL );
4436 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004437 return( PSA_SUCCESS );
4438}
4439
Ronald Croncf56a0a2020-08-04 09:51:30 +02004440psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004441 psa_algorithm_t alg,
4442 const uint8_t *nonce,
4443 size_t nonce_length,
4444 const uint8_t *additional_data,
4445 size_t additional_data_length,
4446 const uint8_t *ciphertext,
4447 size_t ciphertext_length,
4448 uint8_t *plaintext,
4449 size_t plaintext_size,
4450 size_t *plaintext_length )
4451{
mohammad16035955c982018-04-26 00:53:03 +03004452 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004453 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004454 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004455
Gilles Peskineee652a32018-06-01 19:23:52 +02004456 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004457
Ronald Croncf56a0a2020-08-04 09:51:30 +02004458 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004459 if( status != PSA_SUCCESS )
4460 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004461
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004462 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4463 ciphertext, ciphertext_length,
4464 plaintext_size, &tag );
4465 if( status != PSA_SUCCESS )
4466 goto exit;
4467
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004468#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004469 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004470 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004471 status = mbedtls_to_psa_error(
4472 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4473 ciphertext_length - operation.tag_length,
4474 nonce, nonce_length,
4475 additional_data,
4476 additional_data_length,
4477 tag, operation.tag_length,
4478 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004479 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004480 else
4481#endif /* MBEDTLS_GCM_C */
4482#if defined(MBEDTLS_CCM_C)
4483 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004484 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004485 status = mbedtls_to_psa_error(
4486 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4487 ciphertext_length - operation.tag_length,
4488 nonce, nonce_length,
4489 additional_data,
4490 additional_data_length,
4491 ciphertext, plaintext,
4492 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004493 }
mohammad160339574652018-06-01 04:39:53 -07004494 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004495#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004496#if defined(MBEDTLS_CHACHAPOLY_C)
4497 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4498 {
4499 if( nonce_length != 12 || operation.tag_length != 16 )
4500 {
4501 status = PSA_ERROR_NOT_SUPPORTED;
4502 goto exit;
4503 }
4504 status = mbedtls_to_psa_error(
4505 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4506 ciphertext_length - operation.tag_length,
4507 nonce,
4508 additional_data,
4509 additional_data_length,
4510 tag,
4511 ciphertext,
4512 plaintext ) );
4513 }
4514 else
4515#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004516 {
mohammad1603554faad2018-06-03 15:07:38 +03004517 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004518 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004519
Gilles Peskineedf9a652018-08-17 18:11:56 +02004520 if( status != PSA_SUCCESS && plaintext_size != 0 )
4521 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004522
Gilles Peskineedf9a652018-08-17 18:11:56 +02004523exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004524 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004525 if( status == PSA_SUCCESS )
4526 *plaintext_length = ciphertext_length - operation.tag_length;
4527 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004528}
4529
Gilles Peskinea0655c32018-04-30 17:06:50 +02004530
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004531
Gilles Peskinee59236f2018-01-27 23:32:46 +01004532/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004533/* Generators */
4534/****************************************************************/
4535
John Durkop07cc04a2020-11-16 22:08:34 -08004536#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4537 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4538 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4539#define AT_LEAST_ONE_BUILTIN_KDF
4540#endif
4541
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004542#define HKDF_STATE_INIT 0 /* no input yet */
4543#define HKDF_STATE_STARTED 1 /* got salt */
4544#define HKDF_STATE_KEYED 2 /* got key */
4545#define HKDF_STATE_OUTPUT 3 /* output started */
4546
Gilles Peskinecbe66502019-05-16 16:59:18 +02004547static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004548 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004549{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004550 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4551 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004552 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004553 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004554}
4555
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004556psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004557{
4558 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004559 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004560 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004561 {
4562 /* The object has (apparently) been initialized but it is not
4563 * in use. It's ok to call abort on such an object, and there's
4564 * nothing to do. */
4565 }
4566 else
John Durkopd0321952020-10-29 21:37:36 -07004567#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004568 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004569 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004570 mbedtls_free( operation->ctx.hkdf.info );
4571 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004572 }
John Durkop07cc04a2020-11-16 22:08:34 -08004573 else
4574#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4575#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4576 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4577 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004578 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004579 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004580 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004581 if( operation->ctx.tls12_prf.seed != NULL )
4582 {
4583 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4584 operation->ctx.tls12_prf.seed_length );
4585 mbedtls_free( operation->ctx.tls12_prf.seed );
4586 }
4587
4588 if( operation->ctx.tls12_prf.label != NULL )
4589 {
4590 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4591 operation->ctx.tls12_prf.label_length );
4592 mbedtls_free( operation->ctx.tls12_prf.label );
4593 }
4594
4595 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4596
4597 /* We leave the fields Ai and output_block to be erased safely by the
4598 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004599 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004600 else
John Durkop07cc04a2020-11-16 22:08:34 -08004601#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4602 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004603 {
4604 status = PSA_ERROR_BAD_STATE;
4605 }
Janos Follath083036a2019-06-11 10:22:26 +01004606 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004607 return( status );
4608}
4609
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004610psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004611 size_t *capacity)
4612{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004613 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004614 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004615 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004616 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004617 }
4618
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004619 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004620 return( PSA_SUCCESS );
4621}
4622
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004623psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004624 size_t capacity )
4625{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004626 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004627 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004628 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004629 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004630 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004631 return( PSA_SUCCESS );
4632}
4633
John Durkopd0321952020-10-29 21:37:36 -07004634#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004635/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004636 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004637static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004638 psa_algorithm_t hash_alg,
4639 uint8_t *output,
4640 size_t output_length )
4641{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004642 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004643 psa_status_t status;
4644
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004645 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4646 return( PSA_ERROR_BAD_STATE );
4647 hkdf->state = HKDF_STATE_OUTPUT;
4648
Gilles Peskinebef7f142018-07-12 17:22:21 +02004649 while( output_length != 0 )
4650 {
4651 /* Copy what remains of the current block */
4652 uint8_t n = hash_length - hkdf->offset_in_block;
4653 if( n > output_length )
4654 n = (uint8_t) output_length;
4655 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4656 output += n;
4657 output_length -= n;
4658 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004659 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004660 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004661 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004662 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004664 * object was corrupted or if this function is called directly
4665 * inside the library. */
4666 if( hkdf->block_number == 0xff )
4667 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004668
4669 /* We need a new block */
4670 ++hkdf->block_number;
4671 hkdf->offset_in_block = 0;
4672 status = psa_hmac_setup_internal( &hkdf->hmac,
4673 hkdf->prk, hash_length,
4674 hash_alg );
4675 if( status != PSA_SUCCESS )
4676 return( status );
4677 if( hkdf->block_number != 1 )
4678 {
4679 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4680 hkdf->output_block,
4681 hash_length );
4682 if( status != PSA_SUCCESS )
4683 return( status );
4684 }
4685 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4686 hkdf->info,
4687 hkdf->info_length );
4688 if( status != PSA_SUCCESS )
4689 return( status );
4690 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4691 &hkdf->block_number, 1 );
4692 if( status != PSA_SUCCESS )
4693 return( status );
4694 status = psa_hmac_finish_internal( &hkdf->hmac,
4695 hkdf->output_block,
4696 sizeof( hkdf->output_block ) );
4697 if( status != PSA_SUCCESS )
4698 return( status );
4699 }
4700
4701 return( PSA_SUCCESS );
4702}
John Durkop07cc04a2020-11-16 22:08:34 -08004703#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004704
John Durkop07cc04a2020-11-16 22:08:34 -08004705#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4706 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004707static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4708 psa_tls12_prf_key_derivation_t *tls12_prf,
4709 psa_algorithm_t alg )
4710{
4711 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004712 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004713 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4714 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004715
Janos Follath7742fee2019-06-17 12:58:10 +01004716 /* We can't be wanting more output after block 0xff, otherwise
4717 * the capacity check in psa_key_derivation_output_bytes() would have
4718 * prevented this call. It could happen only if the operation
4719 * object was corrupted or if this function is called directly
4720 * inside the library. */
4721 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004722 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004723
4724 /* We need a new block */
4725 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004726 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004727
4728 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4729 *
4730 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4731 *
4732 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4733 * HMAC_hash(secret, A(2) + seed) +
4734 * HMAC_hash(secret, A(3) + seed) + ...
4735 *
4736 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004737 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004738 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004739 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004740 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004741 * is currently extracted as `output_block` and where i is
4742 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004743 */
4744
Janos Follathea29bfb2019-06-19 12:21:20 +01004745 /* Save the hash context before using it, to preserve the hash state with
4746 * only the inner padding in it. We need this, because inner padding depends
4747 * on the key (secret in the RFC's terminology). */
4748 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4749 if( status != PSA_SUCCESS )
4750 goto cleanup;
4751
4752 /* Calculate A(i) where i = tls12_prf->block_number. */
4753 if( tls12_prf->block_number == 1 )
4754 {
4755 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4756 * the variable seed and in this instance means it in the context of the
4757 * P_hash function, where seed = label + seed.) */
4758 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4759 tls12_prf->label, tls12_prf->label_length );
4760 if( status != PSA_SUCCESS )
4761 goto cleanup;
4762 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4763 tls12_prf->seed, tls12_prf->seed_length );
4764 if( status != PSA_SUCCESS )
4765 goto cleanup;
4766 }
4767 else
4768 {
4769 /* A(i) = HMAC_hash(secret, A(i-1)) */
4770 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4771 tls12_prf->Ai, hash_length );
4772 if( status != PSA_SUCCESS )
4773 goto cleanup;
4774 }
4775
4776 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4777 tls12_prf->Ai, hash_length );
4778 if( status != PSA_SUCCESS )
4779 goto cleanup;
4780 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4781 if( status != PSA_SUCCESS )
4782 goto cleanup;
4783
4784 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4785 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4786 tls12_prf->Ai, hash_length );
4787 if( status != PSA_SUCCESS )
4788 goto cleanup;
4789 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4790 tls12_prf->label, tls12_prf->label_length );
4791 if( status != PSA_SUCCESS )
4792 goto cleanup;
4793 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4794 tls12_prf->seed, tls12_prf->seed_length );
4795 if( status != PSA_SUCCESS )
4796 goto cleanup;
4797 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4798 tls12_prf->output_block, hash_length );
4799 if( status != PSA_SUCCESS )
4800 goto cleanup;
4801 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4802 if( status != PSA_SUCCESS )
4803 goto cleanup;
4804
Janos Follath7742fee2019-06-17 12:58:10 +01004805
4806cleanup:
4807
Janos Follathea29bfb2019-06-19 12:21:20 +01004808 cleanup_status = psa_hash_abort( &backup );
4809 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4810 status = cleanup_status;
4811
4812 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004813}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004814
Janos Follath844eb0e2019-06-19 12:10:49 +01004815static psa_status_t psa_key_derivation_tls12_prf_read(
4816 psa_tls12_prf_key_derivation_t *tls12_prf,
4817 psa_algorithm_t alg,
4818 uint8_t *output,
4819 size_t output_length )
4820{
4821 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004822 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004823 psa_status_t status;
4824 uint8_t offset, length;
4825
4826 while( output_length != 0 )
4827 {
4828 /* Check if we have fully processed the current block. */
4829 if( tls12_prf->left_in_block == 0 )
4830 {
4831 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4832 alg );
4833 if( status != PSA_SUCCESS )
4834 return( status );
4835
4836 continue;
4837 }
4838
4839 if( tls12_prf->left_in_block > output_length )
4840 length = (uint8_t) output_length;
4841 else
4842 length = tls12_prf->left_in_block;
4843
4844 offset = hash_length - tls12_prf->left_in_block;
4845 memcpy( output, tls12_prf->output_block + offset, length );
4846 output += length;
4847 output_length -= length;
4848 tls12_prf->left_in_block -= length;
4849 }
4850
4851 return( PSA_SUCCESS );
4852}
John Durkop07cc04a2020-11-16 22:08:34 -08004853#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4854 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004855
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004856psa_status_t psa_key_derivation_output_bytes(
4857 psa_key_derivation_operation_t *operation,
4858 uint8_t *output,
4859 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004860{
4861 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004863
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004865 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004867 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004868 }
4869
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004871 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004872 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004873 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004874 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004875 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004876 goto exit;
4877 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004878 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004879 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004880 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004881 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004882 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4883 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004884 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004885 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004886 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004887 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004889
John Durkopd0321952020-10-29 21:37:36 -07004890#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004891 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004892 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004893 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004895 output, output_length );
4896 }
Janos Follathadbec812019-06-14 11:05:39 +01004897 else
John Durkop07cc04a2020-11-16 22:08:34 -08004898#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4899#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4900 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004901 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004902 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004903 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004906 output_length );
4907 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004908 else
John Durkop07cc04a2020-11-16 22:08:34 -08004909#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4910 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004911 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004912 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004913 return( PSA_ERROR_BAD_STATE );
4914 }
4915
4916exit:
4917 if( status != PSA_SUCCESS )
4918 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004919 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 * This allows us to differentiate between exhausted operations and
4921 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4922 * operations. */
4923 psa_algorithm_t alg = operation->alg;
4924 psa_key_derivation_abort( operation );
4925 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004926 memset( output, '!', output_length );
4927 }
4928 return( status );
4929}
4930
David Brown7807bf72021-02-09 16:28:23 -07004931#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004932static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4933{
4934 if( data_size >= 8 )
4935 mbedtls_des_key_set_parity( data );
4936 if( data_size >= 16 )
4937 mbedtls_des_key_set_parity( data + 8 );
4938 if( data_size >= 24 )
4939 mbedtls_des_key_set_parity( data + 16 );
4940}
David Brown7807bf72021-02-09 16:28:23 -07004941#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004942
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004943static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004944 psa_key_slot_t *slot,
4945 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004946 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004947{
4948 uint8_t *data = NULL;
4949 size_t bytes = PSA_BITS_TO_BYTES( bits );
4950 psa_status_t status;
4951
Gilles Peskine8e338702019-07-30 20:06:31 +02004952 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004953 return( PSA_ERROR_INVALID_ARGUMENT );
4954 if( bits % 8 != 0 )
4955 return( PSA_ERROR_INVALID_ARGUMENT );
4956 data = mbedtls_calloc( 1, bytes );
4957 if( data == NULL )
4958 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4959
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004961 if( status != PSA_SUCCESS )
4962 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004963#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004964 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004965 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07004966#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004967
4968 status = psa_allocate_buffer_to_slot( slot, bytes );
4969 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00004970 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01004971
Ronald Cronbf33c932020-11-28 18:06:53 +01004972 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004973 psa_key_attributes_t attributes = {
4974 .core = slot->attr
4975 };
4976
Ronald Cronbf33c932020-11-28 18:06:53 +01004977 status = psa_driver_wrapper_import_key( &attributes,
4978 data, bytes,
4979 slot->key.data,
4980 slot->key.bytes,
4981 &slot->key.bytes, &bits );
4982 if( bits != slot->attr.bits )
4983 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004984
4985exit:
4986 mbedtls_free( data );
4987 return( status );
4988}
4989
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004990psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004991 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004992 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004993{
4994 psa_status_t status;
4995 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004996 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004997
Ronald Cron81709fc2020-11-14 12:10:32 +01004998 *key = MBEDTLS_SVC_KEY_ID_INIT;
4999
Gilles Peskine0f84d622019-09-12 19:03:13 +02005000 /* Reject any attempt to create a zero-length key so that we don't
5001 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5002 if( psa_get_key_bits( attributes ) == 0 )
5003 return( PSA_ERROR_INVALID_ARGUMENT );
5004
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005005 if( ! operation->can_output_key )
5006 return( PSA_ERROR_NOT_PERMITTED );
5007
Ronald Cron81709fc2020-11-14 12:10:32 +01005008 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5009 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005010#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5011 if( driver != NULL )
5012 {
5013 /* Deriving a key in a secure element is not implemented yet. */
5014 status = PSA_ERROR_NOT_SUPPORTED;
5015 }
5016#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005017 if( status == PSA_SUCCESS )
5018 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005019 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005020 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005022 }
5023 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005024 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005025 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005026 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005027
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005028 return( status );
5029}
5030
Gilles Peskineeab56e42018-07-12 17:12:33 +02005031
5032
5033/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005034/* Key derivation */
5035/****************************************************************/
5036
Steven Cooreman932ffb72021-02-15 12:14:32 +01005037#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005038static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005039 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005040 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005041{
John Durkop07cc04a2020-11-16 22:08:34 -08005042 int is_kdf_alg_supported;
5043
Janos Follath5fe19732019-06-20 15:09:30 +01005044 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5045 * initialisers for this union leave some bytes unspecified.) */
5046 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5047
Gilles Peskine969c5d62019-01-16 15:53:06 +01005048 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005049#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005050 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5051 is_kdf_alg_supported = 1;
5052 else
5053#endif
5054#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5055 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5056 is_kdf_alg_supported = 1;
5057 else
5058#endif
5059#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5060 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5061 is_kdf_alg_supported = 1;
5062 else
5063#endif
5064 is_kdf_alg_supported = 0;
5065
5066 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005067 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005068 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005069 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005070 if( hash_size == 0 )
5071 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005072 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5073 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005074 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005075 {
5076 return( PSA_ERROR_NOT_SUPPORTED );
5077 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005078 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005079 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005080 }
John Durkop07cc04a2020-11-16 22:08:34 -08005081
5082 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005083}
John Durkop07cc04a2020-11-16 22:08:34 -08005084#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005085
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005087 psa_algorithm_t alg )
5088{
5089 psa_status_t status;
5090
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005092 return( PSA_ERROR_BAD_STATE );
5093
Gilles Peskine6843c292019-01-18 16:44:49 +01005094 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5095 return( PSA_ERROR_INVALID_ARGUMENT );
5096 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005097 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005098#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005099 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005100 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005101#else
5102 return( PSA_ERROR_NOT_SUPPORTED );
5103#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005104 }
5105 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5106 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005107#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005108 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005109#else
5110 return( PSA_ERROR_NOT_SUPPORTED );
5111#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005112 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005113 else
5114 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005115
5116 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005117 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005118 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005119}
5120
John Durkopd0321952020-10-29 21:37:36 -07005121#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005122static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005123 psa_algorithm_t hash_alg,
5124 psa_key_derivation_step_t step,
5125 const uint8_t *data,
5126 size_t data_length )
5127{
5128 psa_status_t status;
5129 switch( step )
5130 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005131 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005132 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005133 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005134 status = psa_hmac_setup_internal( &hkdf->hmac,
5135 data, data_length,
5136 hash_alg );
5137 if( status != PSA_SUCCESS )
5138 return( status );
5139 hkdf->state = HKDF_STATE_STARTED;
5140 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005141 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005142 /* If no salt was provided, use an empty salt. */
5143 if( hkdf->state == HKDF_STATE_INIT )
5144 {
5145 status = psa_hmac_setup_internal( &hkdf->hmac,
5146 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005147 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005148 if( status != PSA_SUCCESS )
5149 return( status );
5150 hkdf->state = HKDF_STATE_STARTED;
5151 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005152 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005153 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005154 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5155 data, data_length );
5156 if( status != PSA_SUCCESS )
5157 return( status );
5158 status = psa_hmac_finish_internal( &hkdf->hmac,
5159 hkdf->prk,
5160 sizeof( hkdf->prk ) );
5161 if( status != PSA_SUCCESS )
5162 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005163 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005164 hkdf->block_number = 0;
5165 hkdf->state = HKDF_STATE_KEYED;
5166 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005167 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005168 if( hkdf->state == HKDF_STATE_OUTPUT )
5169 return( PSA_ERROR_BAD_STATE );
5170 if( hkdf->info_set )
5171 return( PSA_ERROR_BAD_STATE );
5172 hkdf->info_length = data_length;
5173 if( data_length != 0 )
5174 {
5175 hkdf->info = mbedtls_calloc( 1, data_length );
5176 if( hkdf->info == NULL )
5177 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5178 memcpy( hkdf->info, data, data_length );
5179 }
5180 hkdf->info_set = 1;
5181 return( PSA_SUCCESS );
5182 default:
5183 return( PSA_ERROR_INVALID_ARGUMENT );
5184 }
5185}
John Durkop07cc04a2020-11-16 22:08:34 -08005186#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005187
John Durkop07cc04a2020-11-16 22:08:34 -08005188#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5189 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005190static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5191 const uint8_t *data,
5192 size_t data_length )
5193{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005194 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005195 return( PSA_ERROR_BAD_STATE );
5196
Janos Follathd6dce9f2019-07-04 09:11:38 +01005197 if( data_length != 0 )
5198 {
5199 prf->seed = mbedtls_calloc( 1, data_length );
5200 if( prf->seed == NULL )
5201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005202
Janos Follathd6dce9f2019-07-04 09:11:38 +01005203 memcpy( prf->seed, data, data_length );
5204 prf->seed_length = data_length;
5205 }
Janos Follathf08e2652019-06-13 09:05:41 +01005206
Gilles Peskine43f958b2020-12-13 14:55:14 +01005207 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005208
5209 return( PSA_SUCCESS );
5210}
5211
Janos Follath81550542019-06-13 14:26:34 +01005212static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5213 psa_algorithm_t hash_alg,
5214 const uint8_t *data,
5215 size_t data_length )
5216{
5217 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005218 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005219 return( PSA_ERROR_BAD_STATE );
5220
5221 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5222 if( status != PSA_SUCCESS )
5223 return( status );
5224
Gilles Peskine43f958b2020-12-13 14:55:14 +01005225 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005226
5227 return( PSA_SUCCESS );
5228}
5229
Janos Follath63028dd2019-06-13 09:15:47 +01005230static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5231 const uint8_t *data,
5232 size_t data_length )
5233{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005234 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005235 return( PSA_ERROR_BAD_STATE );
5236
Janos Follathd6dce9f2019-07-04 09:11:38 +01005237 if( data_length != 0 )
5238 {
5239 prf->label = mbedtls_calloc( 1, data_length );
5240 if( prf->label == NULL )
5241 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005242
Janos Follathd6dce9f2019-07-04 09:11:38 +01005243 memcpy( prf->label, data, data_length );
5244 prf->label_length = data_length;
5245 }
Janos Follath63028dd2019-06-13 09:15:47 +01005246
Gilles Peskine43f958b2020-12-13 14:55:14 +01005247 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005248
5249 return( PSA_SUCCESS );
5250}
5251
Janos Follathb03233e2019-06-11 15:30:30 +01005252static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5253 psa_algorithm_t hash_alg,
5254 psa_key_derivation_step_t step,
5255 const uint8_t *data,
5256 size_t data_length )
5257{
Janos Follathb03233e2019-06-11 15:30:30 +01005258 switch( step )
5259 {
Janos Follathf08e2652019-06-13 09:05:41 +01005260 case PSA_KEY_DERIVATION_INPUT_SEED:
5261 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005262 case PSA_KEY_DERIVATION_INPUT_SECRET:
5263 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005264 case PSA_KEY_DERIVATION_INPUT_LABEL:
5265 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005266 default:
5267 return( PSA_ERROR_INVALID_ARGUMENT );
5268 }
5269}
John Durkop07cc04a2020-11-16 22:08:34 -08005270#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5271 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5272
5273#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5274static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5275 psa_tls12_prf_key_derivation_t *prf,
5276 psa_algorithm_t hash_alg,
5277 const uint8_t *data,
5278 size_t data_length )
5279{
5280 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005281 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005282 uint8_t *cur = pms;
5283
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005284 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005285 return( PSA_ERROR_INVALID_ARGUMENT );
5286
5287 /* Quoting RFC 4279, Section 2:
5288 *
5289 * The premaster secret is formed as follows: if the PSK is N octets
5290 * long, concatenate a uint16 with the value N, N zero octets, a second
5291 * uint16 with the value N, and the PSK itself.
5292 */
5293
5294 *cur++ = ( data_length >> 8 ) & 0xff;
5295 *cur++ = ( data_length >> 0 ) & 0xff;
5296 memset( cur, 0, data_length );
5297 cur += data_length;
5298 *cur++ = pms[0];
5299 *cur++ = pms[1];
5300 memcpy( cur, data, data_length );
5301 cur += data_length;
5302
5303 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5304
5305 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5306 return( status );
5307}
Janos Follath6660f0e2019-06-17 08:44:03 +01005308
5309static psa_status_t psa_tls12_prf_psk_to_ms_input(
5310 psa_tls12_prf_key_derivation_t *prf,
5311 psa_algorithm_t hash_alg,
5312 psa_key_derivation_step_t step,
5313 const uint8_t *data,
5314 size_t data_length )
5315{
5316 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005317 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005318 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5319 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005320 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005321
5322 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5323}
John Durkop07cc04a2020-11-16 22:08:34 -08005324#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005325
Gilles Peskineb8965192019-09-24 16:21:10 +02005326/** Check whether the given key type is acceptable for the given
5327 * input step of a key derivation.
5328 *
5329 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5330 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5331 * Both secret and non-secret inputs can alternatively have the type
5332 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5333 * that the input was passed as a buffer rather than via a key object.
5334 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005335static int psa_key_derivation_check_input_type(
5336 psa_key_derivation_step_t step,
5337 psa_key_type_t key_type )
5338{
5339 switch( step )
5340 {
5341 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005342 if( key_type == PSA_KEY_TYPE_DERIVE )
5343 return( PSA_SUCCESS );
5344 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005345 return( PSA_SUCCESS );
5346 break;
5347 case PSA_KEY_DERIVATION_INPUT_LABEL:
5348 case PSA_KEY_DERIVATION_INPUT_SALT:
5349 case PSA_KEY_DERIVATION_INPUT_INFO:
5350 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005351 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5352 return( PSA_SUCCESS );
5353 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005354 return( PSA_SUCCESS );
5355 break;
5356 }
5357 return( PSA_ERROR_INVALID_ARGUMENT );
5358}
5359
Janos Follathb80a94e2019-06-12 15:54:46 +01005360static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005361 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005362 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005363 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005364 const uint8_t *data,
5365 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005366{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005367 psa_status_t status;
5368 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5369
5370 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005371 if( status != PSA_SUCCESS )
5372 goto exit;
5373
John Durkopd0321952020-10-29 21:37:36 -07005374#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005375 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005376 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005377 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005378 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005379 step, data, data_length );
5380 }
John Durkop07cc04a2020-11-16 22:08:34 -08005381 else
5382#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5383#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5384 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005385 {
Janos Follathb03233e2019-06-11 15:30:30 +01005386 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5387 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5388 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005389 }
John Durkop07cc04a2020-11-16 22:08:34 -08005390 else
5391#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5392#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5393 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005394 {
5395 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5396 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5397 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005398 }
5399 else
John Durkop07cc04a2020-11-16 22:08:34 -08005400#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005401 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005402 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005403 (void) data;
5404 (void) data_length;
5405 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005406 return( PSA_ERROR_BAD_STATE );
5407 }
5408
Gilles Peskine224b0d62019-09-23 18:13:17 +02005409exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005410 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005411 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005412 return( status );
5413}
5414
Janos Follath51f4a0f2019-06-14 11:35:55 +01005415psa_status_t psa_key_derivation_input_bytes(
5416 psa_key_derivation_operation_t *operation,
5417 psa_key_derivation_step_t step,
5418 const uint8_t *data,
5419 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005420{
Gilles Peskineb8965192019-09-24 16:21:10 +02005421 return( psa_key_derivation_input_internal( operation, step,
5422 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005423 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005424}
5425
Janos Follath51f4a0f2019-06-14 11:35:55 +01005426psa_status_t psa_key_derivation_input_key(
5427 psa_key_derivation_operation_t *operation,
5428 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005429 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005430{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005432 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005433 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005434
Ronald Cron5c522922020-11-14 16:35:34 +01005435 status = psa_get_and_lock_transparent_key_slot_with_policy(
5436 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005437 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005438 {
5439 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005440 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005441 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005442
5443 /* Passing a key object as a SECRET input unlocks the permission
5444 * to output to a key object. */
5445 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5446 operation->can_output_key = 1;
5447
Ronald Cronf95a2b12020-10-22 15:24:49 +02005448 status = psa_key_derivation_input_internal( operation,
5449 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005450 slot->key.data,
5451 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005452
Ronald Cron5c522922020-11-14 16:35:34 +01005453 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005454
Ronald Cron5c522922020-11-14 16:35:34 +01005455 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005456}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005457
5458
5459
5460/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005461/* Key agreement */
5462/****************************************************************/
5463
John Durkop6ba40d12020-11-10 08:50:04 -08005464#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005465static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5466 size_t peer_key_length,
5467 const mbedtls_ecp_keypair *our_key,
5468 uint8_t *shared_secret,
5469 size_t shared_secret_size,
5470 size_t *shared_secret_length )
5471{
Steven Cooremand4867872020-08-05 16:31:39 +02005472 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005473 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005474 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005475 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005476 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005477 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005478
Ronald Cron90857082020-11-25 14:58:33 +01005479 status = mbedtls_psa_ecp_load_representation(
5480 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005481 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005482 peer_key,
5483 peer_key_length,
5484 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005485 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005486 goto exit;
5487
Jaeden Amero97271b32019-01-10 19:38:51 +00005488 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005489 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005490 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005491 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005492 status = mbedtls_to_psa_error(
5493 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5494 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005495 goto exit;
5496
Jaeden Amero97271b32019-01-10 19:38:51 +00005497 status = mbedtls_to_psa_error(
5498 mbedtls_ecdh_calc_secret( &ecdh,
5499 shared_secret_length,
5500 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005501 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005502 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005503 if( status != PSA_SUCCESS )
5504 goto exit;
5505 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5506 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005507
5508exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005509 if( status != PSA_SUCCESS )
5510 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005511 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005512 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005513 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005514
Jaeden Amero97271b32019-01-10 19:38:51 +00005515 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005516}
John Durkop6ba40d12020-11-10 08:50:04 -08005517#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005518
Gilles Peskine01d718c2018-09-18 12:01:02 +02005519#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5520
Gilles Peskine0216fe12019-04-11 21:23:21 +02005521static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5522 psa_key_slot_t *private_key,
5523 const uint8_t *peer_key,
5524 size_t peer_key_length,
5525 uint8_t *shared_secret,
5526 size_t shared_secret_size,
5527 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005528{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005529 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005530 {
John Durkop6ba40d12020-11-10 08:50:04 -08005531#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005532 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005533 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005534 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005535 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005536 psa_status_t status = mbedtls_psa_ecp_load_representation(
5537 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005538 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005539 private_key->key.data,
5540 private_key->key.bytes,
5541 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005542 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005543 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005544 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005545 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005546 shared_secret, shared_secret_size,
5547 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005548 mbedtls_ecp_keypair_free( ecp );
5549 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005550 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005551#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005552 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005553 (void) private_key;
5554 (void) peer_key;
5555 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005556 (void) shared_secret;
5557 (void) shared_secret_size;
5558 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005559 return( PSA_ERROR_NOT_SUPPORTED );
5560 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005561}
5562
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005563/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005564 * to potentially free embedded data structures and wipe confidential data.
5565 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005566static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005567 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005568 psa_key_slot_t *private_key,
5569 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005570 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005571{
5572 psa_status_t status;
5573 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5574 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005575 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005576
5577 /* Step 1: run the secret agreement algorithm to generate the shared
5578 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005579 status = psa_key_agreement_raw_internal( ka_alg,
5580 private_key,
5581 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005582 shared_secret,
5583 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005584 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005585 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005586 goto exit;
5587
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005588 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005589 * the shared secret. A shared secret is permitted wherever a key
5590 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005591 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005592 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005593 shared_secret,
5594 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005595exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005596 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005597 return( status );
5598}
5599
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005600psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005601 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005602 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005603 const uint8_t *peer_key,
5604 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005605{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005606 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005607 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005608 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005609
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005610 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005611 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005612 status = psa_get_and_lock_transparent_key_slot_with_policy(
5613 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005614 if( status != PSA_SUCCESS )
5615 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005616 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005617 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005618 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005619 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005620 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005621 else
5622 {
5623 /* If a private key has been added as SECRET, we allow the derived
5624 * key material to be used as a key in PSA Crypto. */
5625 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5626 operation->can_output_key = 1;
5627 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005628
Ronald Cron5c522922020-11-14 16:35:34 +01005629 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005630
Ronald Cron5c522922020-11-14 16:35:34 +01005631 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005632}
5633
Gilles Peskinebe697d82019-05-16 18:00:41 +02005634psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005635 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005636 const uint8_t *peer_key,
5637 size_t peer_key_length,
5638 uint8_t *output,
5639 size_t output_size,
5640 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005641{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005642 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005643 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005644 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005645
5646 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5647 {
5648 status = PSA_ERROR_INVALID_ARGUMENT;
5649 goto exit;
5650 }
Ronald Cron5c522922020-11-14 16:35:34 +01005651 status = psa_get_and_lock_transparent_key_slot_with_policy(
5652 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005653 if( status != PSA_SUCCESS )
5654 goto exit;
5655
5656 status = psa_key_agreement_raw_internal( alg, slot,
5657 peer_key, peer_key_length,
5658 output, output_size,
5659 output_length );
5660
5661exit:
5662 if( status != PSA_SUCCESS )
5663 {
5664 /* If an error happens and is not handled properly, the output
5665 * may be used as a key to protect sensitive data. Arrange for such
5666 * a key to be random, which is likely to result in decryption or
5667 * verification errors. This is better than filling the buffer with
5668 * some constant data such as zeros, which would result in the data
5669 * being protected with a reproducible, easily knowable key.
5670 */
5671 psa_generate_random( output, output_size );
5672 *output_length = output_size;
5673 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005674
Ronald Cron5c522922020-11-14 16:35:34 +01005675 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005676
Ronald Cron5c522922020-11-14 16:35:34 +01005677 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005678}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005679
5680
Gilles Peskine30524eb2020-11-13 17:02:26 +01005681
Gilles Peskine86a440b2018-11-12 18:39:40 +01005682/****************************************************************/
5683/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005684/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005685
Gilles Peskine30524eb2020-11-13 17:02:26 +01005686/** Initialize the PSA random generator.
5687 */
5688static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5689{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005690#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5691 memset( rng, 0, sizeof( *rng ) );
5692#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5693
Gilles Peskine30524eb2020-11-13 17:02:26 +01005694 /* Set default configuration if
5695 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5696 if( rng->entropy_init == NULL )
5697 rng->entropy_init = mbedtls_entropy_init;
5698 if( rng->entropy_free == NULL )
5699 rng->entropy_free = mbedtls_entropy_free;
5700
5701 rng->entropy_init( &rng->entropy );
5702#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5703 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5704 /* The PSA entropy injection feature depends on using NV seed as an entropy
5705 * source. Add NV seed as an entropy source for PSA entropy injection. */
5706 mbedtls_entropy_add_source( &rng->entropy,
5707 mbedtls_nv_seed_poll, NULL,
5708 MBEDTLS_ENTROPY_BLOCK_SIZE,
5709 MBEDTLS_ENTROPY_SOURCE_STRONG );
5710#endif
5711
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005712 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005713#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005714}
5715
5716/** Deinitialize the PSA random generator.
5717 */
5718static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5719{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005720#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5721 memset( rng, 0, sizeof( *rng ) );
5722#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005723 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005724 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005725#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005726}
5727
5728/** Seed the PSA random generator.
5729 */
5730static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5731{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005732#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5733 /* Do nothing: the external RNG seeds itself. */
5734 (void) rng;
5735 return( PSA_SUCCESS );
5736#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005737 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005738 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5739 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005740 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005741#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005742}
5743
Gilles Peskinee59236f2018-01-27 23:32:46 +01005744psa_status_t psa_generate_random( uint8_t *output,
5745 size_t output_size )
5746{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005747 GUARD_MODULE_INITIALIZED;
5748
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005749#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5750
5751 size_t output_length = 0;
5752 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5753 output, output_size,
5754 &output_length );
5755 if( status != PSA_SUCCESS )
5756 return( status );
5757 /* Breaking up a request into smaller chunks is currently not supported
5758 * for the extrernal RNG interface. */
5759 if( output_length != output_size )
5760 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5761 return( PSA_SUCCESS );
5762
5763#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5764
Gilles Peskine71ddab92021-01-04 21:01:07 +01005765 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005766 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005767 size_t request_size =
5768 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5769 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5770 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005771 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
5772 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02005773 if( ret != 0 )
5774 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01005775 output_size -= request_size;
5776 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005777 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005778 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005779#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005780}
5781
Gilles Peskine8814fc42020-12-14 15:33:44 +01005782/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005783 *
5784 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5785 * `psa_generate_random(...)`. The state parameter is ignored since the
5786 * PSA API doesn't support passing an explicit state.
5787 *
5788 * In the non-external case, psa_generate_random() calls an
5789 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5790 * and semantics as mbedtls_psa_get_random(). As an optimization,
5791 * instead of doing this back-and-forth between the PSA API and the
5792 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5793 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005794 */
5795#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5796int mbedtls_psa_get_random( void *p_rng,
5797 unsigned char *output,
5798 size_t output_size )
5799{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005800 /* This function takes a pointer to the RNG state because that's what
5801 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5802 * its own state internally and doesn't let the caller access that state.
5803 * So we just ignore the state parameter, and in practice we'll pass
5804 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005805 (void) p_rng;
5806 psa_status_t status = psa_generate_random( output, output_size );
5807 if( status == PSA_SUCCESS )
5808 return( 0 );
5809 else
5810 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
5811}
5812#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5813
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005814#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5815#include "mbedtls/entropy_poll.h"
5816
Gilles Peskine7228da22019-07-15 11:06:15 +02005817psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005818 size_t seed_size )
5819{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005820 if( global_data.initialized )
5821 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005822
5823 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5824 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5825 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5826 return( PSA_ERROR_INVALID_ARGUMENT );
5827
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005828 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005829}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005830#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005831
Ronald Cron3772afe2021-02-08 16:10:05 +01005832/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005833 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005834 * \param type The key type
5835 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005836 *
5837 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005838 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005839 * \retval #PSA_ERROR_INVALID_ARGUMENT
5840 * The size in bits of the key is not valid.
5841 * \retval #PSA_ERROR_NOT_SUPPORTED
5842 * The type and/or the size in bits of the key or the combination of
5843 * the two is not supported.
5844 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005845static psa_status_t psa_validate_key_type_and_size_for_key_generation(
5846 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005847{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005849
Gilles Peskinee59236f2018-01-27 23:32:46 +01005850 if( key_type_is_raw_bytes( type ) )
5851 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02005852 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005853 if( status != PSA_SUCCESS )
5854 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005855 }
5856 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005857#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005858 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5859 {
5860 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5861 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005862
Ronald Cron01b2aba2020-10-05 09:42:02 +02005863 /* Accept only byte-aligned keys, for the same reasons as
5864 * in psa_import_rsa_key(). */
5865 if( bits % 8 != 0 )
5866 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005867 }
5868 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005869#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005870
Ronald Cron5c4d3862020-12-07 11:07:24 +01005871#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005872 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5873 {
Ronald Crond81ab562021-02-16 09:01:16 +01005874 /* To avoid empty block, return successfully here. */
5875 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005876 }
5877 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005878#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005879 {
5880 return( PSA_ERROR_NOT_SUPPORTED );
5881 }
5882
5883 return( PSA_SUCCESS );
5884}
5885
Ronald Cron55ed0592020-10-05 10:30:40 +02005886psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005887 const psa_key_attributes_t *attributes,
5888 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005889{
5890 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005891 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005892
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005893 if( ( attributes->domain_parameters == NULL ) &&
5894 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005895 return( PSA_ERROR_INVALID_ARGUMENT );
5896
Ronald Cron01b2aba2020-10-05 09:42:02 +02005897 if( key_type_is_raw_bytes( type ) )
5898 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005899 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005900 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005901 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005902
David Brown12ca5032021-02-11 11:02:00 -07005903#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005904 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005905 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07005906#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005907 }
5908 else
5909
John Durkop07cc04a2020-11-16 22:08:34 -08005910#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005911 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005912 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01005913 return( mbedtls_psa_rsa_generate_key( attributes,
5914 key_buffer,
5915 key_buffer_size,
5916 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005917 }
5918 else
John Durkop07cc04a2020-11-16 22:08:34 -08005919#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005920
John Durkop9814fa22020-11-04 12:28:15 -08005921#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005922 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005923 {
Ronald Cron7023db52020-11-20 18:17:42 +01005924 return( mbedtls_psa_ecp_generate_key( attributes,
5925 key_buffer,
5926 key_buffer_size,
5927 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005928 }
5929 else
John Durkop9814fa22020-11-04 12:28:15 -08005930#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005931 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005932 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005933 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005934 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005935
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005936 return( PSA_SUCCESS );
5937}
Darryl Green0c6575a2018-11-07 16:05:30 +00005938
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005939psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005940 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005941{
5942 psa_status_t status;
5943 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005944 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005945 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005946
Ronald Cron81709fc2020-11-14 12:10:32 +01005947 *key = MBEDTLS_SVC_KEY_ID_INIT;
5948
Gilles Peskine0f84d622019-09-12 19:03:13 +02005949 /* Reject any attempt to create a zero-length key so that we don't
5950 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5951 if( psa_get_key_bits( attributes ) == 0 )
5952 return( PSA_ERROR_INVALID_ARGUMENT );
5953
Ronald Cron81709fc2020-11-14 12:10:32 +01005954 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
5955 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005956 if( status != PSA_SUCCESS )
5957 goto exit;
5958
Ronald Cron977c2472020-10-13 08:32:21 +02005959 /* In the case of a transparent key or an opaque key stored in local
5960 * storage (thus not in the case of generating a key in a secure element
5961 * or cryptoprocessor with storage), we have to allocate a buffer to
5962 * hold the generated key material. */
5963 if( slot->key.data == NULL )
5964 {
5965 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
5966 PSA_KEY_LOCATION_LOCAL_STORAGE )
5967 {
Ronald Cron3772afe2021-02-08 16:10:05 +01005968 status = psa_validate_key_type_and_size_for_key_generation(
5969 attributes->core.type, attributes->core.bits );
5970 if( status != PSA_SUCCESS )
5971 goto exit;
5972
5973 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
5974 attributes->core.type,
5975 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02005976 }
5977 else
5978 {
5979 status = psa_driver_wrapper_get_key_buffer_size(
5980 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01005981 if( status != PSA_SUCCESS )
5982 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02005983 }
Ronald Cron977c2472020-10-13 08:32:21 +02005984
5985 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
5986 if( status != PSA_SUCCESS )
5987 goto exit;
5988 }
5989
Steven Cooreman2a1664c2020-07-20 15:33:08 +02005990 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02005991 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02005992
Ronald Cron2b56bc82020-10-05 10:02:26 +02005993 if( status != PSA_SUCCESS )
5994 psa_remove_key_data_from_memory( slot );
5995
Gilles Peskine11792082019-08-06 18:36:36 +02005996exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005997 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005998 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005999 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006000 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006001
Darryl Green0c6575a2018-11-07 16:05:30 +00006002 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006003}
6004
Gilles Peskinee59236f2018-01-27 23:32:46 +01006005/****************************************************************/
6006/* Module setup */
6007/****************************************************************/
6008
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006009#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006010psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6011 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6012 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6013{
6014 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6015 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006016 global_data.rng.entropy_init = entropy_init;
6017 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006018 return( PSA_SUCCESS );
6019}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006020#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006021
Gilles Peskinee59236f2018-01-27 23:32:46 +01006022void mbedtls_psa_crypto_free( void )
6023{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006024 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006025 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6026 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006027 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006028 }
6029 /* Wipe all remaining data, including configuration.
6030 * In particular, this sets all state indicator to the value
6031 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006032 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006033#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006034 /* Unregister all secure element drivers, so that we restart from
6035 * a pristine state. */
6036 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006037#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006038}
6039
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006040#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6041/** Recover a transaction that was interrupted by a power failure.
6042 *
6043 * This function is called during initialization, before psa_crypto_init()
6044 * returns. If this function returns a failure status, the initialization
6045 * fails.
6046 */
6047static psa_status_t psa_crypto_recover_transaction(
6048 const psa_crypto_transaction_t *transaction )
6049{
6050 switch( transaction->unknown.type )
6051 {
6052 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6053 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006054 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006055 * is implemented.
6056 * https://github.com/ARMmbed/mbed-crypto/issues/218
6057 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006058 default:
6059 /* We found an unsupported transaction in the storage.
6060 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006061 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006062 }
6063}
6064#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6065
Gilles Peskinee59236f2018-01-27 23:32:46 +01006066psa_status_t psa_crypto_init( void )
6067{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006068 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006069
Gilles Peskinec6b69072018-11-20 21:42:52 +01006070 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006071 if( global_data.initialized != 0 )
6072 return( PSA_SUCCESS );
6073
Gilles Peskine30524eb2020-11-13 17:02:26 +01006074 /* Initialize and seed the random generator. */
6075 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006076 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006077 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006078 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006079 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006080 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006081
Gilles Peskine66fb1262018-12-10 16:29:04 +01006082 status = psa_initialize_key_slots( );
6083 if( status != PSA_SUCCESS )
6084 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006085
Gilles Peskined9348f22019-10-01 15:22:29 +02006086#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6087 status = psa_init_all_se_drivers( );
6088 if( status != PSA_SUCCESS )
6089 goto exit;
6090#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6091
Gilles Peskine4b734222019-07-24 15:56:31 +02006092#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006093 status = psa_crypto_load_transaction( );
6094 if( status == PSA_SUCCESS )
6095 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006096 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6097 if( status != PSA_SUCCESS )
6098 goto exit;
6099 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006100 }
6101 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6102 {
6103 /* There's no transaction to complete. It's all good. */
6104 status = PSA_SUCCESS;
6105 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006106#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006107
Gilles Peskinec6b69072018-11-20 21:42:52 +01006108 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006109 global_data.initialized = 1;
6110
6111exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006112 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006113 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006114 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006115}
6116
6117#endif /* MBEDTLS_PSA_CRYPTO_C */