blob: e79f949e1af32648905b93aa6db7cd95bd0d25d2 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
Gilles Peskinee59236f2018-01-27 23:32:46 +010029#include "psa/crypto.h"
Przemyslaw Stekiel6aee9642021-11-24 08:47:29 +010030#include "psa/crypto_values.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031
Ronald Crond6d28882020-12-14 14:56:02 +010032#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020035#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010037#include "psa_crypto_hash.h"
Steven Cooreman82c66b62021-03-19 17:39:17 +010038#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010039#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010040#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020041#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020042#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020043#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010044#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010045/* Include internal declarations that are useful for implementing persistently
46 * stored keys. */
47#include "psa_crypto_storage.h"
48
Gilles Peskineb2b64d32020-12-14 16:43:58 +010049#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010050
Gilles Peskineb46bef22019-07-30 21:32:04 +020051#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020055#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056#define mbedtls_calloc calloc
57#define mbedtls_free free
58#endif
59
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010060#include "mbedtls/aes.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020061#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000062#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020063#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020065#include "mbedtls/chacha20.h"
66#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/cipher.h"
68#include "mbedtls/ccm.h"
69#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020071#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010073#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/error.h"
75#include "mbedtls/gcm.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010077#include "mbedtls/md.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000078#include "md_wrap.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010079#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000080#include "pk_wrap.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010081#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000082#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010084#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085#include "mbedtls/sha1.h"
86#include "mbedtls/sha256.h"
87#include "mbedtls/sha512.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088
Gilles Peskine996deb12018-08-01 15:45:45 +020089#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
90
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010091/****************************************************************/
92/* Global data, support functions and library management */
93/****************************************************************/
94
Gilles Peskine48c0ea12018-06-21 14:15:31 +020095static int key_type_is_raw_bytes( psa_key_type_t type )
96{
Gilles Peskine78b3bb62018-08-10 16:03:41 +020097 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +020098}
99
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100100/* Values for psa_global_data_t::rng_state */
101#define RNG_NOT_INITIALIZED 0
102#define RNG_INITIALIZED 1
103#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100104
Gilles Peskine2d277862018-06-18 15:41:12 +0200105typedef struct
106{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100107 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100109 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100110} psa_global_data_t;
111
112static psa_global_data_t global_data;
113
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100114#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
115mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
116 &global_data.rng.drbg;
117#endif
118
itayzafrir0adf0fc2018-09-06 16:24:41 +0300119#define GUARD_MODULE_INITIALIZED \
120 if( global_data.initialized == 0 ) \
121 return( PSA_ERROR_BAD_STATE );
122
Steven Cooreman01164162020-07-20 15:31:37 +0200123psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100124{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100125 /* Mbed TLS error codes can combine a high-level error code and a
126 * low-level error code. The low-level error usually reflects the
127 * root cause better, so dispatch on that preferably. */
128 int low_level_ret = - ( -ret & 0x007f );
129 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100130 {
131 case 0:
132 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100133
134 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
135 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
Gilles Peskinea5905292018-02-07 20:59:33 +0100136 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine9a944802018-06-21 09:35:35 +0200137 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
138 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
139 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
140 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
141 case MBEDTLS_ERR_ASN1_INVALID_DATA:
142 return( PSA_ERROR_INVALID_ARGUMENT );
143 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
144 return( PSA_ERROR_INSUFFICIENT_MEMORY );
145 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
146 return( PSA_ERROR_BUFFER_TOO_SMALL );
147
Jaeden Amero93e21112019-02-20 13:57:28 +0000148#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000149 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000150#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100151 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
152 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100153
154 case MBEDTLS_ERR_CCM_BAD_INPUT:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_CCM_AUTH_FAILED:
157 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100158
Gilles Peskine26869f22019-05-06 15:25:00 +0200159 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
160 return( PSA_ERROR_INVALID_ARGUMENT );
161
162 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
163 return( PSA_ERROR_BAD_STATE );
164 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
165 return( PSA_ERROR_INVALID_SIGNATURE );
166
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
170 return( PSA_ERROR_INVALID_ARGUMENT );
171 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
172 return( PSA_ERROR_INSUFFICIENT_MEMORY );
173 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
174 return( PSA_ERROR_INVALID_PADDING );
175 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200176 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
178 return( PSA_ERROR_INVALID_SIGNATURE );
179 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200180 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100181
Gilles Peskine82e57d12020-11-13 21:31:17 +0100182#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
183 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100184 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
185 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
187 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
188 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
189 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
190 return( PSA_ERROR_NOT_SUPPORTED );
191 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
192 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100193#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100194
195 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
196 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100197
Gilles Peskinee59236f2018-01-27 23:32:46 +0100198 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
199 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
200 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100202
203 case MBEDTLS_ERR_GCM_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
Mateusz Starzykf28261f2021-09-30 16:39:07 +0200205 case MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL:
206 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200208 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100209
Gilles Peskine82e57d12020-11-13 21:31:17 +0100210#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
211 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100212 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
213 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100214 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
216 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
218 return( PSA_ERROR_NOT_SUPPORTED );
219 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
221#endif
222
Gilles Peskinea5905292018-02-07 20:59:33 +0100223 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
226 return( PSA_ERROR_INVALID_ARGUMENT );
227 case MBEDTLS_ERR_MD_ALLOC_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_MEMORY );
229 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
230 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
Gilles Peskinef76aa772018-10-29 19:24:33 +0100232 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
233 return( PSA_ERROR_STORAGE_FAILURE );
234 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
235 return( PSA_ERROR_INVALID_ARGUMENT );
236 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
237 return( PSA_ERROR_INVALID_ARGUMENT );
238 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
239 return( PSA_ERROR_BUFFER_TOO_SMALL );
240 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
241 return( PSA_ERROR_INVALID_ARGUMENT );
242 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
243 return( PSA_ERROR_INVALID_ARGUMENT );
244 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
245 return( PSA_ERROR_INVALID_ARGUMENT );
246 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_MEMORY );
248
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100249 case MBEDTLS_ERR_PK_ALLOC_FAILED:
250 return( PSA_ERROR_INSUFFICIENT_MEMORY );
251 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
252 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100256 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
257 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
260 return( PSA_ERROR_NOT_SUPPORTED );
261 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
262 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
263 return( PSA_ERROR_NOT_PERMITTED );
264 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_PK_INVALID_ALG:
267 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
268 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
269 return( PSA_ERROR_NOT_SUPPORTED );
270 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
271 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinef9f1bdf2021-06-23 20:32:27 +0200272 case MBEDTLS_ERR_PK_BUFFER_TOO_SMALL:
273 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea5905292018-02-07 20:59:33 +0100274
Gilles Peskineff2d2002019-05-06 15:26:23 +0200275 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
276 return( PSA_ERROR_HARDWARE_FAILURE );
277 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
278 return( PSA_ERROR_NOT_SUPPORTED );
279
Gilles Peskinea5905292018-02-07 20:59:33 +0100280 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_RSA_INVALID_PADDING:
283 return( PSA_ERROR_INVALID_PADDING );
284 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
289 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200290 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100291 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
292 return( PSA_ERROR_INVALID_SIGNATURE );
293 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
294 return( PSA_ERROR_BUFFER_TOO_SMALL );
295 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100296 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200297
itayzafrir5c753392018-05-08 11:18:38 +0300298 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300299 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300300 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300301 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
302 return( PSA_ERROR_BUFFER_TOO_SMALL );
303 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
304 return( PSA_ERROR_NOT_SUPPORTED );
305 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
306 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
307 return( PSA_ERROR_INVALID_SIGNATURE );
308 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
309 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100310 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
311 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100312
Janos Follatha13b9052019-11-22 12:48:59 +0000313 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
314 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300315
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316 default:
David Saadab4ecc272019-02-14 13:48:10 +0200317 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100318 }
319}
320
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200321
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200322
323
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324/****************************************************************/
325/* Key management */
326/****************************************************************/
327
John Durkop07cc04a2020-11-16 22:08:34 -0800328/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
329 * current test driver in key_management.c is using this function
330 * when accelerators are used for ECC key pair and public key.
331 * Once that dependency is resolved these guards can be removed.
332 */
John Durkop9814fa22020-11-04 12:28:15 -0800333#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800334 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
335 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
336 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100337mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100338 size_t bits,
339 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200340{
341 switch( curve )
342 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100343 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100344 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100345 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100346#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100347 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100348 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100349#endif
350#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100351 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100352 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100353#endif
354#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100355 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100356 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100357#endif
358#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100359 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100360 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100361#endif
362#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100363 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100364 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100365 case 528:
366 if( bits_is_sloppy )
367 return( MBEDTLS_ECP_DP_SECP521R1 );
368 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100369#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100370 }
371 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100372
Paul Elliott8ff510a2020-06-02 17:19:28 +0100373 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100374 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100375 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100376#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100377 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100378 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100379#endif
380#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100381 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100382 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100383#endif
384#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100385 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100386 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100387#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100388 }
389 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100390
Paul Elliott8ff510a2020-06-02 17:19:28 +0100391 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100392 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100393 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100394#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100395 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100396 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100397 case 256:
398 if( bits_is_sloppy )
399 return( MBEDTLS_ECP_DP_CURVE25519 );
400 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100401#endif
402#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100403 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100404 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100406 }
407 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100408
Paul Elliott8ff510a2020-06-02 17:19:28 +0100409 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100410 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100411 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100412#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100413 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100414 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100415#endif
416#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100418 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100419#endif
420#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100422 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100423#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 }
425 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200426 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100428 (void) bits_is_sloppy;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200430}
John Durkop9814fa22020-11-04 12:28:15 -0800431#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800432 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
433 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
434 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200435
Archana4d7ae1d2021-07-07 02:50:22 +0530436psa_status_t psa_validate_unstructured_key_bit_size( psa_key_type_t type,
Archana449608b2021-09-08 15:36:05 +0530437 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200438{
439 /* Check that the bit size is acceptable for the key type */
440 switch( type )
441 {
442 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200443 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200444 case PSA_KEY_TYPE_DERIVE:
445 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100446#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447 case PSA_KEY_TYPE_AES:
448 if( bits != 128 && bits != 192 && bits != 256 )
449 return( PSA_ERROR_INVALID_ARGUMENT );
450 break;
451#endif
Gilles Peskine6c12a1e2021-09-21 11:59:39 +0200452#if defined(PSA_WANT_KEY_TYPE_ARIA)
453 case PSA_KEY_TYPE_ARIA:
454 if( bits != 128 && bits != 192 && bits != 256 )
455 return( PSA_ERROR_INVALID_ARGUMENT );
456 break;
457#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100458#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459 case PSA_KEY_TYPE_CAMELLIA:
460 if( bits != 128 && bits != 192 && bits != 256 )
461 return( PSA_ERROR_INVALID_ARGUMENT );
462 break;
463#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100464#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465 case PSA_KEY_TYPE_DES:
466 if( bits != 64 && bits != 128 && bits != 192 )
467 return( PSA_ERROR_INVALID_ARGUMENT );
468 break;
469#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100470#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200471 case PSA_KEY_TYPE_CHACHA20:
472 if( bits != 256 )
473 return( PSA_ERROR_INVALID_ARGUMENT );
474 break;
475#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476 default:
477 return( PSA_ERROR_NOT_SUPPORTED );
478 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200479 if( bits % 8 != 0 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482 return( PSA_SUCCESS );
483}
484
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100485/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100486 *
Steven Cooremancd640932021-03-08 12:00:27 +0100487 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
488 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100489 *
490 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100491 * \param[in] key_type The key type of the key to be used with the
492 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100493 *
494 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100495 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100496 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100497 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100498 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100499MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100500 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100501 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100502{
Steven Cooremancd640932021-03-08 12:00:27 +0100503 if( PSA_ALG_IS_HMAC( algorithm ) )
504 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100505 if( key_type == PSA_KEY_TYPE_HMAC )
506 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100507 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100508
509 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100510 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100511 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
512 * key. */
513 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
514 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
515 {
516 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
517 * the block length (larger than 1) for block ciphers. */
518 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
519 return( PSA_SUCCESS );
520 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100521 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100522
523 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100524}
525
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200526psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
527 size_t buffer_length )
Steven Cooreman75b74362020-07-28 14:30:13 +0200528{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100529 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200530 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200531
Ronald Cronea0f8a62020-11-25 17:52:23 +0100532 slot->key.data = mbedtls_calloc( 1, buffer_length );
533 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200534 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200535
Ronald Cronea0f8a62020-11-25 17:52:23 +0100536 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200537 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200538}
539
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200540psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
541 const uint8_t* data,
542 size_t data_length )
543{
544 psa_status_t status = psa_allocate_buffer_to_slot( slot,
545 data_length );
546 if( status != PSA_SUCCESS )
547 return( status );
548
Ronald Cronea0f8a62020-11-25 17:52:23 +0100549 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200550 return( PSA_SUCCESS );
551}
552
Ronald Crona0fe59f2020-11-29 11:14:53 +0100553psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100554 const psa_key_attributes_t *attributes,
555 const uint8_t *data, size_t data_length,
556 uint8_t *key_buffer, size_t key_buffer_size,
557 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
560 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100561
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200562 /* zero-length keys are never supported. */
563 if( data_length == 0 )
564 return( PSA_ERROR_NOT_SUPPORTED );
565
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100566 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100568 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200569
Archana449608b2021-09-08 15:36:05 +0530570 status = psa_validate_unstructured_key_bit_size( attributes->core.type,
571 *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200572 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200573 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200574
Ronald Crondd04d422020-11-28 15:14:42 +0100575 /* Copy the key material. */
576 memcpy( key_buffer, data, data_length );
577 *key_buffer_length = data_length;
578 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200579
Steven Cooreman40120f62020-10-29 11:42:22 +0100580 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100581 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100582 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200583 {
John Durkop9814fa22020-11-04 12:28:15 -0800584#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
585 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100586 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200587 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100588 return( mbedtls_psa_ecp_import_key( attributes,
589 data, data_length,
590 key_buffer, key_buffer_size,
591 key_buffer_length,
592 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100593 }
John Durkop9814fa22020-11-04 12:28:15 -0800594#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
595 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700596#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
597 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100598 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200599 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100600 return( mbedtls_psa_rsa_import_key( attributes,
601 data, data_length,
602 key_buffer, key_buffer_size,
603 key_buffer_length,
604 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200605 }
John Durkop9814fa22020-11-04 12:28:15 -0800606#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
607 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100608 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100609
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100610 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100611}
612
Gilles Peskinef603c712019-01-19 13:40:11 +0100613/** Calculate the intersection of two algorithm usage policies.
614 *
615 * Return 0 (which allows no operation) on incompatibility.
616 */
617static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100618 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100619 psa_algorithm_t alg1,
620 psa_algorithm_t alg2 )
621{
Gilles Peskine549ea862019-05-22 11:45:59 +0200622 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100623 if( alg1 == alg2 )
624 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100625 /* If the policies are from the same hash-and-sign family, check
626 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskinef7b41372021-09-22 16:15:05 +0200627 if( PSA_ALG_IS_SIGN_HASH( alg1 ) &&
628 PSA_ALG_IS_SIGN_HASH( alg2 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100629 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100630 {
Steven Cooreman03488022021-02-18 12:07:20 +0100631 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
632 return( alg2 );
633 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
634 return( alg1 );
635 }
636 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100637 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100638 * restrictive tag length. */
639 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
640 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
641 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
642 {
643 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
644 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100645 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100646
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100647 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100648 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
649 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100650 {
Steven Cooremancd640932021-03-08 12:00:27 +0100651 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
652 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100653 }
654 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100655 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100656 ( alg1_len <= alg2_len ) )
657 {
658 return( alg2 );
659 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100660 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100661 ( alg2_len <= alg1_len ) )
662 {
663 return( alg1 );
664 }
665 }
666 /* If the policies are from the same MAC family, check whether one
667 * of them is a minimum-MAC-length policy. Calculate the most
668 * restrictive tag length. */
669 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
670 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
671 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
672 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100673 /* Validate the combination of key type and algorithm. Since the base
674 * algorithm of alg1 and alg2 are the same, we only need this once. */
675 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100676 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100677
Steven Cooreman31a876d2021-03-03 20:47:40 +0100678 /* Get the (exact or at-least) output lengths for both sides of the
679 * requested intersection. None of the currently supported algorithms
680 * have an output length dependent on the actual key size, so setting it
681 * to a bogus value of 0 is currently OK.
682 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100683 * Note that for at-least-this-length wildcard algorithms, the output
684 * length is set to the shortest allowed length, which allows us to
685 * calculate the most restrictive tag length for the intersection. */
686 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
687 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100688 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100689
Steven Cooremana1d83222021-02-25 10:20:29 +0100690 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100691 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
692 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100693 {
Steven Cooremancd640932021-03-08 12:00:27 +0100694 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100695 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100696
697 /* If only one is an at-least-this-length policy, the intersection would
698 * be the other (fixed-length) policy as long as said fixed length is
699 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100700 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100701 {
Steven Cooremancd640932021-03-08 12:00:27 +0100702 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100703 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100704 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100705 {
Steven Cooremancd640932021-03-08 12:00:27 +0100706 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100707 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100708
Steven Cooremancd640932021-03-08 12:00:27 +0100709 /* If none of them are wildcards, check whether they define the same tag
710 * length. This is still possible here when one is default-length and
711 * the other specific-length. Ensure to always return the
712 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100713 if( alg1_len == alg2_len )
714 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100715 }
716 /* If the policies are incompatible, allow nothing. */
717 return( 0 );
718}
719
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100720static int psa_key_algorithm_permits( psa_key_type_t key_type,
721 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200722 psa_algorithm_t requested_alg )
723{
Gilles Peskine549ea862019-05-22 11:45:59 +0200724 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200725 if( requested_alg == policy_alg )
726 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100727 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
728 * and requested_alg is the same hash-and-sign family with any hash,
729 * then requested_alg is compliant with policy_alg. */
Gilles Peskinef7b41372021-09-22 16:15:05 +0200730 if( PSA_ALG_IS_SIGN_HASH( requested_alg ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100731 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200732 {
Steven Cooreman03488022021-02-18 12:07:20 +0100733 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
734 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
735 }
736 /* If policy_alg is a wildcard AEAD algorithm of the same base as
737 * the requested algorithm, check the requested tag length to be
738 * equal-length or longer than the wildcard-specified length. */
739 if( PSA_ALG_IS_AEAD( policy_alg ) &&
740 PSA_ALG_IS_AEAD( requested_alg ) &&
741 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
742 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100743 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100744 {
745 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
746 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
747 }
Steven Cooremancd640932021-03-08 12:00:27 +0100748 /* If policy_alg is a MAC algorithm of the same base as the requested
749 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100750 if( PSA_ALG_IS_MAC( policy_alg ) &&
751 PSA_ALG_IS_MAC( requested_alg ) &&
752 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100753 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100754 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100755 /* Validate the combination of key type and algorithm. Since the policy
756 * and requested algorithms are the same, we only need this once. */
757 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100758 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100759
Steven Cooreman31a876d2021-03-03 20:47:40 +0100760 /* Get both the requested output length for the algorithm which is to be
761 * verified, and the default output length for the base algorithm.
762 * Note that none of the currently supported algorithms have an output
763 * length dependent on actual key size, so setting it to a bogus value
764 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100765 size_t requested_output_length = PSA_MAC_LENGTH(
766 key_type, 0, requested_alg );
767 size_t default_output_length = PSA_MAC_LENGTH(
768 key_type, 0,
769 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100770
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100771 /* If the policy is default-length, only allow an algorithm with
772 * a declared exact-length matching the default. */
773 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100774 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100775
776 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100777 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100778 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
779 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
780 {
781 return( 1 );
782 }
783
Steven Cooremancd640932021-03-08 12:00:27 +0100784 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
785 * check for the requested MAC length to be equal to or longer than the
786 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100787 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
788 {
789 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100790 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100791 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200792 }
Steven Cooremance48e852020-10-05 16:02:45 +0200793 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200794 * a key derivation with that key agreement should also be allowed. This
795 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200796 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
797 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
798 {
799 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
800 policy_alg );
801 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100802 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200803 return( 0 );
804}
805
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100806/** Test whether a policy permits an algorithm.
807 *
808 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100809 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100810 * \note This function requires providing the key type for which the policy is
811 * being validated, since some algorithm policy definitions (e.g. MAC)
812 * have different properties depending on what kind of cipher it is
813 * combined with.
814 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100815 * \retval PSA_SUCCESS When \p alg is a specific algorithm
816 * allowed by the \p policy.
817 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
818 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
819 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100820 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100821static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100822 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100823 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100824{
Steven Cooremand788fab2021-02-25 11:29:17 +0100825 /* '0' is not a valid algorithm */
826 if( alg == 0 )
827 return( PSA_ERROR_INVALID_ARGUMENT );
828
Steven Cooreman7e39f052021-02-23 14:18:32 +0100829 /* A requested algorithm cannot be a wildcard. */
830 if( PSA_ALG_IS_WILDCARD( alg ) )
831 return( PSA_ERROR_INVALID_ARGUMENT );
832
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100833 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
834 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100835 return( PSA_SUCCESS );
836 else
837 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100838}
839
Gilles Peskinef603c712019-01-19 13:40:11 +0100840/** Restrict a key policy based on a constraint.
841 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100842 * \note This function requires providing the key type for which the policy is
843 * being restricted, since some algorithm policy definitions (e.g. MAC)
844 * have different properties depending on what kind of cipher it is
845 * combined with.
846 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100847 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100848 * \param[in,out] policy The policy to restrict.
849 * \param[in] constraint The policy constraint to apply.
850 *
851 * \retval #PSA_SUCCESS
852 * \c *policy contains the intersection of the original value of
853 * \c *policy and \c *constraint.
854 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100855 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100856 * \c *policy is unchanged.
857 */
858static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100859 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100860 psa_key_policy_t *policy,
861 const psa_key_policy_t *constraint )
862{
863 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100864 psa_key_policy_algorithm_intersection( key_type, policy->alg,
865 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200866 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100867 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
868 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100869 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
870 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200871 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
872 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100873 policy->usage &= constraint->usage;
874 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200875 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100876 return( PSA_SUCCESS );
877}
878
Ronald Cron5c522922020-11-14 16:35:34 +0100879/** Get the description of a key given its identifier and policy constraints
880 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200881 *
Ronald Cron5c522922020-11-14 16:35:34 +0100882 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100883 * nonzero, the key must allow operations with this algorithm. If \p alg is
884 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100885 *
Ronald Cron5c522922020-11-14 16:35:34 +0100886 * In case of a persistent key, the function loads the description of the key
887 * into a key slot if not already done.
888 *
889 * On success, the returned key slot is locked. It is the responsibility of
890 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200891 */
Ronald Cron5c522922020-11-14 16:35:34 +0100892static psa_status_t psa_get_and_lock_key_slot_with_policy(
893 mbedtls_svc_key_id_t key,
894 psa_key_slot_t **p_slot,
895 psa_key_usage_t usage,
896 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100897{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200898 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
899 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100900
Ronald Cron5c522922020-11-14 16:35:34 +0100901 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100902 if( status != PSA_SUCCESS )
903 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200904 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
906 /* Enforce that usage policy for the key slot contains all the flags
907 * required by the usage parameter. There is one exception: public
908 * keys can always be exported, so we treat public key objects as
909 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200910 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100911 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200912
Gilles Peskine8e338702019-07-30 20:06:31 +0200913 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100914 {
915 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200916 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100917 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100918
919 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100920 if( alg != 0 )
921 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100922 status = psa_key_policy_permits( &slot->attr.policy,
923 slot->attr.type,
924 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +0100925 if( status != PSA_SUCCESS )
926 goto error;
927 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100928
Darryl Green06fd18d2018-07-16 11:21:11 +0100929 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200930
931error:
932 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100933 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200934
935 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100936}
Darryl Green940d72c2018-07-13 13:18:51 +0100937
Ronald Cron5c522922020-11-14 16:35:34 +0100938/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200939 *
940 * A transparent key is a key for which the key material is directly
Ronald Cronddae0f52021-08-24 15:39:44 +0200941 * available, as opposed to a key in a secure element and/or to be used
942 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200943 *
Ronald Cronddae0f52021-08-24 15:39:44 +0200944 * This is a temporary function that may be used instead of
945 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
946 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200947 *
Ronald Cron5c522922020-11-14 16:35:34 +0100948 * On success, the returned key slot is locked. It is the responsibility of the
949 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200950 */
Ronald Cron5c522922020-11-14 16:35:34 +0100951static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
952 mbedtls_svc_key_id_t key,
953 psa_key_slot_t **p_slot,
954 psa_key_usage_t usage,
955 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200956{
Ronald Cron5c522922020-11-14 16:35:34 +0100957 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
958 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200959 if( status != PSA_SUCCESS )
960 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200961
Ronald Cronddae0f52021-08-24 15:39:44 +0200962 if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200963 {
Ronald Cron5c522922020-11-14 16:35:34 +0100964 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200965 *p_slot = NULL;
966 return( PSA_ERROR_NOT_SUPPORTED );
967 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200968
Gilles Peskine28f8f302019-07-24 13:30:31 +0200969 return( PSA_SUCCESS );
970}
Gilles Peskine28f8f302019-07-24 13:30:31 +0200971
Steven Cooreman7ddee7f2021-04-07 18:08:30 +0200972psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000973{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100974 /* Data pointer will always be either a valid pointer or NULL in an
975 * initialized slot, so we can just free it. */
976 if( slot->key.data != NULL )
977 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
978
979 mbedtls_free( slot->key.data );
980 slot->key.data = NULL;
981 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000982
983 return( PSA_SUCCESS );
984}
985
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100986/** Completely wipe a slot in memory, including its policy.
987 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100988psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100989{
990 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100991
TRodziewicz18cddc02021-07-13 12:19:15 +0200992 /*
993 * As the return error code may not be handled in case of multiple errors,
TRodziewiczc9890e92021-07-14 10:16:26 +0200994 * do our best to report an unexpected lock counter. Assert with
995 * MBEDTLS_TEST_HOOK_TEST_ASSERT that the lock counter is equal to one:
996 * if the MBEDTLS_TEST_HOOKS configuration option is enabled and the
997 * function is called as part of the execution of a test suite, the
998 * execution of the test suite is stopped in error if the assertion fails.
TRodziewicz18cddc02021-07-13 12:19:15 +0200999 */
Ronald Cron5c522922020-11-14 16:35:34 +01001000 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001001 {
TRodziewicz7871c2e2021-07-07 17:29:43 +02001002 MBEDTLS_TEST_HOOK_TEST_ASSERT( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001003 status = PSA_ERROR_CORRUPTION_DETECTED;
1004 }
1005
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001006 /* Multipart operations may still be using the key. This is safe
1007 * because all multipart operation objects are independent from
1008 * the key slot: if they need to access the key after the setup
1009 * phase, they have a copy of the key. Note that this means that
1010 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001011 /* At this point, key material and other type-specific content has
1012 * been wiped. Clear remaining metadata. We can call memset and not
1013 * zeroize because the metadata is not particularly sensitive. */
1014 memset( slot, 0, sizeof( *slot ) );
1015 return( status );
1016}
1017
Ronald Croncf56a0a2020-08-04 09:51:30 +02001018psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001020 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001021 psa_status_t status; /* status of the last operation */
1022 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1024 psa_se_drv_table_entry_t *driver;
1025#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026
Ronald Croncf56a0a2020-08-04 09:51:30 +02001027 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001028 return( PSA_SUCCESS );
1029
Ronald Cronf2911112020-10-29 17:51:10 +01001030 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001031 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001032 * key, this will load the key description from persistent memory if not
1033 * done yet. We cannot avoid this loading as without it we don't know if
1034 * the key is operated by an SE or not and this information is needed by
1035 * the current implementation.
1036 */
Ronald Cron5c522922020-11-14 16:35:34 +01001037 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001038 if( status != PSA_SUCCESS )
1039 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001040
Ronald Cronf2911112020-10-29 17:51:10 +01001041 /*
1042 * If the key slot containing the key description is under access by the
1043 * library (apart from the present access), the key cannot be destroyed
1044 * yet. For the time being, just return in error. Eventually (to be
1045 * implemented), the key should be destroyed when all accesses have
1046 * stopped.
1047 */
Ronald Cron5c522922020-11-14 16:35:34 +01001048 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001049 {
Ronald Cron5c522922020-11-14 16:35:34 +01001050 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001051 return( PSA_ERROR_GENERIC_ERROR );
1052 }
1053
Gilles Peskine6687cd02021-04-21 22:32:05 +02001054 if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) )
1055 {
1056 /* Refuse the destruction of a read-only key (which may or may not work
1057 * if we attempt it, depending on whether the key is merely read-only
1058 * by policy or actually physically read-only).
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001059 * Just do the best we can, which is to wipe the copy in memory
1060 * (done in this function's cleanup code). */
1061 overall_status = PSA_ERROR_NOT_PERMITTED;
1062 goto exit;
Gilles Peskine6687cd02021-04-21 22:32:05 +02001063 }
1064
Gilles Peskine354f7672019-07-12 23:46:38 +02001065#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001066 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001067 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001068 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001069 /* For a key in a secure element, we need to do three things:
1070 * remove the key file in internal storage, destroy the
1071 * key inside the secure element, and update the driver's
1072 * persistent data. Start a transaction that will encompass these
1073 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001074 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001075 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001076 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001077 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001078 status = psa_crypto_save_transaction( );
1079 if( status != PSA_SUCCESS )
1080 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001081 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001082 /* We should still try to destroy the key in the secure
1083 * element and the key metadata in storage. This is especially
1084 * important if the error is that the storage is full.
1085 * But how to do it exactly without risking an inconsistent
1086 * state after a reset?
1087 * https://github.com/ARMmbed/mbed-crypto/issues/215
1088 */
1089 overall_status = status;
1090 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001091 }
1092
Ronald Cronea0f8a62020-11-25 17:52:23 +01001093 status = psa_destroy_se_key( driver,
1094 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001095 if( overall_status == PSA_SUCCESS )
1096 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001097 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001098#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1099
Darryl Greend49a4992018-06-18 17:27:26 +01001100#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001101 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001102 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001103 status = psa_destroy_persistent_key( slot->attr.id );
1104 if( overall_status == PSA_SUCCESS )
1105 overall_status = status;
1106
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001107 /* TODO: other slots may have a copy of the same key. We should
1108 * invalidate them.
1109 * https://github.com/ARMmbed/mbed-crypto/issues/214
1110 */
Darryl Greend49a4992018-06-18 17:27:26 +01001111 }
1112#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001113
Gilles Peskinefc762652019-07-22 19:30:34 +02001114#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1115 if( driver != NULL )
1116 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001117 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001118 if( overall_status == PSA_SUCCESS )
1119 overall_status = status;
1120 status = psa_crypto_stop_transaction( );
1121 if( overall_status == PSA_SUCCESS )
1122 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001123 }
1124#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1125
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001126exit:
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001127 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001128 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskinef9a046e2021-06-07 23:27:54 +02001129 if( status != PSA_SUCCESS )
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001130 overall_status = status;
1131 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001132}
1133
John Durkop07cc04a2020-11-16 22:08:34 -08001134#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001135 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001136static psa_status_t psa_get_rsa_public_exponent(
1137 const mbedtls_rsa_context *rsa,
1138 psa_key_attributes_t *attributes )
1139{
1140 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001142 uint8_t *buffer = NULL;
1143 size_t buflen;
1144 mbedtls_mpi_init( &mpi );
1145
1146 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1147 if( ret != 0 )
1148 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001149 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1150 {
1151 /* It's the default value, which is reported as an empty string,
1152 * so there's nothing to do. */
1153 goto exit;
1154 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001155
1156 buflen = mbedtls_mpi_size( &mpi );
1157 buffer = mbedtls_calloc( 1, buflen );
1158 if( buffer == NULL )
1159 {
1160 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1161 goto exit;
1162 }
1163 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1164 if( ret != 0 )
1165 goto exit;
1166 attributes->domain_parameters = buffer;
1167 attributes->domain_parameters_size = buflen;
1168
1169exit:
1170 mbedtls_mpi_free( &mpi );
1171 if( ret != 0 )
1172 mbedtls_free( buffer );
1173 return( mbedtls_to_psa_error( ret ) );
1174}
John Durkop07cc04a2020-11-16 22:08:34 -08001175#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001176 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001177
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001178/** Retrieve all the publicly-accessible attributes of a key.
1179 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001180psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001181 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001182{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001183 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001184 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001185 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001187 psa_reset_key_attributes( attributes );
1188
Ronald Cron5c522922020-11-14 16:35:34 +01001189 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001190 if( status != PSA_SUCCESS )
1191 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001192
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001193 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001194 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1195 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1196
1197#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron512ad812021-08-24 15:50:05 +02001198 if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001199 psa_set_key_slot_number( attributes,
1200 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001201#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001202
Gilles Peskine8e338702019-07-30 20:06:31 +02001203 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001204 {
John Durkop07cc04a2020-11-16 22:08:34 -08001205#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001206 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001207 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001208 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001209 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001210 * is not yet implemented.
1211 * https://github.com/ARMmbed/mbed-crypto/issues/216
1212 */
Ronald Cron9b8b69c2021-08-24 16:00:51 +02001213 if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02001214 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001215 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001216
Ronald Cron90857082020-11-25 14:58:33 +01001217 status = mbedtls_psa_rsa_load_representation(
1218 slot->attr.type,
1219 slot->key.data,
1220 slot->key.bytes,
1221 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001222 if( status != PSA_SUCCESS )
1223 break;
1224
Steven Cooremana2371e52020-07-28 14:30:39 +02001225 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001226 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001227 mbedtls_rsa_free( rsa );
1228 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001229 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001230 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001231#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001232 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001233 default:
1234 /* Nothing else to do. */
1235 break;
1236 }
1237
1238 if( status != PSA_SUCCESS )
1239 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001240
Ronald Cron5c522922020-11-14 16:35:34 +01001241 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001242
Ronald Cron5c522922020-11-14 16:35:34 +01001243 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244}
1245
Gilles Peskinec8000c02019-08-02 20:15:51 +02001246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1247psa_status_t psa_get_key_slot_number(
1248 const psa_key_attributes_t *attributes,
1249 psa_key_slot_number_t *slot_number )
1250{
1251 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1252 {
1253 *slot_number = attributes->slot_number;
1254 return( PSA_SUCCESS );
1255 }
1256 else
1257 return( PSA_ERROR_INVALID_ARGUMENT );
1258}
1259#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1260
Ronald Crond18b5f82020-11-25 16:46:05 +01001261static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1262 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001263 uint8_t *data,
1264 size_t data_size,
1265 size_t *data_length )
1266{
Ronald Crond18b5f82020-11-25 16:46:05 +01001267 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001268 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001269 memcpy( data, key_buffer, key_buffer_size );
1270 memset( data + key_buffer_size, 0,
1271 data_size - key_buffer_size );
1272 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001273 return( PSA_SUCCESS );
1274}
1275
Ronald Cron7285cda2020-11-26 14:46:37 +01001276psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001277 const psa_key_attributes_t *attributes,
1278 const uint8_t *key_buffer, size_t key_buffer_size,
1279 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280{
Ronald Crond18b5f82020-11-25 16:46:05 +01001281 psa_key_type_t type = attributes->core.type;
1282
Ronald Crond18b5f82020-11-25 16:46:05 +01001283 if( key_type_is_raw_bytes( type ) ||
1284 PSA_KEY_TYPE_IS_RSA( type ) ||
1285 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001286 {
1287 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001288 key_buffer, key_buffer_size,
1289 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001290 }
1291 else
1292 {
1293 /* This shouldn't happen in the reference implementation, but
1294 it is valid for a special-purpose implementation to omit
1295 support for exporting certain key types. */
1296 return( PSA_ERROR_NOT_SUPPORTED );
1297 }
1298}
1299
1300psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1301 uint8_t *data,
1302 size_t data_size,
1303 size_t *data_length )
1304{
1305 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1306 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1307 psa_key_slot_t *slot;
1308
Ronald Crone907e552021-01-18 13:22:38 +01001309 /* Reject a zero-length output buffer now, since this can never be a
1310 * valid key representation. This way we know that data must be a valid
1311 * pointer and we can do things like memset(data, ..., data_size). */
1312 if( data_size == 0 )
1313 return( PSA_ERROR_BUFFER_TOO_SMALL );
1314
Ronald Cron9486f9d2020-11-26 13:36:23 +01001315 /* Set the key to empty now, so that even when there are errors, we always
1316 * set data_length to a value between 0 and data_size. On error, setting
1317 * the key to empty is a good choice because an empty key representation is
1318 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001319 *data_length = 0;
1320
Ronald Cron9486f9d2020-11-26 13:36:23 +01001321 /* Export requires the EXPORT flag. There is an exception for public keys,
1322 * which don't require any flag, but
1323 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1324 */
1325 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1326 PSA_KEY_USAGE_EXPORT, 0 );
1327 if( status != PSA_SUCCESS )
1328 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001329
Ronald Crond18b5f82020-11-25 16:46:05 +01001330 psa_key_attributes_t attributes = {
1331 .core = slot->attr
1332 };
Ronald Cron67227982020-11-26 15:16:05 +01001333 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001334 slot->key.data, slot->key.bytes,
1335 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001336
Ronald Cron9486f9d2020-11-26 13:36:23 +01001337 unlock_status = psa_unlock_key_slot( slot );
1338
1339 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1340}
1341
Ronald Cron7285cda2020-11-26 14:46:37 +01001342psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001343 const psa_key_attributes_t *attributes,
1344 const uint8_t *key_buffer,
1345 size_t key_buffer_size,
1346 uint8_t *data,
1347 size_t data_size,
1348 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001349{
Ronald Crond18b5f82020-11-25 16:46:05 +01001350 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001351
Ronald Crond18b5f82020-11-25 16:46:05 +01001352 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001353 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001354 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001355 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001356 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001357 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001358 key_buffer, key_buffer_size,
1359 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001360 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001361
Ronald Crond18b5f82020-11-25 16:46:05 +01001362 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001363 {
John Durkop0e005192020-10-31 22:06:54 -07001364#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1365 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001366 return( mbedtls_psa_rsa_export_public_key( attributes,
1367 key_buffer,
1368 key_buffer_size,
1369 data,
1370 data_size,
1371 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001372#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001373 /* We don't know how to convert a private RSA key to public. */
1374 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001375#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1376 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001377 }
1378 else
Moran Pekera998bc62018-04-16 18:16:20 +03001379 {
John Durkop6ba40d12020-11-10 08:50:04 -08001380#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1381 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001382 return( mbedtls_psa_ecp_export_public_key( attributes,
1383 key_buffer,
1384 key_buffer_size,
1385 data,
1386 data_size,
1387 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001388#else
1389 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001390 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001391#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1392 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001393 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001394 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001395 else
1396 {
1397 /* This shouldn't happen in the reference implementation, but
1398 it is valid for a special-purpose implementation to omit
1399 support for exporting certain key types. */
1400 return( PSA_ERROR_NOT_SUPPORTED );
1401 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001402}
Ronald Crond18b5f82020-11-25 16:46:05 +01001403
Ronald Croncf56a0a2020-08-04 09:51:30 +02001404psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001405 uint8_t *data,
1406 size_t data_size,
1407 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001408{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001409 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001410 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001411 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001412
Ronald Crone907e552021-01-18 13:22:38 +01001413 /* Reject a zero-length output buffer now, since this can never be a
1414 * valid key representation. This way we know that data must be a valid
1415 * pointer and we can do things like memset(data, ..., data_size). */
1416 if( data_size == 0 )
1417 return( PSA_ERROR_BUFFER_TOO_SMALL );
1418
Darryl Greendd8fb772018-11-07 16:00:44 +00001419 /* Set the key to empty now, so that even when there are errors, we always
1420 * set data_length to a value between 0 and data_size. On error, setting
1421 * the key to empty is a good choice because an empty key representation is
1422 * unlikely to be accepted anywhere. */
1423 *data_length = 0;
1424
1425 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001426 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001427 if( status != PSA_SUCCESS )
1428 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001429
Ronald Cron9486f9d2020-11-26 13:36:23 +01001430 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1431 {
1432 status = PSA_ERROR_INVALID_ARGUMENT;
1433 goto exit;
1434 }
1435
Ronald Crond18b5f82020-11-25 16:46:05 +01001436 psa_key_attributes_t attributes = {
1437 .core = slot->attr
1438 };
Ronald Cron67227982020-11-26 15:16:05 +01001439 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001440 &attributes, slot->key.data, slot->key.bytes,
1441 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001442
1443exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001444 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001445
Ronald Cron5c522922020-11-14 16:35:34 +01001446 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001447}
1448
Gilles Peskine91e8c332019-08-02 19:19:39 +02001449#if defined(static_assert)
1450static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1451 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001452static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001453 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001454static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001455 "One or more key attribute flag is listed as both internal-only and external-only" );
1456#endif
1457
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001458/** Validate that a key policy is internally well-formed.
1459 *
1460 * This function only rejects invalid policies. It does not validate the
1461 * consistency of the policy with respect to other attributes of the key
1462 * such as the key type.
1463 */
1464static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001465{
1466 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001467 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001468 PSA_KEY_USAGE_ENCRYPT |
1469 PSA_KEY_USAGE_DECRYPT |
gabor-mezei-arm4a210192021-04-14 21:14:28 +02001470 PSA_KEY_USAGE_SIGN_MESSAGE |
1471 PSA_KEY_USAGE_VERIFY_MESSAGE |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001472 PSA_KEY_USAGE_SIGN_HASH |
1473 PSA_KEY_USAGE_VERIFY_HASH |
Manuel Pégourié-Gonnard805251b2021-05-04 09:49:59 +02001474 PSA_KEY_USAGE_VERIFY_DERIVATION |
Gilles Peskine4747d192019-04-17 15:05:45 +02001475 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1476 return( PSA_ERROR_INVALID_ARGUMENT );
1477
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 return( PSA_SUCCESS );
1479}
1480
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001481/** Validate the internal consistency of key attributes.
1482 *
1483 * This function only rejects invalid attribute values. If does not
1484 * validate the consistency of the attributes with any key data that may
1485 * be involved in the creation of the key.
1486 *
1487 * Call this function early in the key creation process.
1488 *
1489 * \param[in] attributes Key attributes for the new key.
1490 * \param[out] p_drv On any return, the driver for the key, if any.
1491 * NULL for a transparent key.
1492 *
1493 */
1494static psa_status_t psa_validate_key_attributes(
1495 const psa_key_attributes_t *attributes,
1496 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001497{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001498 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001499 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001500 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001501
Ronald Cron54b90082020-10-29 15:26:43 +01001502 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001503 if( status != PSA_SUCCESS )
1504 return( status );
1505
Ronald Crond2ed4812020-07-17 16:11:30 +02001506 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001507 if( status != PSA_SUCCESS )
1508 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001509
Ronald Cron65f38a32020-10-23 17:11:13 +02001510 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1511 {
1512 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1513 return( PSA_ERROR_INVALID_ARGUMENT );
1514 }
1515 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001516 {
Ronald Cron77e412c2021-03-31 17:36:31 +02001517 if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
1518 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Crond2ed4812020-07-17 16:11:30 +02001519 }
1520
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001521 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001522 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001523 return( status );
1524
1525 /* Refuse to create overly large keys.
1526 * Note that this doesn't trigger on import if the attributes don't
1527 * explicitly specify a size (so psa_get_key_bits returns 0), so
1528 * psa_import_key() needs its own checks. */
1529 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1530 return( PSA_ERROR_NOT_SUPPORTED );
1531
Gilles Peskine91e8c332019-08-02 19:19:39 +02001532 /* Reject invalid flags. These should not be reachable through the API. */
1533 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1534 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1535 return( PSA_ERROR_INVALID_ARGUMENT );
1536
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001537 return( PSA_SUCCESS );
1538}
1539
Gilles Peskine4747d192019-04-17 15:05:45 +02001540/** Prepare a key slot to receive key material.
1541 *
1542 * This function allocates a key slot and sets its metadata.
1543 *
1544 * If this function fails, call psa_fail_key_creation().
1545 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001546 * This function is intended to be used as follows:
1547 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001548 * it with the specified attributes, and in case of a volatile key assign it
1549 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001550 * -# Populate the slot with the key material.
1551 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1552 * In case of failure at any step, stop the sequence and call
1553 * psa_fail_key_creation().
1554 *
Ronald Cron5c522922020-11-14 16:35:34 +01001555 * On success, the key slot is locked. It is the responsibility of the caller
1556 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001557 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001558 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001559 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001560 * \param[out] p_slot On success, a pointer to the prepared slot.
1561 * \param[out] p_drv On any return, the driver for the key, if any.
1562 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001563 *
1564 * \retval #PSA_SUCCESS
1565 * The key slot is ready to receive key material.
1566 * \return If this function fails, the key slot is an invalid state.
1567 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001568 */
1569static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001570 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001571 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001572 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001573 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001574{
1575 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001576 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001577 psa_key_slot_t *slot;
1578
Gilles Peskinedf179142019-07-15 22:02:14 +02001579 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001580 *p_drv = NULL;
1581
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001582 status = psa_validate_key_attributes( attributes, p_drv );
1583 if( status != PSA_SUCCESS )
1584 return( status );
1585
Ronald Cronc4d1b512020-07-31 11:26:37 +02001586 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001587 if( status != PSA_SUCCESS )
1588 return( status );
1589 slot = *p_slot;
1590
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001591 /* We're storing the declared bit-size of the key. It's up to each
1592 * creation mechanism to verify that this information is correct.
1593 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001594 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001595 * is optional (import, copy). In case of a volatile key, assign it the
1596 * volatile key identifier associated to the slot returned to contain its
1597 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001598
1599 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001600 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1601 {
1602#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1603 slot->attr.id = volatile_key_id;
1604#else
1605 slot->attr.id.key_id = volatile_key_id;
1606#endif
1607 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001608
Gilles Peskine91e8c332019-08-02 19:19:39 +02001609 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001610 * external-only flags, query `attributes`. Thanks to the check
1611 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001612 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001613 * may have set. */
1614 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001615
Gilles Peskinecbaff462019-07-12 23:46:04 +02001616#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001617 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001618 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001619 * create the key file in internal storage, create the
1620 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001621 * persistent data. This is done by starting a transaction that will
1622 * encompass these three actions.
1623 * For registering a volatile key, we just need to find an appropriate
1624 * slot number inside the SE. Since the key is designated volatile, creating
1625 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001626 /* The first thing to do is to find a slot number for the new key.
1627 * We save the slot number in persistent storage as part of the
1628 * transaction data. It will be needed to recover if the power
1629 * fails during the key creation process, to clean up on the secure
1630 * element side after restarting. Obtaining a slot number from the
1631 * secure element driver updates its persistent state, but we do not yet
1632 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001633 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001634 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001635 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001636 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001637 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001638 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001639 if( status != PSA_SUCCESS )
1640 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001641
1642 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001643 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001644 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1645 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001646 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001647 psa_crypto_transaction.key.id = slot->attr.id;
1648 status = psa_crypto_save_transaction( );
1649 if( status != PSA_SUCCESS )
1650 {
1651 (void) psa_crypto_stop_transaction( );
1652 return( status );
1653 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001654 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001655
1656 status = psa_copy_key_material_into_slot(
1657 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001658 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001659
1660 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1661 {
1662 /* Key registration only makes sense with a secure element. */
1663 return( PSA_ERROR_INVALID_ARGUMENT );
1664 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001665#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1666
Ronald Cronc4d1b512020-07-31 11:26:37 +02001667 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001668}
Gilles Peskine4747d192019-04-17 15:05:45 +02001669
1670/** Finalize the creation of a key once its key material has been set.
1671 *
1672 * This entails writing the key to persistent storage.
1673 *
1674 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001675 * See the documentation of psa_start_key_creation() for the intended use
1676 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001677 *
Ronald Cron5c522922020-11-14 16:35:34 +01001678 * If the finalization succeeds, the function unlocks the key slot (it was
1679 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1680 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001681 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001682 * \param[in,out] slot Pointer to the slot with key material.
1683 * \param[in] driver The secure element driver for the key,
1684 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001685 * \param[out] key On success, identifier of the key. Note that the
1686 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001687 *
1688 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001689 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001690 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1691 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1692 * \retval #PSA_ERROR_ALREADY_EXISTS
1693 * \retval #PSA_ERROR_DATA_INVALID
1694 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001695 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001696 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001697 * \return If this function fails, the key slot is an invalid state.
1698 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001699 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001700static psa_status_t psa_finish_key_creation(
1701 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001702 psa_se_drv_table_entry_t *driver,
1703 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001704{
1705 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001706 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001707 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001708
1709#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001710 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001711 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001712#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1713 if( driver != NULL )
1714 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001715 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001716 psa_key_slot_number_t slot_number =
1717 psa_key_slot_get_slot_number( slot ) ;
1718
Gilles Peskineb46bef22019-07-30 21:32:04 +02001719#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001720 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001721 sizeof( data.slot_number ),
1722 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001723#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001724 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001725 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001726 (uint8_t*) &data,
1727 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001728 }
1729 else
1730#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1731 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001732 /* Key material is saved in export representation in the slot, so
1733 * just pass the slot buffer for storage. */
1734 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001735 slot->key.data,
1736 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001737 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001738 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001739#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1740
Gilles Peskinecbaff462019-07-12 23:46:04 +02001741#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001742 /* Finish the transaction for a key creation. This does not
1743 * happen when registering an existing key. Detect this case
1744 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001745 * creation of a persistent key in a secure element requires a transaction,
1746 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001747 if( driver != NULL &&
1748 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001749 {
1750 status = psa_save_se_persistent_data( driver );
1751 if( status != PSA_SUCCESS )
1752 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001753 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001754 return( status );
1755 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001756 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001757 }
1758#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1759
Ronald Cron50972942020-11-14 11:28:25 +01001760 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001761 {
1762 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001763 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001764 if( status != PSA_SUCCESS )
1765 *key = MBEDTLS_SVC_KEY_ID_INIT;
1766 }
Ronald Cron50972942020-11-14 11:28:25 +01001767
Gilles Peskine4747d192019-04-17 15:05:45 +02001768 return( status );
1769}
1770
1771/** Abort the creation of a key.
1772 *
1773 * You may call this function after calling psa_start_key_creation(),
1774 * or after psa_finish_key_creation() fails. In other circumstances, this
1775 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001776 * See the documentation of psa_start_key_creation() for the intended use
1777 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001779 * \param[in,out] slot Pointer to the slot with key material.
1780 * \param[in] driver The secure element driver for the key,
1781 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001783static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001784 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001785{
Gilles Peskine011e4282019-06-26 18:34:38 +02001786 (void) driver;
1787
Gilles Peskine4747d192019-04-17 15:05:45 +02001788 if( slot == NULL )
1789 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001790
1791#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001792 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001793 * element, and the failure happened later (when saving metadata
1794 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001795 * element.
1796 * https://github.com/ARMmbed/mbed-crypto/issues/217
1797 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001798
Gilles Peskined7729582019-08-05 15:55:54 +02001799 /* Abort the ongoing transaction if any (there may not be one if
1800 * the creation process failed before starting one, or if the
1801 * key creation is a registration of a key in a secure element).
1802 * Earlier functions must already have done what it takes to undo any
1803 * partial creation. All that's left is to update the transaction data
1804 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001805 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001806#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1807
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 psa_wipe_key_slot( slot );
1809}
1810
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001811/** Validate optional attributes during key creation.
1812 *
1813 * Some key attributes are optional during key creation. If they are
1814 * specified in the attributes structure, check that they are consistent
1815 * with the data in the slot.
1816 *
1817 * This function should be called near the end of key creation, after
1818 * the slot in memory is fully populated but before saving persistent data.
1819 */
1820static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001821 const psa_key_slot_t *slot,
1822 const psa_key_attributes_t *attributes )
1823{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001824 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001825 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001826 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001827 return( PSA_ERROR_INVALID_ARGUMENT );
1828 }
1829
1830 if( attributes->domain_parameters_size != 0 )
1831 {
John Durkop0e005192020-10-31 22:06:54 -07001832#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1833 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001834 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001835 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001836 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001837 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001839
Ronald Cron90857082020-11-25 14:58:33 +01001840 psa_status_t status = mbedtls_psa_rsa_load_representation(
1841 slot->attr.type,
1842 slot->key.data,
1843 slot->key.bytes,
1844 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001845 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001846 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001847
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001848 mbedtls_mpi_init( &actual );
1849 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001850 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001852 mbedtls_rsa_free( rsa );
1853 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001854 if( ret != 0 )
1855 goto rsa_exit;
1856 ret = mbedtls_mpi_read_binary( &required,
1857 attributes->domain_parameters,
1858 attributes->domain_parameters_size );
1859 if( ret != 0 )
1860 goto rsa_exit;
1861 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1862 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1863 rsa_exit:
1864 mbedtls_mpi_free( &actual );
1865 mbedtls_mpi_free( &required );
1866 if( ret != 0)
1867 return( mbedtls_to_psa_error( ret ) );
1868 }
1869 else
John Durkop9814fa22020-11-04 12:28:15 -08001870#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1871 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001872 {
1873 return( PSA_ERROR_INVALID_ARGUMENT );
1874 }
1875 }
1876
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001877 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001878 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001879 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001880 return( PSA_ERROR_INVALID_ARGUMENT );
1881 }
1882
1883 return( PSA_SUCCESS );
1884}
1885
Gilles Peskine4747d192019-04-17 15:05:45 +02001886psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001887 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001888 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001889 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001890{
1891 psa_status_t status;
1892 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001893 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001894 size_t bits;
Archanad8a83dc2021-06-14 10:04:16 +05301895 size_t storage_size = data_length;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896
Ronald Cron81709fc2020-11-14 12:10:32 +01001897 *key = MBEDTLS_SVC_KEY_ID_INIT;
1898
Gilles Peskine0f84d622019-09-12 19:03:13 +02001899 /* Reject zero-length symmetric keys (including raw data key objects).
1900 * This also rejects any key which might be encoded as an empty string,
1901 * which is never valid. */
1902 if( data_length == 0 )
1903 return( PSA_ERROR_INVALID_ARGUMENT );
1904
Archana449608b2021-09-08 15:36:05 +05301905 /* Ensure that the bytes-to-bits conversion cannot overflow. */
Archana6ed4bda2021-08-04 10:47:15 +05301906 if( data_length > SIZE_MAX / 8 )
1907 return( PSA_ERROR_NOT_SUPPORTED );
1908
Gilles Peskinedf179142019-07-15 22:02:14 +02001909 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001910 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001911 if( status != PSA_SUCCESS )
1912 goto exit;
1913
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001914 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05301915 * storage ( thus not in the case of importing a key in a secure element
1916 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
1917 * buffer to hold the imported key material. */
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001918 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001919 {
Archanad8a83dc2021-06-14 10:04:16 +05301920 if( psa_key_lifetime_is_external( attributes->core.lifetime ) )
1921 {
Archana449608b2021-09-08 15:36:05 +05301922 status = psa_driver_wrapper_get_key_buffer_size_from_key_data(
1923 attributes, data, data_length, &storage_size );
Archanad8a83dc2021-06-14 10:04:16 +05301924 if( status != PSA_SUCCESS )
1925 goto exit;
1926 }
1927 status = psa_allocate_buffer_to_slot( slot, storage_size );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001928 if( status != PSA_SUCCESS )
1929 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001930 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001931
1932 bits = slot->attr.bits;
1933 status = psa_driver_wrapper_import_key( attributes,
1934 data, data_length,
1935 slot->key.data,
1936 slot->key.bytes,
1937 &slot->key.bytes, &bits );
1938 if( status != PSA_SUCCESS )
1939 goto exit;
1940
1941 if( slot->attr.bits == 0 )
1942 slot->attr.bits = (psa_key_bits_t) bits;
1943 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001944 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001945 status = PSA_ERROR_INVALID_ARGUMENT;
1946 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001947 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001948
Archana6ed4bda2021-08-04 10:47:15 +05301949 /* Enforce a size limit, and in particular ensure that the bit
1950 * size fits in its representation type.*/
1951 if( bits > PSA_MAX_KEY_BITS )
1952 {
1953 status = PSA_ERROR_NOT_SUPPORTED;
1954 goto exit;
1955 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001956 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001957 if( status != PSA_SUCCESS )
1958 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959
Ronald Cron81709fc2020-11-14 12:10:32 +01001960 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001961exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001962 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001963 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001964
Gilles Peskine4747d192019-04-17 15:05:45 +02001965 return( status );
1966}
1967
Gilles Peskined7729582019-08-05 15:55:54 +02001968#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1969psa_status_t mbedtls_psa_register_se_key(
1970 const psa_key_attributes_t *attributes )
1971{
1972 psa_status_t status;
1973 psa_key_slot_t *slot = NULL;
1974 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001976
1977 /* Leaving attributes unspecified is not currently supported.
1978 * It could make sense to query the key type and size from the
1979 * secure element, but not all secure elements support this
1980 * and the driver HAL doesn't currently support it. */
1981 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1982 return( PSA_ERROR_NOT_SUPPORTED );
1983 if( psa_get_key_bits( attributes ) == 0 )
1984 return( PSA_ERROR_NOT_SUPPORTED );
1985
1986 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001987 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001988 if( status != PSA_SUCCESS )
1989 goto exit;
1990
Ronald Cron81709fc2020-11-14 12:10:32 +01001991 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001992
1993exit:
1994 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001995 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001996
Gilles Peskined7729582019-08-05 15:55:54 +02001997 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001998 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02001999 return( status );
2000}
2001#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2002
Ronald Croncf56a0a2020-08-04 09:51:30 +02002003psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002004 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002005 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002006{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002007 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002008 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002009 psa_key_slot_t *source_slot = NULL;
2010 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002011 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002012 psa_se_drv_table_entry_t *driver = NULL;
Archana8a180362021-07-05 02:18:48 +05302013 size_t storage_size = 0;
Gilles Peskinef603c712019-01-19 13:40:11 +01002014
Ronald Cron81709fc2020-11-14 12:10:32 +01002015 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2016
Archana8a180362021-07-05 02:18:48 +05302017 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron5c522922020-11-14 16:35:34 +01002018 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002019 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002020 goto exit;
2021
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002022 status = psa_validate_optional_attributes( source_slot,
2023 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002024 if( status != PSA_SUCCESS )
2025 goto exit;
2026
Archana9d17bf42021-09-10 06:22:44 +05302027 /* The target key type and number of bits have been validated by
2028 * psa_validate_optional_attributes() to be either equal to zero or
2029 * equal to the ones of the source key. So it is safe to inherit
2030 * them from the source key now."
Archana374fe5b2021-09-08 15:50:28 +05302031 * */
Archana9d17bf42021-09-10 06:22:44 +05302032 actual_attributes.core.bits = source_slot->attr.bits;
2033 actual_attributes.core.type = source_slot->attr.type;
Archana374fe5b2021-09-08 15:50:28 +05302034
2035
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002036 status = psa_restrict_key_policy( source_slot->attr.type,
2037 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002038 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002039 if( status != PSA_SUCCESS )
2040 goto exit;
2041
Ronald Cron81709fc2020-11-14 12:10:32 +01002042 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2043 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002044 if( status != PSA_SUCCESS )
2045 goto exit;
Archana8a180362021-07-05 02:18:48 +05302046 if( PSA_KEY_LIFETIME_GET_LOCATION( target_slot->attr.lifetime ) !=
2047 PSA_KEY_LIFETIME_GET_LOCATION( source_slot->attr.lifetime ) )
Gilles Peskinef603c712019-01-19 13:40:11 +01002048 {
Archana8a180362021-07-05 02:18:48 +05302049 /*
Archana9d17bf42021-09-10 06:22:44 +05302050 * If the source and target keys are stored in different locations,
Archana8a180362021-07-05 02:18:48 +05302051 * the source key would need to be exported as plaintext and re-imported
2052 * in the other location. This has security implications which have not
Archana449608b2021-09-08 15:36:05 +05302053 * been fully mapped. For now, this can be achieved through
Archana8a180362021-07-05 02:18:48 +05302054 * appropriate API invocations from the application, if needed.
2055 * */
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002056 status = PSA_ERROR_NOT_SUPPORTED;
2057 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002058 }
Archana8a180362021-07-05 02:18:48 +05302059 /*
2060 * When the source and target keys are within the same location,
Archana449608b2021-09-08 15:36:05 +05302061 * - For transparent keys it is a blind copy without any driver invocation,
Archana8a180362021-07-05 02:18:48 +05302062 * - For opaque keys this translates to an invocation of the drivers'
2063 * copy_key entry point through the dispatch layer.
2064 * */
Ronald Cron6cc66312021-04-02 12:27:47 +02002065 if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
2066 {
Archana8a180362021-07-05 02:18:48 +05302067 status = psa_driver_wrapper_get_key_buffer_size( &actual_attributes,
Archana449608b2021-09-08 15:36:05 +05302068 &storage_size );
Archana8a180362021-07-05 02:18:48 +05302069 if( status != PSA_SUCCESS )
2070 goto exit;
Archana374fe5b2021-09-08 15:50:28 +05302071
Archana8a180362021-07-05 02:18:48 +05302072 status = psa_allocate_buffer_to_slot( target_slot, storage_size );
2073 if( status != PSA_SUCCESS )
2074 goto exit;
Archana374fe5b2021-09-08 15:50:28 +05302075
Archana8a180362021-07-05 02:18:48 +05302076 status = psa_driver_wrapper_copy_key( &actual_attributes,
2077 source_slot->key.data,
2078 source_slot->key.bytes,
2079 target_slot->key.data,
2080 target_slot->key.bytes,
2081 &target_slot->key.bytes );
2082 if( status != PSA_SUCCESS )
2083 goto exit;
Ronald Cron6cc66312021-04-02 12:27:47 +02002084 }
Archana8a180362021-07-05 02:18:48 +05302085 else
2086 {
Archana9d17bf42021-09-10 06:22:44 +05302087 status = psa_copy_key_material_into_slot( target_slot,
2088 source_slot->key.data,
2089 source_slot->key.bytes );
Archana8a180362021-07-05 02:18:48 +05302090 if( status != PSA_SUCCESS )
2091 goto exit;
2092 }
Ronald Cron81709fc2020-11-14 12:10:32 +01002093 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002095 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002096 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002097
Ronald Cron5c522922020-11-14 16:35:34 +01002098 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002099
Ronald Cron5c522922020-11-14 16:35:34 +01002100 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002101}
2102
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002103
2104
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002105/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002106/* Message digests */
2107/****************************************************************/
2108
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002109psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2110{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002111 /* Aborting a non-active operation is allowed */
2112 if( operation->id == 0 )
2113 return( PSA_SUCCESS );
2114
2115 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2116 operation->id = 0;
2117
2118 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002119}
2120
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002121psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122 psa_algorithm_t alg )
2123{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002124 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002125
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002126 /* A context must be freshly initialized before it can be set up. */
2127 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002128 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002129 status = PSA_ERROR_BAD_STATE;
2130 goto exit;
2131 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002132
2133 if( !PSA_ALG_IS_HASH( alg ) )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002134 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002135 status = PSA_ERROR_INVALID_ARGUMENT;
2136 goto exit;
2137 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002138
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002139 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2140 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002141 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2142
Dave Rodgman685b6a72021-06-24 11:49:14 +01002143 status = psa_driver_wrapper_hash_setup( operation, alg );
2144
2145exit:
2146 if( status != PSA_SUCCESS )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002147 psa_hash_abort( operation );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002148
2149 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002150}
2151
2152psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2153 const uint8_t *input,
2154 size_t input_length )
2155{
Dave Rodgman685b6a72021-06-24 11:49:14 +01002156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2157
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002158 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002159 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002160 status = PSA_ERROR_BAD_STATE;
2161 goto exit;
2162 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002163
Steven Cooremanc8288352021-03-04 14:02:19 +01002164 /* Don't require hash implementations to behave correctly on a
2165 * zero-length input, which may have an invalid pointer. */
2166 if( input_length == 0 )
2167 return( PSA_SUCCESS );
2168
Dave Rodgman685b6a72021-06-24 11:49:14 +01002169 status = psa_driver_wrapper_hash_update( operation, input, input_length );
2170
2171exit:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002172 if( status != PSA_SUCCESS )
2173 psa_hash_abort( operation );
2174
2175 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002176}
2177
2178psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2179 uint8_t *hash,
2180 size_t hash_size,
2181 size_t *hash_length )
2182{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002183 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002184 if( operation->id == 0 )
2185 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002186
Steven Cooreman1e582352021-02-18 17:24:37 +01002187 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002188 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002189 psa_hash_abort( operation );
2190 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002191}
2192
Gilles Peskine2d277862018-06-18 15:41:12 +02002193psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2194 const uint8_t *hash,
2195 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196{
2197 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2198 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002199 psa_status_t status = psa_hash_finish(
2200 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002201 actual_hash, sizeof( actual_hash ),
2202 &actual_hash_length );
Dave Rodgman685b6a72021-06-24 11:49:14 +01002203
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002204 if( status != PSA_SUCCESS )
Dave Rodgman685b6a72021-06-24 11:49:14 +01002205 goto exit;
2206
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002207 if( actual_hash_length != hash_length )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002208 {
Dave Rodgman685b6a72021-06-24 11:49:14 +01002209 status = PSA_ERROR_INVALID_SIGNATURE;
2210 goto exit;
2211 }
2212
Steven Cooreman36876a02021-04-29 16:37:59 +02002213 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Dave Rodgman685b6a72021-06-24 11:49:14 +01002214 status = PSA_ERROR_INVALID_SIGNATURE;
2215
2216exit:
2217 if( status != PSA_SUCCESS )
2218 psa_hash_abort(operation);
2219
2220 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221}
2222
Gilles Peskine0a749c82019-11-28 19:33:58 +01002223psa_status_t psa_hash_compute( psa_algorithm_t alg,
2224 const uint8_t *input, size_t input_length,
2225 uint8_t *hash, size_t hash_size,
2226 size_t *hash_length )
2227{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002228 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002229 if( !PSA_ALG_IS_HASH( alg ) )
2230 return( PSA_ERROR_INVALID_ARGUMENT );
2231
Steven Cooreman1e582352021-02-18 17:24:37 +01002232 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2233 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002234}
2235
2236psa_status_t psa_hash_compare( psa_algorithm_t alg,
2237 const uint8_t *input, size_t input_length,
2238 const uint8_t *hash, size_t hash_length )
2239{
Steven Cooreman84d670d2021-02-18 16:22:53 +01002240 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2241 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002242
2243 if( !PSA_ALG_IS_HASH( alg ) )
2244 return( PSA_ERROR_INVALID_ARGUMENT );
2245
Steven Cooreman1e582352021-02-18 17:24:37 +01002246 psa_status_t status = psa_driver_wrapper_hash_compute(
2247 alg, input, input_length,
2248 actual_hash, sizeof(actual_hash),
2249 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002250 if( status != PSA_SUCCESS )
Steven Cooreman84d670d2021-02-18 16:22:53 +01002251 return( status );
2252 if( actual_hash_length != hash_length )
2253 return( PSA_ERROR_INVALID_SIGNATURE );
Steven Cooreman36876a02021-04-29 16:37:59 +02002254 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Steven Cooreman84d670d2021-02-18 16:22:53 +01002255 return( PSA_ERROR_INVALID_SIGNATURE );
2256 return( PSA_SUCCESS );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002257}
2258
Gilles Peskineeb35d782019-01-22 17:56:16 +01002259psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2260 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002261{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002262 if( source_operation->id == 0 ||
2263 target_operation->id != 0 )
2264 {
2265 return( PSA_ERROR_BAD_STATE );
2266 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002267
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002268 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2269 target_operation );
2270 if( status != PSA_SUCCESS )
2271 psa_hash_abort( target_operation );
2272
2273 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002274}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275
2276
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002277/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002278/* MAC */
2279/****************************************************************/
2280
Steven Cooremane6804192021-03-19 18:28:56 +01002281psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002282{
Steven Cooremane6804192021-03-19 18:28:56 +01002283 /* Aborting a non-active operation is allowed */
2284 if( operation->id == 0 )
2285 return( PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002286
Steven Cooremane6804192021-03-19 18:28:56 +01002287 psa_status_t status = psa_driver_wrapper_mac_abort( operation );
Steven Cooreman72f736a2021-05-07 14:14:37 +02002288 operation->mac_size = 0;
2289 operation->is_sign = 0;
Steven Cooremane6804192021-03-19 18:28:56 +01002290 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002291
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002292 return( status );
2293}
2294
Ronald Cron2dff3b22021-06-17 16:33:22 +02002295static psa_status_t psa_mac_finalize_alg_and_key_validation(
2296 psa_algorithm_t alg,
Ronald Cron79bdd822021-06-17 16:46:44 +02002297 const psa_key_attributes_t *attributes,
2298 uint8_t *mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002299{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron79bdd822021-06-17 16:46:44 +02002301 psa_key_type_t key_type = psa_get_key_type( attributes );
2302 size_t key_bits = psa_get_key_bits( attributes );
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002303
Ronald Cron28ea0502021-06-17 16:10:24 +02002304 if( ! PSA_ALG_IS_MAC( alg ) )
Ronald Cron79bdd822021-06-17 16:46:44 +02002305 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremane6804192021-03-19 18:28:56 +01002306
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002307 /* Validate the combination of key type and algorithm */
Ronald Cron79bdd822021-06-17 16:46:44 +02002308 status = psa_mac_key_can_do( alg, key_type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002309 if( status != PSA_SUCCESS )
Ronald Cron79bdd822021-06-17 16:46:44 +02002310 return( status );
Steven Cooreman15472f82021-03-02 16:16:22 +01002311
Steven Cooremand1a68f12021-05-10 11:13:41 +02002312 /* Get the output length for the algorithm and key combination */
Ronald Cron79bdd822021-06-17 16:46:44 +02002313 *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002314
Ronald Cron79bdd822021-06-17 16:46:44 +02002315 if( *mac_size < 4 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002316 {
2317 /* A very short MAC is too short for security since it can be
2318 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2319 * so we make this our minimum, even though 32 bits is still
2320 * too small for security. */
Ronald Cron79bdd822021-06-17 16:46:44 +02002321 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman15472f82021-03-02 16:16:22 +01002322 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002323
Ronald Cron79bdd822021-06-17 16:46:44 +02002324 if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits,
2325 PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002326 {
2327 /* It's impossible to "truncate" to a larger length than the full length
2328 * of the algorithm. */
Ronald Cron79bdd822021-06-17 16:46:44 +02002329 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002330 }
2331
Ronald Cron79bdd822021-06-17 16:46:44 +02002332 return( PSA_SUCCESS );
Ronald Cron2dff3b22021-06-17 16:33:22 +02002333}
2334
Gilles Peskinee59236f2018-01-27 23:32:46 +01002335static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
2336 mbedtls_svc_key_id_t key,
2337 psa_algorithm_t alg,
2338 int is_sign )
2339{
2340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2341 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01002342 psa_key_slot_t *slot = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002343
2344 /* A context must be freshly initialized before it can be set up. */
2345 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002346 {
Dave Rodgman54648242021-06-24 11:49:45 +01002347 status = PSA_ERROR_BAD_STATE;
2348 goto exit;
2349 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002350
2351 status = psa_get_and_lock_key_slot_with_policy(
2352 key,
2353 &slot,
Mateusz Starzykce0e6a92021-08-17 15:24:32 +02002354 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002355 alg );
2356 if( status != PSA_SUCCESS )
Dave Rodgman54648242021-06-24 11:49:45 +01002357 goto exit;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002358
2359 psa_key_attributes_t attributes = {
2360 .core = slot->attr
2361 };
2362
Ronald Cron79bdd822021-06-17 16:46:44 +02002363 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2364 &operation->mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002365 if( status != PSA_SUCCESS )
2366 goto exit;
2367
2368 operation->is_sign = is_sign;
Steven Cooremane6804192021-03-19 18:28:56 +01002369 /* Dispatch the MAC setup call with validated input */
2370 if( is_sign )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002371 {
Steven Cooremane6804192021-03-19 18:28:56 +01002372 status = psa_driver_wrapper_mac_sign_setup( operation,
2373 &attributes,
2374 slot->key.data,
2375 slot->key.bytes,
2376 alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002377 }
2378 else
Gilles Peskinefbfac682018-07-08 20:51:54 +02002379 {
Steven Cooremane6804192021-03-19 18:28:56 +01002380 status = psa_driver_wrapper_mac_verify_setup( operation,
2381 &attributes,
2382 slot->key.data,
2383 slot->key.bytes,
2384 alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002385 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002386
Gilles Peskinefbfac682018-07-08 20:51:54 +02002387exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002388 if( status != PSA_SUCCESS )
Steven Cooremane6804192021-03-19 18:28:56 +01002389 psa_mac_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002390
Ronald Cron5c522922020-11-14 16:35:34 +01002391 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002392
Ronald Cron5c522922020-11-14 16:35:34 +01002393 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002394}
2395
Gilles Peskine89167cb2018-07-08 20:12:23 +02002396psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002397 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002398 psa_algorithm_t alg )
2399{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002400 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002401}
2402
2403psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002404 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002405 psa_algorithm_t alg )
2406{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002407 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002408}
2409
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002410psa_status_t psa_mac_update( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002411 const uint8_t *input,
2412 size_t input_length )
2413{
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002414 if( operation->id == 0 )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002415 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002416
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002417 /* Don't require hash implementations to behave correctly on a
2418 * zero-length input, which may have an invalid pointer. */
2419 if( input_length == 0 )
2420 return( PSA_SUCCESS );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002421
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002422 psa_status_t status = psa_driver_wrapper_mac_update( operation,
2423 input, input_length );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002424 if( status != PSA_SUCCESS )
Steven Cooreman6e7f2912021-03-19 18:38:46 +01002425 psa_mac_abort( operation );
2426
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002427 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002428}
2429
Steven Cooremana5b860a2021-03-19 19:04:39 +01002430psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002431 uint8_t *mac,
2432 size_t mac_size,
2433 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002434{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002435 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2436 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002437
Steven Cooremana5b860a2021-03-19 19:04:39 +01002438 if( operation->id == 0 )
Dave Rodgman8036bdd2021-06-24 16:19:08 +01002439 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002440 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002441 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002442 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002443
Steven Cooreman72f736a2021-05-07 14:14:37 +02002444 if( ! operation->is_sign )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002445 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002446 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002447 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002448 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002449
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002450 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2451 * once all the error checks are done. */
2452 if( operation->mac_size == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002453 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002454 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002455 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002456 }
Steven Cooreman8af5c5c2021-05-11 11:09:13 +02002457
2458 if( mac_size < operation->mac_size )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002459 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002460 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002461 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002462 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002463
Steven Cooremana5b860a2021-03-19 19:04:39 +01002464 status = psa_driver_wrapper_mac_sign_finish( operation,
Steven Cooreman72f736a2021-05-07 14:14:37 +02002465 mac, operation->mac_size,
2466 mac_length );
2467
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002468exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002469 /* In case of success, set the potential excess room in the output buffer
2470 * to an invalid value, to avoid potentially leaking a longer MAC.
2471 * In case of error, set the output length and content to a safe default,
2472 * such that in case the caller misses an error check, the output would be
2473 * an unachievable MAC.
2474 */
2475 if( status != PSA_SUCCESS )
Steven Cooreman72f736a2021-05-07 14:14:37 +02002476 {
Steven Cooreman72f736a2021-05-07 14:14:37 +02002477 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002478 operation->mac_size = 0;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002479 }
mohammad16036df908f2018-04-02 08:34:15 -07002480
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002481 if( mac_size > operation->mac_size )
2482 memset( &mac[operation->mac_size], '!',
2483 mac_size - operation->mac_size );
2484
Steven Cooremana5b860a2021-03-19 19:04:39 +01002485 abort_status = psa_mac_abort( operation );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002486
Steven Cooremana5b860a2021-03-19 19:04:39 +01002487 return( status == PSA_SUCCESS ? abort_status : status );
mohammad16036df908f2018-04-02 08:34:15 -07002488}
2489
Steven Cooremana5b860a2021-03-19 19:04:39 +01002490psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002491 const uint8_t *mac,
2492 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493{
Steven Cooremana5b860a2021-03-19 19:04:39 +01002494 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2495 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002496
Steven Cooremana5b860a2021-03-19 19:04:39 +01002497 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002498 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002499 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002500 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002501 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002502
Steven Cooreman72f736a2021-05-07 14:14:37 +02002503 if( operation->is_sign )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01002504 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002505 status = PSA_ERROR_BAD_STATE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002506 goto exit;
Dave Rodgman38e62ae2021-06-23 11:38:39 +01002507 }
Steven Cooreman72f736a2021-05-07 14:14:37 +02002508
2509 if( operation->mac_size != mac_length )
2510 {
2511 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002512 goto exit;
Steven Cooreman72f736a2021-05-07 14:14:37 +02002513 }
2514
Steven Cooremana5b860a2021-03-19 19:04:39 +01002515 status = psa_driver_wrapper_mac_verify_finish( operation,
2516 mac, mac_length );
Steven Cooreman72f736a2021-05-07 14:14:37 +02002517
Dave Rodgman90d1cb82021-06-25 09:09:02 +01002518exit:
Steven Cooremana5b860a2021-03-19 19:04:39 +01002519 abort_status = psa_mac_abort( operation );
mohammad16036df908f2018-04-02 08:34:15 -07002520
Steven Cooremana5b860a2021-03-19 19:04:39 +01002521 return( status == PSA_SUCCESS ? abort_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002522}
2523
Ronald Croncd989b52021-06-18 14:23:33 +02002524static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key,
2525 psa_algorithm_t alg,
2526 const uint8_t *input,
2527 size_t input_length,
2528 uint8_t *mac,
2529 size_t mac_size,
2530 size_t *mac_length,
2531 int is_sign )
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002532{
2533 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron51131b52021-06-17 17:17:20 +02002534 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2535 psa_key_slot_t *slot;
2536 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537
Ronald Cron51131b52021-06-17 17:17:20 +02002538 status = psa_get_and_lock_key_slot_with_policy(
Mateusz Starzykce0e6a92021-08-17 15:24:32 +02002539 key,
2540 &slot,
2541 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Croncd989b52021-06-18 14:23:33 +02002542 alg );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002543 if( status != PSA_SUCCESS )
2544 goto exit;
2545
Ronald Cron51131b52021-06-17 17:17:20 +02002546 psa_key_attributes_t attributes = {
2547 .core = slot->attr
2548 };
2549
2550 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2551 &operation_mac_size );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002552 if( status != PSA_SUCCESS )
2553 goto exit;
2554
Ronald Cron51131b52021-06-17 17:17:20 +02002555 if( mac_size < operation_mac_size )
2556 {
2557 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002558 goto exit;
Ronald Cron51131b52021-06-17 17:17:20 +02002559 }
2560
2561 status = psa_driver_wrapper_mac_compute(
2562 &attributes,
2563 slot->key.data, slot->key.bytes,
2564 alg,
2565 input, input_length,
2566 mac, operation_mac_size, mac_length );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002567
2568exit:
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002569 /* In case of success, set the potential excess room in the output buffer
2570 * to an invalid value, to avoid potentially leaking a longer MAC.
2571 * In case of error, set the output length and content to a safe default,
2572 * such that in case the caller misses an error check, the output would be
2573 * an unachievable MAC.
2574 */
2575 if( status != PSA_SUCCESS )
Ronald Cron51131b52021-06-17 17:17:20 +02002576 {
Ronald Cron51131b52021-06-17 17:17:20 +02002577 *mac_length = mac_size;
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002578 operation_mac_size = 0;
Ronald Cron51131b52021-06-17 17:17:20 +02002579 }
Ronald Cronc3dd75f2021-06-18 13:05:48 +02002580 if( mac_size > operation_mac_size )
2581 memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002582
Ronald Cron51131b52021-06-17 17:17:20 +02002583 unlock_status = psa_unlock_key_slot( slot );
2584
2585 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002586}
2587
Ronald Croncd989b52021-06-18 14:23:33 +02002588psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key,
2589 psa_algorithm_t alg,
2590 const uint8_t *input,
2591 size_t input_length,
2592 uint8_t *mac,
2593 size_t mac_size,
2594 size_t *mac_length)
2595{
2596 return( psa_mac_compute_internal( key, alg,
2597 input, input_length,
2598 mac, mac_size, mac_length, 1 ) );
2599}
2600
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002601psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key,
2602 psa_algorithm_t alg,
2603 const uint8_t *input,
2604 size_t input_length,
2605 const uint8_t *mac,
2606 size_t mac_length)
2607{
2608 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona587cbc2021-06-18 14:51:29 +02002609 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2610 size_t actual_mac_length;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002611
Ronald Crona587cbc2021-06-18 14:51:29 +02002612 status = psa_mac_compute_internal( key, alg,
2613 input, input_length,
2614 actual_mac, sizeof( actual_mac ),
2615 &actual_mac_length, 0 );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002616 if( status != PSA_SUCCESS )
2617 goto exit;
2618
Ronald Crona587cbc2021-06-18 14:51:29 +02002619 if( mac_length != actual_mac_length )
2620 {
2621 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002622 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002623 }
2624 if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
2625 {
2626 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002627 goto exit;
Ronald Crona587cbc2021-06-18 14:51:29 +02002628 }
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002629
2630exit:
Ronald Crona587cbc2021-06-18 14:51:29 +02002631 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
gabor-mezei-arm4076d3e2021-03-01 15:34:18 +01002632
2633 return ( status );
2634}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002635
Gilles Peskinee59236f2018-01-27 23:32:46 +01002636/****************************************************************/
2637/* Asymmetric cryptography */
2638/****************************************************************/
2639
gabor-mezei-armf0486182021-05-12 11:03:09 +02002640static psa_status_t psa_sign_verify_check_alg( int input_is_message,
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002641 psa_algorithm_t alg )
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002642{
gabor-mezei-armf0486182021-05-12 11:03:09 +02002643 if( input_is_message )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002644 {
2645 if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) )
2646 return( PSA_ERROR_INVALID_ARGUMENT );
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002647
Gilles Peskinef7b41372021-09-22 16:15:05 +02002648 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002649 {
2650 if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) )
2651 return( PSA_ERROR_INVALID_ARGUMENT );
2652 }
2653 }
2654 else
2655 {
Mateusz Starzyke6d3eda2021-08-26 11:46:14 +02002656 if( ! PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002657 return( PSA_ERROR_INVALID_ARGUMENT );
2658 }
2659
2660 return( PSA_SUCCESS );
2661}
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002662
2663static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002664 int input_is_message,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002665 psa_algorithm_t alg,
2666 const uint8_t * input,
2667 size_t input_length,
2668 uint8_t * signature,
2669 size_t signature_size,
2670 size_t * signature_length )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002671{
2672 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2673 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2674 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002675
2676 *signature_length = 0;
2677
gabor-mezei-armf0486182021-05-12 11:03:09 +02002678 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002679 if( status != PSA_SUCCESS )
2680 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002681
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002682 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12b4f342021-05-05 13:54:55 +02002683 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002684 * buffer can in principle be empty since it doesn't actually have
2685 * to be a hash.) */
2686 if( signature_size == 0 )
2687 return( PSA_ERROR_BUFFER_TOO_SMALL );
2688
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002689 status = psa_get_and_lock_key_slot_with_policy(
2690 key, &slot,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002691 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2692 PSA_KEY_USAGE_SIGN_HASH,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002693 alg );
2694
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002695 if( status != PSA_SUCCESS )
2696 goto exit;
2697
2698 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
2699 {
2700 status = PSA_ERROR_INVALID_ARGUMENT;
2701 goto exit;
2702 }
2703
2704 psa_key_attributes_t attributes = {
2705 .core = slot->attr
2706 };
2707
gabor-mezei-armf0486182021-05-12 11:03:09 +02002708 if( input_is_message )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002709 {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002710 status = psa_driver_wrapper_sign_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002711 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002712 alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002713 signature, signature_size, signature_length );
2714 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002715 else
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002716 {
2717
2718 status = psa_driver_wrapper_sign_hash(
2719 &attributes, slot->key.data, slot->key.bytes,
2720 alg, input, input_length,
2721 signature, signature_size, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002722 }
2723
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002724
2725exit:
2726 /* Fill the unused part of the output buffer (the whole buffer on error,
2727 * the trailing part on success) with something that isn't a valid signature
2728 * (barring an attack on the signature and deliberately-crafted input),
2729 * in case the caller doesn't check the return status properly. */
2730 if( status == PSA_SUCCESS )
2731 memset( signature + *signature_length, '!',
2732 signature_size - *signature_length );
2733 else
2734 memset( signature, '!', signature_size );
2735 /* If signature_size is 0 then we have nothing to do. We must not call
2736 * memset because signature may be NULL in this case. */
2737
2738 unlock_status = psa_unlock_key_slot( slot );
2739
2740 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
2741}
2742
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002743static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002744 int input_is_message,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002745 psa_algorithm_t alg,
2746 const uint8_t * input,
2747 size_t input_length,
2748 const uint8_t * signature,
2749 size_t signature_length )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002750{
2751 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2752 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2753 psa_key_slot_t *slot;
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002754
gabor-mezei-armf0486182021-05-12 11:03:09 +02002755 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002756 if( status != PSA_SUCCESS )
2757 return status;
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002758
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002759 status = psa_get_and_lock_key_slot_with_policy(
2760 key, &slot,
gabor-mezei-armf0486182021-05-12 11:03:09 +02002761 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2762 PSA_KEY_USAGE_VERIFY_HASH,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002763 alg );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002764
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002765 if( status != PSA_SUCCESS )
2766 return( status );
2767
2768 psa_key_attributes_t attributes = {
2769 .core = slot->attr
2770 };
2771
gabor-mezei-armf0486182021-05-12 11:03:09 +02002772 if( input_is_message )
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002773 {
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002774 status = psa_driver_wrapper_verify_message(
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002775 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002776 alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002777 signature, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002778 }
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002779 else
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002780 {
2781 status = psa_driver_wrapper_verify_hash(
2782 &attributes, slot->key.data, slot->key.bytes,
2783 alg, input, input_length,
2784 signature, signature_length );
2785 }
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002786
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002787 unlock_status = psa_unlock_key_slot( slot );
2788
2789 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002790
2791}
2792
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002793psa_status_t psa_sign_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002794 const psa_key_attributes_t *attributes,
2795 const uint8_t *key_buffer,
2796 size_t key_buffer_size,
2797 psa_algorithm_t alg,
2798 const uint8_t *input,
2799 size_t input_length,
2800 uint8_t *signature,
2801 size_t signature_size,
2802 size_t *signature_length )
2803{
2804 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002805
Gilles Peskinef7b41372021-09-22 16:15:05 +02002806 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002807 {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002808 size_t hash_length;
2809 uint8_t hash[PSA_HASH_MAX_SIZE];
2810
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002811 status = psa_driver_wrapper_hash_compute(
2812 PSA_ALG_SIGN_GET_HASH( alg ),
2813 input, input_length,
2814 hash, sizeof( hash ), &hash_length );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002815
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002816 if( status != PSA_SUCCESS )
2817 return status;
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002818
2819 return psa_driver_wrapper_sign_hash(
2820 attributes, key_buffer, key_buffer_size,
2821 alg, hash, hash_length,
2822 signature, signature_size, signature_length );
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002823 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002824
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002825 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002826}
2827
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002828psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2829 psa_algorithm_t alg,
2830 const uint8_t * input,
2831 size_t input_length,
2832 uint8_t * signature,
2833 size_t signature_size,
2834 size_t * signature_length )
2835{
2836 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002837 key, 1, alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002838 signature, signature_size, signature_length );
2839}
2840
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002841psa_status_t psa_verify_message_builtin(
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002842 const psa_key_attributes_t *attributes,
2843 const uint8_t *key_buffer,
2844 size_t key_buffer_size,
2845 psa_algorithm_t alg,
2846 const uint8_t *input,
2847 size_t input_length,
2848 const uint8_t *signature,
2849 size_t signature_length )
2850{
2851 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002852
Gilles Peskinef7b41372021-09-22 16:15:05 +02002853 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002854 {
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002855 size_t hash_length;
2856 uint8_t hash[PSA_HASH_MAX_SIZE];
2857
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002858 status = psa_driver_wrapper_hash_compute(
2859 PSA_ALG_SIGN_GET_HASH( alg ),
2860 input, input_length,
2861 hash, sizeof( hash ), &hash_length );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002862
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002863 if( status != PSA_SUCCESS )
2864 return status;
gabor-mezei-armf9820f92021-05-03 16:26:20 +02002865
2866 return psa_driver_wrapper_verify_hash(
2867 attributes, key_buffer, key_buffer_size,
2868 alg, hash, hash_length,
2869 signature, signature_length );
gabor-mezei-arm0f622402021-04-29 16:48:44 +02002870 }
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002871
gabor-mezei-arm474a35f2021-05-05 13:59:01 +02002872 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm50eac352021-04-22 11:32:19 +02002873}
2874
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002875psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
2876 psa_algorithm_t alg,
2877 const uint8_t * input,
2878 size_t input_length,
2879 const uint8_t * signature,
2880 size_t signature_length )
2881{
2882 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002883 key, 1, alg, input, input_length,
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002884 signature, signature_length );
gabor-mezei-arm4a210192021-04-14 21:14:28 +02002885}
2886
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002887psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002888 const psa_key_attributes_t *attributes,
2889 const uint8_t *key_buffer, size_t key_buffer_size,
2890 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2891 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002892{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002893 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002894 {
Ronald Cron4501c982021-03-19 20:09:32 +01002895 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2896 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002897 {
Ronald Cron4501c982021-03-19 20:09:32 +01002898#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2899 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2900 return( mbedtls_psa_rsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002901 attributes,
2902 key_buffer, key_buffer_size,
2903 alg, hash, hash_length,
2904 signature, signature_size, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002905#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2906 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002907 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002908 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002909 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002910 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002911 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002912 }
2913 else
Ronald Cron4501c982021-03-19 20:09:32 +01002914 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
2915 {
2916 if( PSA_ALG_IS_ECDSA( alg ) )
2917 {
2918#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2919 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2920 return( mbedtls_psa_ecdsa_sign_hash(
2921 attributes,
2922 key_buffer, key_buffer_size,
2923 alg, hash, hash_length,
2924 signature, signature_size, signature_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002925#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2926 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron4501c982021-03-19 20:09:32 +01002927 }
2928 else
2929 {
2930 return( PSA_ERROR_INVALID_ARGUMENT );
2931 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002932 }
Ronald Cron4501c982021-03-19 20:09:32 +01002933
2934 (void)key_buffer;
2935 (void)key_buffer_size;
2936 (void)hash;
2937 (void)hash_length;
2938 (void)signature;
2939 (void)signature_size;
2940 (void)signature_length;
2941
2942 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002943}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002944
2945psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
2946 psa_algorithm_t alg,
2947 const uint8_t *hash,
2948 size_t hash_length,
2949 uint8_t *signature,
2950 size_t signature_size,
2951 size_t *signature_length )
2952{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02002953 return psa_sign_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02002954 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01002955 signature, signature_size, signature_length );
itayzafrir5c753392018-05-08 11:18:38 +03002956}
2957
gabor-mezei-arm6883fd22021-05-05 14:18:36 +02002958psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002959 const psa_key_attributes_t *attributes,
2960 const uint8_t *key_buffer, size_t key_buffer_size,
2961 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2962 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002963{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002964 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002965 {
Ronald Cron4501c982021-03-19 20:09:32 +01002966 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2967 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002968 {
Ronald Cron4501c982021-03-19 20:09:32 +01002969#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2970 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2971 return( mbedtls_psa_rsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002972 attributes,
2973 key_buffer, key_buffer_size,
2974 alg, hash, hash_length,
2975 signature, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002976#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2977 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002978 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002979 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002980 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002981 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002982 }
itayzafrir5c753392018-05-08 11:18:38 +03002983 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002984 else
Ronald Cron4501c982021-03-19 20:09:32 +01002985 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002986 {
Ronald Cron4501c982021-03-19 20:09:32 +01002987 if( PSA_ALG_IS_ECDSA( alg ) )
2988 {
2989#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2990 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2991 return( mbedtls_psa_ecdsa_verify_hash(
2992 attributes,
2993 key_buffer, key_buffer_size,
2994 alg, hash, hash_length,
2995 signature, signature_length ) );
2996#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2997 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
2998 }
2999 else
3000 {
3001 return( PSA_ERROR_INVALID_ARGUMENT );
3002 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003003 }
Ronald Cron4501c982021-03-19 20:09:32 +01003004
3005 (void)key_buffer;
3006 (void)key_buffer_size;
3007 (void)hash;
3008 (void)hash_length;
3009 (void)signature;
3010 (void)signature_length;
3011
3012 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01003013}
3014
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003015psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3016 psa_algorithm_t alg,
3017 const uint8_t *hash,
3018 size_t hash_length,
3019 const uint8_t *signature,
3020 size_t signature_length )
3021{
gabor-mezei-arm5b446522021-04-20 12:11:35 +02003022 return psa_verify_internal(
gabor-mezei-arm8b3e8862021-05-05 13:56:27 +02003023 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003024 signature, signature_length );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003025}
3026
John Durkop0e005192020-10-31 22:06:54 -07003027#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Ronald Cronea7631b2021-06-03 18:51:59 +02003028static int psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3029 mbedtls_rsa_context *rsa )
Gilles Peskine072ac562018-06-30 00:21:29 +02003030{
3031 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3032 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3033 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Ronald Cronea7631b2021-06-03 18:51:59 +02003034
3035 return( mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ) );
Gilles Peskine072ac562018-06-30 00:21:29 +02003036}
John Durkop0e005192020-10-31 22:06:54 -07003037#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003038
Ronald Croncf56a0a2020-08-04 09:51:30 +02003039psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003040 psa_algorithm_t alg,
3041 const uint8_t *input,
3042 size_t input_length,
3043 const uint8_t *salt,
3044 size_t salt_length,
3045 uint8_t *output,
3046 size_t output_size,
3047 size_t *output_length )
3048{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003050 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003051 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003052
Darryl Green5cc689a2018-07-24 15:34:10 +01003053 (void) input;
3054 (void) input_length;
3055 (void) salt;
3056 (void) output;
3057 (void) output_size;
3058
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003059 *output_length = 0;
3060
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003061 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3062 return( PSA_ERROR_INVALID_ARGUMENT );
3063
Ronald Cron5c522922020-11-14 16:35:34 +01003064 status = psa_get_and_lock_transparent_key_slot_with_policy(
3065 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003066 if( status != PSA_SUCCESS )
3067 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003068 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3069 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003070 {
3071 status = PSA_ERROR_INVALID_ARGUMENT;
3072 goto exit;
3073 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003074
John Durkop6ba40d12020-11-10 08:50:04 -08003075#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3076 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003077 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003079 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003080 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3081 slot->key.data,
3082 slot->key.bytes,
3083 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003084 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003085 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003086
3087 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003088 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003089 status = PSA_ERROR_BUFFER_TOO_SMALL;
3090 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003091 }
John Durkop0e005192020-10-31 22:06:54 -07003092#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
3094 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003095 status = mbedtls_to_psa_error(
3096 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003097 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003098 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003099 input_length,
3100 input,
3101 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003102 }
3103 else
John Durkop0e005192020-10-31 22:06:54 -07003104#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3105#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003106 if( PSA_ALG_IS_RSA_OAEP( alg ) )
3107 {
Ronald Cronea7631b2021-06-03 18:51:59 +02003108 status = mbedtls_to_psa_error(
3109 psa_rsa_oaep_set_padding_mode( alg, rsa ) );
3110 if( status != PSA_SUCCESS )
3111 goto rsa_exit;
3112
Steven Cooreman4fed4552020-08-03 14:46:03 +02003113 status = mbedtls_to_psa_error(
3114 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003115 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003116 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003117 salt, salt_length,
3118 input_length,
3119 input,
3120 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003121 }
3122 else
John Durkop0e005192020-10-31 22:06:54 -07003123#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003124 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003125 status = PSA_ERROR_INVALID_ARGUMENT;
3126 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003128rsa_exit:
3129 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003130 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003131
Steven Cooremana2371e52020-07-28 14:30:39 +02003132 mbedtls_rsa_free( rsa );
3133 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003134 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003135 else
John Durkop9814fa22020-11-04 12:28:15 -08003136#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003137 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003138 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003139 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003141
3142exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003143 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003144
Ronald Cron5c522922020-11-14 16:35:34 +01003145 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003146}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147
Ronald Croncf56a0a2020-08-04 09:51:30 +02003148psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003149 psa_algorithm_t alg,
3150 const uint8_t *input,
3151 size_t input_length,
3152 const uint8_t *salt,
3153 size_t salt_length,
3154 uint8_t *output,
3155 size_t output_size,
3156 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003157{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003158 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003159 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003160 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003161
Darryl Green5cc689a2018-07-24 15:34:10 +01003162 (void) input;
3163 (void) input_length;
3164 (void) salt;
3165 (void) output;
3166 (void) output_size;
3167
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003168 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003169
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003170 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3171 return( PSA_ERROR_INVALID_ARGUMENT );
3172
Ronald Cron5c522922020-11-14 16:35:34 +01003173 status = psa_get_and_lock_transparent_key_slot_with_policy(
3174 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003175 if( status != PSA_SUCCESS )
3176 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003177 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003178 {
3179 status = PSA_ERROR_INVALID_ARGUMENT;
3180 goto exit;
3181 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182
John Durkop6ba40d12020-11-10 08:50:04 -08003183#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3184 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003185 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003186 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003187 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003188 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3189 slot->key.data,
3190 slot->key.bytes,
3191 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003192 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003193 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194
Steven Cooremana2371e52020-07-28 14:30:39 +02003195 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003196 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003197 status = PSA_ERROR_INVALID_ARGUMENT;
3198 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003199 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003200
John Durkop0e005192020-10-31 22:06:54 -07003201#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003202 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003203 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003204 status = mbedtls_to_psa_error(
3205 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003206 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003207 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003208 output_length,
3209 input,
3210 output,
3211 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212 }
3213 else
John Durkop0e005192020-10-31 22:06:54 -07003214#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3215#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003216 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003217 {
Ronald Cronea7631b2021-06-03 18:51:59 +02003218 status = mbedtls_to_psa_error(
3219 psa_rsa_oaep_set_padding_mode( alg, rsa ) );
3220 if( status != PSA_SUCCESS )
3221 goto rsa_exit;
3222
Steven Cooreman4fed4552020-08-03 14:46:03 +02003223 status = mbedtls_to_psa_error(
3224 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003225 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003226 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003227 salt, salt_length,
3228 output_length,
3229 input,
3230 output,
3231 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003232 }
3233 else
John Durkop0e005192020-10-31 22:06:54 -07003234#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003235 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003236 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003238
Steven Cooreman4fed4552020-08-03 14:46:03 +02003239rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003240 mbedtls_rsa_free( rsa );
3241 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003243 else
John Durkop6ba40d12020-11-10 08:50:04 -08003244#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3245 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003246 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003247 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003248 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003249
3250exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003251 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003252
Ronald Cron5c522922020-11-14 16:35:34 +01003253 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003254}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003255
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003256
3257
mohammad1603503973b2018-03-12 15:59:30 +02003258/****************************************************************/
3259/* Symmetric cryptography */
3260/****************************************************************/
3261
Ronald Cronab99ac22020-10-01 16:38:28 +02003262static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
3263 mbedtls_svc_key_id_t key,
3264 psa_algorithm_t alg,
3265 mbedtls_operation_t cipher_operation )
3266{
3267 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3268 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgman54648242021-06-24 11:49:45 +01003269 psa_key_slot_t *slot = NULL;
Ronald Cronab99ac22020-10-01 16:38:28 +02003270 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3271 PSA_KEY_USAGE_ENCRYPT :
3272 PSA_KEY_USAGE_DECRYPT );
3273
3274 /* A context must be freshly initialized before it can be set up. */
Ronald Cron49fafa92021-03-10 08:34:23 +01003275 if( operation->id != 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003276 {
Dave Rodgman54648242021-06-24 11:49:45 +01003277 status = PSA_ERROR_BAD_STATE;
3278 goto exit;
3279 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003280
Ronald Cronab99ac22020-10-01 16:38:28 +02003281 if( ! PSA_ALG_IS_CIPHER( alg ) )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003282 {
Dave Rodgman54648242021-06-24 11:49:45 +01003283 status = PSA_ERROR_INVALID_ARGUMENT;
3284 goto exit;
3285 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003286
Ronald Cronab99ac22020-10-01 16:38:28 +02003287 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
3288 if( status != PSA_SUCCESS )
3289 goto exit;
3290
Ronald Cron49fafa92021-03-10 08:34:23 +01003291 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003292 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003293 * so we only set it (in the driver wrapper) after resources have been
3294 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003295 operation->iv_set = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003296 if( alg == PSA_ALG_ECB_NO_PADDING )
3297 operation->iv_required = 0;
3298 else
3299 operation->iv_required = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003300 operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
Ronald Cronab99ac22020-10-01 16:38:28 +02003301
Ronald Crona4af55f2020-12-14 14:36:06 +01003302 psa_key_attributes_t attributes = {
3303 .core = slot->attr
3304 };
3305
Ronald Cronab99ac22020-10-01 16:38:28 +02003306 /* Try doing the operation through a driver before using software fallback. */
3307 if( cipher_operation == MBEDTLS_ENCRYPT )
Ronald Crona4af55f2020-12-14 14:36:06 +01003308 status = psa_driver_wrapper_cipher_encrypt_setup( operation,
3309 &attributes,
3310 slot->key.data,
3311 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003312 alg );
3313 else
Ronald Crona4af55f2020-12-14 14:36:06 +01003314 status = psa_driver_wrapper_cipher_decrypt_setup( operation,
3315 &attributes,
3316 slot->key.data,
3317 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003318 alg );
3319
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003320exit:
Ronald Cron06aa4422021-03-09 17:32:57 +01003321 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003322 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003323
Ronald Cron5c522922020-11-14 16:35:34 +01003324 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003325
Ronald Cron5c522922020-11-14 16:35:34 +01003326 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003327}
3328
Gilles Peskinefe119512018-07-08 21:39:34 +02003329psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003330 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003331 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003332{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003333 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003334}
3335
Gilles Peskinefe119512018-07-08 21:39:34 +02003336psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003337 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003338 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003339{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003340 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003341}
3342
Gilles Peskinefe119512018-07-08 21:39:34 +02003343psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003344 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003345 size_t iv_size,
3346 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003347{
Ronald Cron6d051732020-10-01 14:10:20 +02003348 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003349
Ronald Cron5618a392021-03-26 09:52:26 +01003350 *iv_length = 0;
3351
Ronald Cron49fafa92021-03-10 08:34:23 +01003352 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003353 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003354 status = PSA_ERROR_BAD_STATE;
3355 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003356 }
3357
Steven Cooreman7df02922020-09-09 15:28:49 +02003358 if( operation->iv_set || ! operation->iv_required )
3359 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003360 status = PSA_ERROR_BAD_STATE;
3361 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003362 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003363
Ronald Cron5618a392021-03-26 09:52:26 +01003364 if( iv_size < operation->default_iv_length )
3365 {
3366 status = PSA_ERROR_BUFFER_TOO_SMALL;
3367 goto exit;
3368 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003369
Ronald Cron5618a392021-03-26 09:52:26 +01003370 status = psa_generate_random( iv, operation->default_iv_length );
3371 if( status != PSA_SUCCESS )
3372 goto exit;
3373
3374 status = psa_driver_wrapper_cipher_set_iv( operation,
3375 iv,
3376 operation->default_iv_length );
3377
3378exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003379 if( status == PSA_SUCCESS )
Ronald Cron5618a392021-03-26 09:52:26 +01003380 {
Steven Cooreman7df02922020-09-09 15:28:49 +02003381 operation->iv_set = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003382 *iv_length = operation->default_iv_length;
3383 }
Steven Cooreman7df02922020-09-09 15:28:49 +02003384 else
Moran Peker395db872018-05-31 14:07:14 +03003385 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003386
itayzafrir534bd7c2018-08-02 13:56:32 +03003387 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003388}
3389
Gilles Peskinefe119512018-07-08 21:39:34 +02003390psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003391 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003392 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003393{
Ronald Cron6d051732020-10-01 14:10:20 +02003394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003395
Ronald Cron49fafa92021-03-10 08:34:23 +01003396 if( operation->id == 0 )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003397 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003398 status = PSA_ERROR_BAD_STATE;
3399 goto exit;
3400 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003401
Steven Cooreman7df02922020-09-09 15:28:49 +02003402 if( operation->iv_set || ! operation->iv_required )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003403 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003404 status = PSA_ERROR_BAD_STATE;
3405 goto exit;
3406 }
Ronald Crona0d68172021-03-26 10:15:08 +01003407
3408 if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
Dave Rodgmanb5dd7c72021-06-24 16:17:43 +01003409 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003410 status = PSA_ERROR_INVALID_ARGUMENT;
3411 goto exit;
3412 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003413
Ronald Crondd24c9b2020-12-15 14:10:01 +01003414 status = psa_driver_wrapper_cipher_set_iv( operation,
3415 iv,
3416 iv_length );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003417
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003418exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003419 if( status == PSA_SUCCESS )
3420 operation->iv_set = 1;
3421 else
mohammad1603503973b2018-03-12 15:59:30 +02003422 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003423 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003424}
3425
Gilles Peskinee553c652018-06-04 16:22:46 +02003426psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3427 const uint8_t *input,
3428 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003429 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003430 size_t output_size,
3431 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003432{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003433 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003434
Ronald Cron49fafa92021-03-10 08:34:23 +01003435 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003436 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003437 status = PSA_ERROR_BAD_STATE;
3438 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003439 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003440
Steven Cooreman7df02922020-09-09 15:28:49 +02003441 if( operation->iv_required && ! operation->iv_set )
3442 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003443 status = PSA_ERROR_BAD_STATE;
3444 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003445 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003446
Ronald Crondd24c9b2020-12-15 14:10:01 +01003447 status = psa_driver_wrapper_cipher_update( operation,
3448 input,
3449 input_length,
3450 output,
3451 output_size,
3452 output_length );
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003453
3454exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003455 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003456 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003457
itayzafrir534bd7c2018-08-02 13:56:32 +03003458 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003459}
3460
Gilles Peskinee553c652018-06-04 16:22:46 +02003461psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3462 uint8_t *output,
3463 size_t output_size,
3464 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003465{
David Saadab4ecc272019-02-14 13:48:10 +02003466 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003467
Ronald Cron49fafa92021-03-10 08:34:23 +01003468 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003469 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003470 status = PSA_ERROR_BAD_STATE;
3471 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003472 }
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003473
Steven Cooreman7df02922020-09-09 15:28:49 +02003474 if( operation->iv_required && ! operation->iv_set )
3475 {
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003476 status = PSA_ERROR_BAD_STATE;
3477 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003478 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003479
Ronald Crondd24c9b2020-12-15 14:10:01 +01003480 status = psa_driver_wrapper_cipher_finish( operation,
3481 output,
3482 output_size,
3483 output_length );
Dave Rodgman38e62ae2021-06-23 11:38:39 +01003484
3485exit:
Steven Cooremanef8575e2020-09-11 11:44:50 +02003486 if( status == PSA_SUCCESS )
3487 return( psa_cipher_abort( operation ) );
3488 else
3489 {
3490 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003491 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003492
Steven Cooremanef8575e2020-09-11 11:44:50 +02003493 return( status );
3494 }
mohammad1603503973b2018-03-12 15:59:30 +02003495}
3496
Gilles Peskinee553c652018-06-04 16:22:46 +02003497psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3498{
Ronald Cron49fafa92021-03-10 08:34:23 +01003499 if( operation->id == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003500 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003501 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003502 * in use. It's ok to call abort on such an object, and there's
3503 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003504 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003505 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003506
Ronald Crondd24c9b2020-12-15 14:10:01 +01003507 psa_driver_wrapper_cipher_abort( operation );
Gilles Peskinee553c652018-06-04 16:22:46 +02003508
Ronald Cron49fafa92021-03-10 08:34:23 +01003509 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003510 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003511 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003512
Moran Peker395db872018-05-31 14:07:14 +03003513 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003514}
3515
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003516psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key,
3517 psa_algorithm_t alg,
3518 const uint8_t *input,
3519 size_t input_length,
3520 uint8_t *output,
3521 size_t output_size,
3522 size_t *output_length )
3523{
3524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003525 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3526 psa_key_slot_t *slot;
3527 psa_key_type_t key_type;
3528 size_t iv_length;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003529
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003530 *output_length = 0;
3531
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003532 if( ! PSA_ALG_IS_CIPHER( alg ) )
3533 return( PSA_ERROR_INVALID_ARGUMENT );
3534
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003535 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3536 PSA_KEY_USAGE_ENCRYPT,
3537 alg );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003538 if( status != PSA_SUCCESS )
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003539 return( status );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003540
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003541 psa_key_attributes_t attributes = {
3542 .core = slot->attr
3543 };
3544
3545 key_type = slot->attr.type;
3546 iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg );
3547
3548 if( iv_length > 0 )
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003549 {
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003550 if( output_size < iv_length )
3551 {
3552 status = PSA_ERROR_BUFFER_TOO_SMALL;
3553 goto exit;
3554 }
3555
3556 status = psa_generate_random( output, iv_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003557 if( status != PSA_SUCCESS )
3558 goto exit;
3559 }
3560
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003561 status = psa_driver_wrapper_cipher_encrypt(
3562 &attributes, slot->key.data, slot->key.bytes,
3563 alg, input, input_length,
3564 output, output_size, output_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003565
3566exit:
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003567 unlock_status = psa_unlock_key_slot( slot );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003568
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003569 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003570}
3571
3572psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key,
3573 psa_algorithm_t alg,
3574 const uint8_t *input,
3575 size_t input_length,
3576 uint8_t *output,
3577 size_t output_size,
3578 size_t *output_length )
3579{
3580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003581 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3582 psa_key_slot_t *slot;
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003583
gabor-mezei-arm6f4e5bb2021-06-25 15:21:11 +02003584 *output_length = 0;
3585
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003586 if( ! PSA_ALG_IS_CIPHER( alg ) )
3587 return( PSA_ERROR_INVALID_ARGUMENT );
3588
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003589 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3590 PSA_KEY_USAGE_DECRYPT,
3591 alg );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003592 if( status != PSA_SUCCESS )
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003593 return( status );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003594
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003595 psa_key_attributes_t attributes = {
3596 .core = slot->attr
3597 };
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003598
Mateusz Starzyk594215b2021-10-14 12:23:06 +02003599 if( alg == PSA_ALG_CCM_STAR_NO_TAG && input_length < PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) )
3600 {
3601 status = PSA_ERROR_INVALID_ARGUMENT;
3602 goto exit;
3603 }
3604 else if ( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) )
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003605 {
3606 status = PSA_ERROR_INVALID_ARGUMENT;
3607 goto exit;
3608 }
3609
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003610 status = psa_driver_wrapper_cipher_decrypt(
3611 &attributes, slot->key.data, slot->key.bytes,
3612 alg, input, input_length,
3613 output, output_size, output_length );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003614
gabor-mezei-arm258ae072021-06-25 15:25:38 +02003615exit:
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003616 unlock_status = psa_unlock_key_slot( slot );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003617
gabor-mezei-arma9449a02021-03-25 11:17:10 +01003618 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-armba0fa752021-03-01 15:04:24 +01003619}
3620
3621
mohammad16035955c982018-04-26 00:53:03 +03003622/****************************************************************/
3623/* AEAD */
3624/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003625
Paul Elliottbaff51c2021-09-28 17:44:45 +01003626/* Helper function to get the base algorithm from its variants. */
3627static psa_algorithm_t psa_aead_get_base_algorithm( psa_algorithm_t alg )
3628{
3629 return PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( alg );
3630}
3631
3632/* Helper function to perform common nonce length checks. */
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003633static psa_status_t psa_aead_check_nonce_length( psa_algorithm_t alg,
3634 size_t nonce_length )
3635{
Paul Elliottbaff51c2021-09-28 17:44:45 +01003636 psa_algorithm_t base_alg = psa_aead_get_base_algorithm( alg );
3637
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003638 switch(base_alg)
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003639 {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003640#if defined(PSA_WANT_ALG_GCM)
3641 case PSA_ALG_GCM:
3642 /* Not checking max nonce size here as GCM spec allows almost
3643 * arbitrarily large nonces. Please note that we do not generally
3644 * recommend the usage of nonces of greater length than
3645 * PSA_AEAD_NONCE_MAX_SIZE, as large nonces are hashed to a shorter
3646 * size, which can then lead to collisions if you encrypt a very
3647 * large number of messages.*/
3648 if( nonce_length != 0 )
3649 return( PSA_SUCCESS );
3650 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003651#endif /* PSA_WANT_ALG_GCM */
3652#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003653 case PSA_ALG_CCM:
3654 if( nonce_length >= 7 && nonce_length <= 13 )
3655 return( PSA_SUCCESS );
3656 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003657#endif /* PSA_WANT_ALG_CCM */
3658#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003659 case PSA_ALG_CHACHA20_POLY1305:
3660 if( nonce_length == 12 )
3661 return( PSA_SUCCESS );
Bence Szépkúti6d48e202021-11-15 20:04:15 +01003662 else if( nonce_length == 8 )
3663 return( PSA_ERROR_NOT_SUPPORTED );
3664 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003665#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003666 default:
Bence Szépkúti357b78e2021-11-09 13:17:17 +01003667 return( PSA_ERROR_NOT_SUPPORTED );
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003668 }
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003669
Bence Szépkúti357b78e2021-11-09 13:17:17 +01003670 return( PSA_ERROR_INVALID_ARGUMENT );
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003671}
3672
Ronald Croncf56a0a2020-08-04 09:51:30 +02003673psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003674 psa_algorithm_t alg,
3675 const uint8_t *nonce,
3676 size_t nonce_length,
3677 const uint8_t *additional_data,
3678 size_t additional_data_length,
3679 const uint8_t *plaintext,
3680 size_t plaintext_length,
3681 uint8_t *ciphertext,
3682 size_t ciphertext_size,
3683 size_t *ciphertext_length )
3684{
Ronald Cron215633c2021-03-16 17:15:37 +01003685 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3686 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003687
mohammad1603f08a5502018-06-03 15:05:47 +03003688 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003689
Steven Cooremanea7ab132021-03-17 16:28:00 +01003690 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3691 return( PSA_ERROR_NOT_SUPPORTED );
3692
Ronald Cron9a986162021-03-26 12:40:07 +01003693 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003694 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003695 if( status != PSA_SUCCESS )
3696 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003697
Ronald Cron215633c2021-03-16 17:15:37 +01003698 psa_key_attributes_t attributes = {
3699 .core = slot->attr
3700 };
mohammad16035955c982018-04-26 00:53:03 +03003701
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003702 status = psa_aead_check_nonce_length( alg, nonce_length );
3703 if( status != PSA_SUCCESS )
3704 goto exit;
3705
Ronald Cronde822812021-03-17 16:08:20 +01003706 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003707 &attributes, slot->key.data, slot->key.bytes,
3708 alg,
3709 nonce, nonce_length,
3710 additional_data, additional_data_length,
3711 plaintext, plaintext_length,
3712 ciphertext, ciphertext_size, ciphertext_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003713
Gilles Peskineedf9a652018-08-17 18:11:56 +02003714 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3715 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003716
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003717exit:
Ronald Cron9f310172021-03-16 16:36:37 +01003718 psa_unlock_key_slot( slot );
mohammad16035955c982018-04-26 00:53:03 +03003719
mohammad16035955c982018-04-26 00:53:03 +03003720 return( status );
Gilles Peskineee652a32018-06-01 19:23:52 +02003721}
3722
Ronald Croncf56a0a2020-08-04 09:51:30 +02003723psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003724 psa_algorithm_t alg,
3725 const uint8_t *nonce,
3726 size_t nonce_length,
3727 const uint8_t *additional_data,
3728 size_t additional_data_length,
3729 const uint8_t *ciphertext,
3730 size_t ciphertext_length,
3731 uint8_t *plaintext,
3732 size_t plaintext_size,
3733 size_t *plaintext_length )
3734{
Ronald Cron215633c2021-03-16 17:15:37 +01003735 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3736 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003737
Gilles Peskineee652a32018-06-01 19:23:52 +02003738 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003739
Steven Cooremanea7ab132021-03-17 16:28:00 +01003740 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3741 return( PSA_ERROR_NOT_SUPPORTED );
3742
Ronald Cron9a986162021-03-26 12:40:07 +01003743 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003744 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003745 if( status != PSA_SUCCESS )
3746 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003747
Ronald Cron215633c2021-03-16 17:15:37 +01003748 psa_key_attributes_t attributes = {
3749 .core = slot->attr
3750 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003751
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003752 status = psa_aead_check_nonce_length( alg, nonce_length );
3753 if( status != PSA_SUCCESS )
3754 goto exit;
3755
Ronald Cronde822812021-03-17 16:08:20 +01003756 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003757 &attributes, slot->key.data, slot->key.bytes,
3758 alg,
3759 nonce, nonce_length,
3760 additional_data, additional_data_length,
3761 ciphertext, ciphertext_length,
3762 plaintext, plaintext_size, plaintext_length );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003763
Gilles Peskineedf9a652018-08-17 18:11:56 +02003764 if( status != PSA_SUCCESS && plaintext_size != 0 )
3765 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003766
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003767exit:
Ronald Cron215633c2021-03-16 17:15:37 +01003768 psa_unlock_key_slot( slot );
3769
Gilles Peskineedf9a652018-08-17 18:11:56 +02003770 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003771}
3772
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003773/* Set the key for a multipart authenticated operation. */
3774static psa_status_t psa_aead_setup( psa_aead_operation_t *operation,
Paul Elliottd9343f22021-08-23 18:59:49 +01003775 int is_encrypt,
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003776 mbedtls_svc_key_id_t key,
3777 psa_algorithm_t alg )
Paul Elliottc2b71442021-06-24 18:17:52 +01003778{
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003779 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3780 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3781 psa_key_slot_t *slot = NULL;
3782 psa_key_usage_t key_usage = 0;
3783
Paul Elliottc2b71442021-06-24 18:17:52 +01003784 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3785 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003786 status = PSA_ERROR_INVALID_ARGUMENT;
3787 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003788 }
3789
3790 if( operation->id != 0 )
3791 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003792 status = PSA_ERROR_BAD_STATE;
3793 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003794 }
3795
3796 if( operation->nonce_set || operation->lengths_set ||
3797 operation->ad_started || operation->body_started )
3798 {
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003799 status = PSA_ERROR_BAD_STATE;
3800 goto exit;
Paul Elliottc2b71442021-06-24 18:17:52 +01003801 }
3802
Paul Elliottd9343f22021-08-23 18:59:49 +01003803 if( is_encrypt )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003804 key_usage = PSA_KEY_USAGE_ENCRYPT;
3805 else
3806 key_usage = PSA_KEY_USAGE_DECRYPT;
3807
3808 status = psa_get_and_lock_key_slot_with_policy( key, &slot, key_usage,
3809 alg );
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003810 if( status != PSA_SUCCESS )
3811 goto exit;
3812
3813 psa_key_attributes_t attributes = {
3814 .core = slot->attr
3815 };
3816
Paul Elliottd9343f22021-08-23 18:59:49 +01003817 if( is_encrypt )
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003818 status = psa_driver_wrapper_aead_encrypt_setup( operation,
3819 &attributes,
3820 slot->key.data,
3821 slot->key.bytes,
3822 alg );
3823 else
3824 status = psa_driver_wrapper_aead_decrypt_setup( operation,
3825 &attributes,
3826 slot->key.data,
3827 slot->key.bytes,
3828 alg );
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003829 if( status != PSA_SUCCESS )
3830 goto exit;
3831
3832 operation->key_type = psa_get_key_type( &attributes );
3833
3834exit:
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003835 unlock_status = psa_unlock_key_slot( slot );
3836
3837 if( status == PSA_SUCCESS )
3838 {
3839 status = unlock_status;
3840 operation->alg = psa_aead_get_base_algorithm( alg );
Paul Elliottd9343f22021-08-23 18:59:49 +01003841 operation->is_encrypt = is_encrypt;
Paul Elliotte0a12bd2021-08-19 18:55:56 +01003842 }
3843 else
3844 psa_aead_abort( operation );
3845
3846 return( status );
Paul Elliottc2b71442021-06-24 18:17:52 +01003847}
3848
Paul Elliott302ff6b2021-04-20 18:10:30 +01003849/* Set the key for a multipart authenticated encryption operation. */
3850psa_status_t psa_aead_encrypt_setup( psa_aead_operation_t *operation,
3851 mbedtls_svc_key_id_t key,
3852 psa_algorithm_t alg )
3853{
Paul Elliottd9343f22021-08-23 18:59:49 +01003854 return( psa_aead_setup( operation, 1, key, alg ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003855}
3856
3857/* Set the key for a multipart authenticated decryption operation. */
3858psa_status_t psa_aead_decrypt_setup( psa_aead_operation_t *operation,
3859 mbedtls_svc_key_id_t key,
3860 psa_algorithm_t alg )
3861{
Paul Elliottd9343f22021-08-23 18:59:49 +01003862 return( psa_aead_setup( operation, 0, key, alg ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003863}
3864
3865/* Generate a random nonce / IV for multipart AEAD operation */
3866psa_status_t psa_aead_generate_nonce( psa_aead_operation_t *operation,
3867 uint8_t *nonce,
3868 size_t nonce_size,
3869 size_t *nonce_length )
3870{
3871 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliottb91da712021-05-20 14:43:47 +01003872 size_t required_nonce_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003873
Paul Elliott8eb9daf2021-06-04 16:42:21 +01003874 *nonce_length = 0;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003875
Paul Elliottcee785c2021-05-20 14:29:20 +01003876 if( operation->id == 0 )
3877 {
3878 status = PSA_ERROR_BAD_STATE;
3879 goto exit;
3880 }
3881
Paul Elliottdaf5c892021-08-25 16:24:58 +01003882 if( operation->nonce_set || !operation->is_encrypt )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003883 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003884 status = PSA_ERROR_BAD_STATE;
3885 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003886 }
3887
Paul Elliottbbe90b52021-05-11 22:22:42 +01003888 required_nonce_size = PSA_AEAD_NONCE_LENGTH( operation->key_type,
Paul Elliott3242f6c2021-08-25 16:33:47 +01003889 operation->alg );
Paul Elliott39dc6b82021-05-11 19:16:09 +01003890 if( nonce_size < required_nonce_size )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003891 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003892 status = PSA_ERROR_BUFFER_TOO_SMALL;
3893 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003894 }
3895
3896 status = psa_generate_random( nonce, required_nonce_size );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003897 if( status != PSA_SUCCESS )
Paul Elliott39dc6b82021-05-11 19:16:09 +01003898 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003899
Paul Elliott95271f12021-06-23 16:50:45 +01003900 status = psa_aead_set_nonce( operation, nonce, required_nonce_size );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003901
Paul Elliott39dc6b82021-05-11 19:16:09 +01003902exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01003903 if( status == PSA_SUCCESS )
3904 *nonce_length = required_nonce_size;
3905 else
3906 psa_aead_abort( operation );
3907
3908 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003909}
3910
3911/* Set the nonce for a multipart authenticated encryption or decryption
3912 operation.*/
3913psa_status_t psa_aead_set_nonce( psa_aead_operation_t *operation,
3914 const uint8_t *nonce,
3915 size_t nonce_length )
3916{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003917 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3918
Paul Elliottcee785c2021-05-20 14:29:20 +01003919 if( operation->id == 0 )
3920 {
3921 status = PSA_ERROR_BAD_STATE;
3922 goto exit;
3923 }
3924
Paul Elliott3d7d52c2021-09-01 10:33:14 +01003925 if( operation->nonce_set )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003926 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003927 status = PSA_ERROR_BAD_STATE;
3928 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003929 }
3930
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003931 status = psa_aead_check_nonce_length( operation->alg, nonce_length );
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003932 if( status != PSA_SUCCESS )
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003933 {
Paul Elliottbb0f9e12021-09-28 11:14:27 +01003934 status = PSA_ERROR_INVALID_ARGUMENT;
3935 goto exit;
Paul Elliott4ed1ed12021-09-27 18:09:28 +01003936 }
Paul Elliott1a98aca2021-05-20 18:24:07 +01003937
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003938 status = psa_driver_wrapper_aead_set_nonce( operation, nonce,
3939 nonce_length );
3940
Paul Elliott39dc6b82021-05-11 19:16:09 +01003941exit:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003942 if( status == PSA_SUCCESS )
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003943 operation->nonce_set = 1;
Paul Elliott39dc6b82021-05-11 19:16:09 +01003944 else
3945 psa_aead_abort( operation );
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003946
3947 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01003948}
3949
3950/* Declare the lengths of the message and additional data for multipart AEAD. */
3951psa_status_t psa_aead_set_lengths( psa_aead_operation_t *operation,
3952 size_t ad_length,
3953 size_t plaintext_length )
3954{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01003955 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3956
Paul Elliottcee785c2021-05-20 14:29:20 +01003957 if( operation->id == 0 )
3958 {
3959 status = PSA_ERROR_BAD_STATE;
3960 goto exit;
3961 }
3962
Paul Elliott6eb95982021-05-21 17:41:41 +01003963 if( operation->lengths_set || operation->ad_started ||
Paul Elliott7f429b72021-06-24 18:08:54 +01003964 operation->body_started )
Paul Elliott302ff6b2021-04-20 18:10:30 +01003965 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01003966 status = PSA_ERROR_BAD_STATE;
3967 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01003968 }
3969
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003970 switch(operation->alg)
Paul Elliott325d3742021-09-27 17:56:28 +01003971 {
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003972#if defined(PSA_WANT_ALG_GCM)
3973 case PSA_ALG_GCM:
3974 /* Lengths can only be too large for GCM if size_t is bigger than 32
3975 * bits. Without the guard this code will generate warnings on 32bit
3976 * builds. */
Paul Elliott325d3742021-09-27 17:56:28 +01003977#if SIZE_MAX > UINT32_MAX
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003978 if( (( uint64_t ) ad_length ) >> 61 != 0 ||
3979 (( uint64_t ) plaintext_length ) > 0xFFFFFFFE0ull )
3980 {
3981 status = PSA_ERROR_INVALID_ARGUMENT;
3982 goto exit;
3983 }
Paul Elliott325d3742021-09-27 17:56:28 +01003984#endif
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003985 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003986#endif /* PSA_WANT_ALG_GCM */
3987#if defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003988 case PSA_ALG_CCM:
3989 if( ad_length > 0xFF00 )
3990 {
3991 status = PSA_ERROR_INVALID_ARGUMENT;
3992 goto exit;
3993 }
3994 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01003995#endif /* PSA_WANT_ALG_CCM */
3996#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02003997 case PSA_ALG_CHACHA20_POLY1305:
3998 /* No length restrictions for ChaChaPoly. */
3999 break;
Paul Elliotte716e6c2021-09-29 14:10:20 +01004000#endif /* PSA_WANT_ALG_CHACHA20_POLY1305 */
Przemyslaw Stekiel4cad4fc2021-10-13 11:12:08 +02004001 default:
4002 break;
4003 }
Paul Elliott325d3742021-09-27 17:56:28 +01004004
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004005 status = psa_driver_wrapper_aead_set_lengths( operation, ad_length,
4006 plaintext_length );
4007
Paul Elliott39dc6b82021-05-11 19:16:09 +01004008exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004009 if( status == PSA_SUCCESS )
Paul Elliottee4ffe02021-05-20 17:25:06 +01004010 {
4011 operation->ad_remaining = ad_length;
4012 operation->body_remaining = plaintext_length;
Paul Elliott39dc6b82021-05-11 19:16:09 +01004013 operation->lengths_set = 1;
Paul Elliottee4ffe02021-05-20 17:25:06 +01004014 }
Paul Elliott39dc6b82021-05-11 19:16:09 +01004015 else
4016 psa_aead_abort( operation );
4017
4018 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004019}
Paul Elliott3d7d52c2021-09-01 10:33:14 +01004020
4021/* Pass additional data to an active multipart AEAD operation. */
Paul Elliott302ff6b2021-04-20 18:10:30 +01004022psa_status_t psa_aead_update_ad( psa_aead_operation_t *operation,
4023 const uint8_t *input,
4024 size_t input_length )
4025{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004026 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4027
Paul Elliottcee785c2021-05-20 14:29:20 +01004028 if( operation->id == 0 )
4029 {
4030 status = PSA_ERROR_BAD_STATE;
4031 goto exit;
4032 }
4033
Paul Elliott6eb95982021-05-21 17:41:41 +01004034 if( !operation->nonce_set || operation->body_started )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004035 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004036 status = PSA_ERROR_BAD_STATE;
4037 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004038 }
4039
Paul Elliottee4ffe02021-05-20 17:25:06 +01004040 if( operation->lengths_set )
4041 {
Paul Elliott6eb95982021-05-21 17:41:41 +01004042 if( operation->ad_remaining < input_length )
Paul Elliottee4ffe02021-05-20 17:25:06 +01004043 {
4044 status = PSA_ERROR_INVALID_ARGUMENT;
4045 goto exit;
4046 }
4047
4048 operation->ad_remaining -= input_length;
4049 }
4050
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004051 status = psa_driver_wrapper_aead_update_ad( operation, input,
4052 input_length );
4053
Paul Elliott39dc6b82021-05-11 19:16:09 +01004054exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004055 if( status == PSA_SUCCESS )
4056 operation->ad_started = 1;
4057 else
4058 psa_aead_abort( operation );
4059
4060 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004061}
4062
4063/* Encrypt or decrypt a message fragment in an active multipart AEAD
4064 operation.*/
4065psa_status_t psa_aead_update( psa_aead_operation_t *operation,
4066 const uint8_t *input,
4067 size_t input_length,
4068 uint8_t *output,
4069 size_t output_size,
4070 size_t *output_length )
4071{
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004072 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004073
4074 *output_length = 0;
4075
Paul Elliottcee785c2021-05-20 14:29:20 +01004076 if( operation->id == 0 )
4077 {
4078 status = PSA_ERROR_BAD_STATE;
4079 goto exit;
4080 }
4081
Paul Elliott6eb95982021-05-21 17:41:41 +01004082 if( !operation->nonce_set )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004083 {
Paul Elliott39dc6b82021-05-11 19:16:09 +01004084 status = PSA_ERROR_BAD_STATE;
4085 goto exit;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004086 }
4087
Paul Elliottee4ffe02021-05-20 17:25:06 +01004088 if( operation->lengths_set )
4089 {
4090 /* Additional data length was supplied, but not all the additional
4091 data was supplied.*/
4092 if( operation->ad_remaining != 0 )
4093 {
4094 status = PSA_ERROR_INVALID_ARGUMENT;
4095 goto exit;
4096 }
4097
4098 /* Too much data provided. */
4099 if( operation->body_remaining < input_length )
4100 {
4101 status = PSA_ERROR_INVALID_ARGUMENT;
4102 goto exit;
4103 }
4104
4105 operation->body_remaining -= input_length;
4106 }
4107
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004108 status = psa_driver_wrapper_aead_update( operation, input, input_length,
4109 output, output_size,
4110 output_length );
4111
Paul Elliott39dc6b82021-05-11 19:16:09 +01004112exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004113 if( status == PSA_SUCCESS )
4114 operation->body_started = 1;
4115 else
4116 psa_aead_abort( operation );
4117
4118 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004119}
4120
Paul Elliott69bf5fc2021-09-19 18:26:37 +01004121static psa_status_t psa_aead_final_checks( const psa_aead_operation_t *operation )
Paul Elliottad53dcc2021-06-23 08:50:14 +01004122{
Paul Elliott7f429b72021-06-24 18:08:54 +01004123 if( operation->id == 0 || !operation->nonce_set )
Paul Elliottad53dcc2021-06-23 08:50:14 +01004124 return( PSA_ERROR_BAD_STATE );
4125
Paul Elliotta5614442021-07-14 14:54:11 +01004126 if( operation->lengths_set && ( operation->ad_remaining != 0 ||
Paul Elliottad53dcc2021-06-23 08:50:14 +01004127 operation->body_remaining != 0 ) )
4128 return( PSA_ERROR_INVALID_ARGUMENT );
4129
4130 return( PSA_SUCCESS );
4131}
4132
Paul Elliott302ff6b2021-04-20 18:10:30 +01004133/* Finish encrypting a message in a multipart AEAD operation. */
4134psa_status_t psa_aead_finish( psa_aead_operation_t *operation,
4135 uint8_t *ciphertext,
4136 size_t ciphertext_size,
4137 size_t *ciphertext_length,
4138 uint8_t *tag,
4139 size_t tag_size,
4140 size_t *tag_length )
4141{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004142 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4143
Paul Elliott302ff6b2021-04-20 18:10:30 +01004144 *ciphertext_length = 0;
Paul Elliottf88a5652021-06-22 17:53:45 +01004145 *tag_length = tag_size;
Paul Elliott302ff6b2021-04-20 18:10:30 +01004146
Paul Elliott814fffb2021-08-16 18:20:36 +01004147 status = psa_aead_final_checks( operation );
Paul Elliottad53dcc2021-06-23 08:50:14 +01004148 if( status != PSA_SUCCESS )
4149 goto exit;
4150
Paul Elliott7f429b72021-06-24 18:08:54 +01004151 if( !operation->is_encrypt )
Paul Elliottcee785c2021-05-20 14:29:20 +01004152 {
4153 status = PSA_ERROR_BAD_STATE;
4154 goto exit;
4155 }
4156
Paul Elliott39dc6b82021-05-11 19:16:09 +01004157 status = psa_driver_wrapper_aead_finish( operation, ciphertext,
4158 ciphertext_size,
4159 ciphertext_length,
4160 tag, tag_size, tag_length );
Paul Elliottcbbde5f2021-05-10 18:19:46 +01004161
Paul Elliott39dc6b82021-05-11 19:16:09 +01004162exit:
Paul Elliottf47b0952021-05-21 18:02:33 +01004163 /* In case the operation fails and the user fails to check for failure or
Paul Elliott4c916e82021-09-19 18:34:50 +01004164 * the zero tag size, make sure the tag is set to something implausible.
4165 * Even if the operation succeeds, make sure we clear the rest of the
4166 * buffer to prevent potential leakage of anything previously placed in
4167 * the same buffer.*/
Paul Elliott90fdc112021-09-22 17:15:48 +01004168 if( tag != NULL )
Paul Elliottec95cc92021-09-19 22:33:09 +01004169 {
4170 if( status != PSA_SUCCESS )
4171 memset( tag, '!', tag_size );
4172 else if( *tag_length < tag_size )
4173 memset( tag + *tag_length, '!', ( tag_size - *tag_length ) );
4174 }
Paul Elliottf47b0952021-05-21 18:02:33 +01004175
Paul Elliott39dc6b82021-05-11 19:16:09 +01004176 psa_aead_abort( operation );
4177
4178 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004179}
4180
4181/* Finish authenticating and decrypting a message in a multipart AEAD
4182 operation.*/
4183psa_status_t psa_aead_verify( psa_aead_operation_t *operation,
4184 uint8_t *plaintext,
4185 size_t plaintext_size,
4186 size_t *plaintext_length,
4187 const uint8_t *tag,
4188 size_t tag_length )
4189{
Paul Elliott39dc6b82021-05-11 19:16:09 +01004190 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4191
Paul Elliott302ff6b2021-04-20 18:10:30 +01004192 *plaintext_length = 0;
4193
Paul Elliott814fffb2021-08-16 18:20:36 +01004194 status = psa_aead_final_checks( operation );
Paul Elliottad53dcc2021-06-23 08:50:14 +01004195 if( status != PSA_SUCCESS )
4196 goto exit;
4197
Paul Elliott7f429b72021-06-24 18:08:54 +01004198 if( operation->is_encrypt )
Paul Elliottcee785c2021-05-20 14:29:20 +01004199 {
4200 status = PSA_ERROR_BAD_STATE;
4201 goto exit;
4202 }
4203
Paul Elliott39dc6b82021-05-11 19:16:09 +01004204 status = psa_driver_wrapper_aead_verify( operation, plaintext,
4205 plaintext_size,
4206 plaintext_length,
4207 tag, tag_length );
4208
4209exit:
Paul Elliott39dc6b82021-05-11 19:16:09 +01004210 psa_aead_abort( operation );
4211
4212 return( status );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004213}
4214
4215/* Abort an AEAD operation. */
Paul Elliottbbe90b52021-05-11 22:22:42 +01004216psa_status_t psa_aead_abort( psa_aead_operation_t *operation )
Paul Elliott302ff6b2021-04-20 18:10:30 +01004217{
4218 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4219
4220 if( operation->id == 0 )
4221 {
4222 /* The object has (apparently) been initialized but it is not (yet)
4223 * in use. It's ok to call abort on such an object, and there's
4224 * nothing to do. */
4225 return( PSA_SUCCESS );
4226 }
4227
4228 status = psa_driver_wrapper_aead_abort( operation );
4229
Paul Elliott70618b22021-09-22 17:12:16 +01004230 memset( operation, 0, sizeof( *operation ) );
Paul Elliott302ff6b2021-04-20 18:10:30 +01004231
4232 return( status );
4233}
4234
Gilles Peskinee59236f2018-01-27 23:32:46 +01004235/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004236/* Generators */
4237/****************************************************************/
4238
John Durkop07cc04a2020-11-16 22:08:34 -08004239#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4240 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4241 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4242#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman094a77e2021-05-06 17:58:36 +02004243#endif /* At least one builtin KDF */
4244
4245#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4246 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4247 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4248static psa_status_t psa_key_derivation_start_hmac(
4249 psa_mac_operation_t *operation,
4250 psa_algorithm_t hash_alg,
4251 const uint8_t *hmac_key,
4252 size_t hmac_key_length )
4253{
4254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4256 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
4257 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) );
4258 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
4259
Steven Cooreman72f736a2021-05-07 14:14:37 +02004260 operation->is_sign = 1;
4261 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
4262
Steven Cooreman094a77e2021-05-06 17:58:36 +02004263 status = psa_driver_wrapper_mac_sign_setup( operation,
4264 &attributes,
4265 hmac_key, hmac_key_length,
4266 PSA_ALG_HMAC( hash_alg ) );
4267
4268 psa_reset_key_attributes( &attributes );
4269 return( status );
4270}
4271#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08004272
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004273#define HKDF_STATE_INIT 0 /* no input yet */
4274#define HKDF_STATE_STARTED 1 /* got salt */
4275#define HKDF_STATE_KEYED 2 /* got key */
4276#define HKDF_STATE_OUTPUT 3 /* output started */
4277
Gilles Peskinecbe66502019-05-16 16:59:18 +02004278static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004279 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004280{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004281 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4282 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004283 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004284 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004285}
4286
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004287psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004288{
4289 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004290 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004291 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004292 {
4293 /* The object has (apparently) been initialized but it is not
4294 * in use. It's ok to call abort on such an object, and there's
4295 * nothing to do. */
4296 }
4297 else
John Durkopd0321952020-10-29 21:37:36 -07004298#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004299 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004300 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004301 mbedtls_free( operation->ctx.hkdf.info );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004302 status = psa_mac_abort( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004303 }
John Durkop07cc04a2020-11-16 22:08:34 -08004304 else
4305#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4306#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4307 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4308 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004309 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004310 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004311 {
Steven Cooremana6df6042021-04-29 19:32:25 +02004312 if( operation->ctx.tls12_prf.secret != NULL )
4313 {
4314 mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret,
4315 operation->ctx.tls12_prf.secret_length );
4316 mbedtls_free( operation->ctx.tls12_prf.secret );
4317 }
4318
Janos Follath6a1d2622019-06-11 10:37:28 +01004319 if( operation->ctx.tls12_prf.seed != NULL )
4320 {
4321 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4322 operation->ctx.tls12_prf.seed_length );
4323 mbedtls_free( operation->ctx.tls12_prf.seed );
4324 }
4325
4326 if( operation->ctx.tls12_prf.label != NULL )
4327 {
4328 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4329 operation->ctx.tls12_prf.label_length );
4330 mbedtls_free( operation->ctx.tls12_prf.label );
4331 }
4332
Steven Cooremana6df6042021-04-29 19:32:25 +02004333 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01004334
4335 /* We leave the fields Ai and output_block to be erased safely by the
4336 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004337 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004338 else
John Durkop07cc04a2020-11-16 22:08:34 -08004339#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4340 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004341 {
4342 status = PSA_ERROR_BAD_STATE;
4343 }
Janos Follath083036a2019-06-11 10:22:26 +01004344 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004345 return( status );
4346}
4347
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004348psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004349 size_t *capacity)
4350{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004351 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004352 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004353 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004354 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004355 }
4356
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004357 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004358 return( PSA_SUCCESS );
4359}
4360
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004361psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004362 size_t capacity )
4363{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004364 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004365 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004366 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004367 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004369 return( PSA_SUCCESS );
4370}
4371
John Durkopd0321952020-10-29 21:37:36 -07004372#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004374 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004375static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004376 psa_algorithm_t hash_alg,
4377 uint8_t *output,
4378 size_t output_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004379{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004380 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004381 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004382 psa_status_t status;
4383
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004384 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4385 return( PSA_ERROR_BAD_STATE );
4386 hkdf->state = HKDF_STATE_OUTPUT;
4387
Gilles Peskinebef7f142018-07-12 17:22:21 +02004388 while( output_length != 0 )
4389 {
4390 /* Copy what remains of the current block */
4391 uint8_t n = hash_length - hkdf->offset_in_block;
4392 if( n > output_length )
4393 n = (uint8_t) output_length;
4394 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4395 output += n;
4396 output_length -= n;
4397 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004398 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004399 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004400 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004401 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004402 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004403 * object was corrupted or if this function is called directly
4404 * inside the library. */
4405 if( hkdf->block_number == 0xff )
4406 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004407
4408 /* We need a new block */
4409 ++hkdf->block_number;
4410 hkdf->offset_in_block = 0;
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004411
Steven Cooreman094a77e2021-05-06 17:58:36 +02004412 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4413 hash_alg,
4414 hkdf->prk,
4415 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004416 if( status != PSA_SUCCESS )
4417 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004418
Gilles Peskinebef7f142018-07-12 17:22:21 +02004419 if( hkdf->block_number != 1 )
4420 {
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004421 status = psa_mac_update( &hkdf->hmac,
4422 hkdf->output_block,
4423 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004424 if( status != PSA_SUCCESS )
4425 return( status );
4426 }
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004427 status = psa_mac_update( &hkdf->hmac,
4428 hkdf->info,
4429 hkdf->info_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004430 if( status != PSA_SUCCESS )
4431 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004432 status = psa_mac_update( &hkdf->hmac,
4433 &hkdf->block_number, 1 );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004434 if( status != PSA_SUCCESS )
4435 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02004436 status = psa_mac_sign_finish( &hkdf->hmac,
4437 hkdf->output_block,
4438 sizeof( hkdf->output_block ),
4439 &hmac_output_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004440 if( status != PSA_SUCCESS )
4441 return( status );
4442 }
4443
4444 return( PSA_SUCCESS );
4445}
John Durkop07cc04a2020-11-16 22:08:34 -08004446#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004447
John Durkop07cc04a2020-11-16 22:08:34 -08004448#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4449 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004450static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4451 psa_tls12_prf_key_derivation_t *tls12_prf,
4452 psa_algorithm_t alg )
4453{
4454 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004455 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremana6df6042021-04-29 19:32:25 +02004456 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4457 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004458 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004459
Janos Follath7742fee2019-06-17 12:58:10 +01004460 /* We can't be wanting more output after block 0xff, otherwise
4461 * the capacity check in psa_key_derivation_output_bytes() would have
4462 * prevented this call. It could happen only if the operation
4463 * object was corrupted or if this function is called directly
4464 * inside the library. */
4465 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004466 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004467
4468 /* We need a new block */
4469 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004470 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004471
4472 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4473 *
4474 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4475 *
4476 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4477 * HMAC_hash(secret, A(2) + seed) +
4478 * HMAC_hash(secret, A(3) + seed) + ...
4479 *
4480 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004481 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004482 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004483 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004484 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004485 * is currently extracted as `output_block` and where i is
4486 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004487 */
4488
Steven Cooreman094a77e2021-05-06 17:58:36 +02004489 status = psa_key_derivation_start_hmac( &hmac,
4490 hash_alg,
4491 tls12_prf->secret,
4492 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004493 if( status != PSA_SUCCESS )
4494 goto cleanup;
4495
4496 /* Calculate A(i) where i = tls12_prf->block_number. */
4497 if( tls12_prf->block_number == 1 )
4498 {
4499 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4500 * the variable seed and in this instance means it in the context of the
4501 * P_hash function, where seed = label + seed.) */
Steven Cooremana6df6042021-04-29 19:32:25 +02004502 status = psa_mac_update( &hmac,
4503 tls12_prf->label,
4504 tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004505 if( status != PSA_SUCCESS )
4506 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004507 status = psa_mac_update( &hmac,
4508 tls12_prf->seed,
4509 tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004510 if( status != PSA_SUCCESS )
4511 goto cleanup;
4512 }
4513 else
4514 {
4515 /* A(i) = HMAC_hash(secret, A(i-1)) */
Steven Cooremana6df6042021-04-29 19:32:25 +02004516 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004517 if( status != PSA_SUCCESS )
4518 goto cleanup;
4519 }
4520
Steven Cooremana6df6042021-04-29 19:32:25 +02004521 status = psa_mac_sign_finish( &hmac,
4522 tls12_prf->Ai, hash_length,
4523 &hmac_output_length );
4524 if( hmac_output_length != hash_length )
4525 status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follathea29bfb2019-06-19 12:21:20 +01004526 if( status != PSA_SUCCESS )
4527 goto cleanup;
4528
4529 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Steven Cooreman094a77e2021-05-06 17:58:36 +02004530 status = psa_key_derivation_start_hmac( &hmac,
4531 hash_alg,
4532 tls12_prf->secret,
4533 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004534 if( status != PSA_SUCCESS )
4535 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004536 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004537 if( status != PSA_SUCCESS )
4538 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004539 status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004540 if( status != PSA_SUCCESS )
4541 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004542 status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004543 if( status != PSA_SUCCESS )
4544 goto cleanup;
Steven Cooremana6df6042021-04-29 19:32:25 +02004545 status = psa_mac_sign_finish( &hmac,
4546 tls12_prf->output_block, hash_length,
4547 &hmac_output_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004548 if( status != PSA_SUCCESS )
4549 goto cleanup;
4550
Janos Follath7742fee2019-06-17 12:58:10 +01004551
4552cleanup:
Steven Cooremana6df6042021-04-29 19:32:25 +02004553 cleanup_status = psa_mac_abort( &hmac );
Janos Follathea29bfb2019-06-19 12:21:20 +01004554 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4555 status = cleanup_status;
4556
4557 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004558}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004559
Janos Follath844eb0e2019-06-19 12:10:49 +01004560static psa_status_t psa_key_derivation_tls12_prf_read(
4561 psa_tls12_prf_key_derivation_t *tls12_prf,
4562 psa_algorithm_t alg,
4563 uint8_t *output,
4564 size_t output_length )
4565{
4566 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004567 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004568 psa_status_t status;
4569 uint8_t offset, length;
4570
Gilles Peskineb1edaec2021-06-11 22:41:46 +02004571 switch( tls12_prf->state )
4572 {
4573 case PSA_TLS12_PRF_STATE_LABEL_SET:
4574 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4575 break;
4576 case PSA_TLS12_PRF_STATE_OUTPUT:
4577 break;
4578 default:
4579 return( PSA_ERROR_BAD_STATE );
4580 }
4581
Janos Follath844eb0e2019-06-19 12:10:49 +01004582 while( output_length != 0 )
4583 {
4584 /* Check if we have fully processed the current block. */
4585 if( tls12_prf->left_in_block == 0 )
4586 {
4587 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4588 alg );
4589 if( status != PSA_SUCCESS )
4590 return( status );
4591
4592 continue;
4593 }
4594
4595 if( tls12_prf->left_in_block > output_length )
4596 length = (uint8_t) output_length;
4597 else
4598 length = tls12_prf->left_in_block;
4599
4600 offset = hash_length - tls12_prf->left_in_block;
4601 memcpy( output, tls12_prf->output_block + offset, length );
4602 output += length;
4603 output_length -= length;
4604 tls12_prf->left_in_block -= length;
4605 }
4606
4607 return( PSA_SUCCESS );
4608}
John Durkop07cc04a2020-11-16 22:08:34 -08004609#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4610 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004611
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004612psa_status_t psa_key_derivation_output_bytes(
4613 psa_key_derivation_operation_t *operation,
4614 uint8_t *output,
4615 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004616{
4617 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004618 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004619
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004620 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004621 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004622 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004623 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004624 }
4625
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004626 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004627 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004628 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004629 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004630 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004631 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004632 goto exit;
4633 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004634 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004635 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004636 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004637 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004638 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4639 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004640 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004641 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004642 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004643 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004644 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004645
John Durkopd0321952020-10-29 21:37:36 -07004646#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004647 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004648 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004649 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004650 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004651 output, output_length );
4652 }
Janos Follathadbec812019-06-14 11:05:39 +01004653 else
John Durkop07cc04a2020-11-16 22:08:34 -08004654#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4655#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4656 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004657 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004658 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004659 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004660 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Steven Cooremana6df6042021-04-29 19:32:25 +02004661 kdf_alg, output,
4662 output_length );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004663 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004664 else
John Durkop07cc04a2020-11-16 22:08:34 -08004665#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4666 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004667 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004668 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004669 return( PSA_ERROR_BAD_STATE );
4670 }
4671
4672exit:
4673 if( status != PSA_SUCCESS )
4674 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004675 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004676 * This allows us to differentiate between exhausted operations and
4677 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4678 * operations. */
4679 psa_algorithm_t alg = operation->alg;
4680 psa_key_derivation_abort( operation );
4681 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004682 memset( output, '!', output_length );
4683 }
4684 return( status );
4685}
4686
David Brown7807bf72021-02-09 16:28:23 -07004687#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004688static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4689{
4690 if( data_size >= 8 )
4691 mbedtls_des_key_set_parity( data );
4692 if( data_size >= 16 )
4693 mbedtls_des_key_set_parity( data + 8 );
4694 if( data_size >= 24 )
4695 mbedtls_des_key_set_parity( data + 16 );
4696}
David Brown7807bf72021-02-09 16:28:23 -07004697#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004698
Przemyslaw Stekiel12b24712021-11-15 12:38:53 +01004699/*
Przemyslaw Stekiel6aee9642021-11-24 08:47:29 +01004700* ECC keys on a Weierstrass elliptic curve require the generation
4701* of a private key which is an integer
Przemyslaw Stekiel12b24712021-11-15 12:38:53 +01004702* in the range [1, N - 1], where N is the boundary of the private key domain:
4703* N is the prime p for Diffie-Hellman, or the order of the
4704* curve’s base point for ECC.
4705*
4706* Let m be the bit size of N, such that 2^m > N >= 2^(m-1).
4707* This function generates the private key using the following process:
4708*
4709* 1. Draw a byte string of length ceiling(m/8) bytes.
4710* 2. If m is not a multiple of 8, set the most significant
4711* (8 * ceiling(m/8) - m) bits of the first byte in the string to zero.
4712* 3. Convert the string to integer k by decoding it as a big-endian byte string.
4713* 4. If k > N - 2, discard the result and return to step 1.
4714* 5. Output k + 1 as the private key.
4715*
4716* This method allows compliance to NIST standards, specifically the methods titled
4717* Key-Pair Generation by Testing Candidates in the following publications:
4718* - NIST Special Publication 800-56A: Recommendation for Pair-Wise Key-Establishment
4719* Schemes Using Discrete Logarithm Cryptography [SP800-56A] §5.6.1.1.4 for
4720* Diffie-Hellman keys.
4721*
4722* - [SP800-56A] §5.6.1.2.2 or FIPS Publication 186-4: Digital Signature
4723* Standard (DSS) [FIPS186-4] §B.4.2 for elliptic curve keys.
4724*/
Przemyslaw Stekiel14e19e22021-11-18 13:35:00 +01004725#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4726 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4727 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
4728 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Przemyslaw Stekiel8d322932021-11-25 13:53:28 +01004729static psa_status_t psa_generate_derived_ecc_key_weierstrass_helper(
Przemyslaw Stekiel12b24712021-11-15 12:38:53 +01004730 psa_key_slot_t *slot,
4731 size_t bits,
4732 psa_key_derivation_operation_t *operation,
4733 uint8_t **data,
4734 unsigned *error)
4735{
4736 mbedtls_mpi N;
4737 mbedtls_mpi k;
4738 mbedtls_mpi diff_N_2;
4739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4740 psa_status_t status;
4741
4742 mbedtls_mpi_init( &k );
4743 mbedtls_mpi_init( &N );
4744 mbedtls_mpi_init( &diff_N_2 );
Przemyslaw Stekiel14e19e22021-11-18 13:35:00 +01004745
Przemyslaw Stekiel12b24712021-11-15 12:38:53 +01004746 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY(
4747 slot->attr.type );
4748 mbedtls_ecp_group_id grp_id =
4749 mbedtls_ecc_group_of_psa( curve, bits, 0 );
4750
4751 mbedtls_ecp_group ecp_group;
Przemyslaw Stekieldc6b3bf2021-11-18 11:57:07 +01004752 mbedtls_ecp_group_init( &ecp_group );
Przemyslaw Stekiel12b24712021-11-15 12:38:53 +01004753
4754 if( ( status = mbedtls_ecp_group_load( &ecp_group, grp_id ) ) != 0 )
4755 {
4756 ret = status;
4757 goto cleanup;
4758 }
4759
4760 /* N is the boundary of the private key domain. */
Przemyslaw Stekieldc6b3bf2021-11-18 11:57:07 +01004761 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &N, &ecp_group.N ) );
Przemyslaw Stekiel12b24712021-11-15 12:38:53 +01004762 /* Let m be the bit size of N. */
4763 size_t m = ecp_group.nbits;
4764
4765 size_t m_bytes = PSA_BITS_TO_BYTES( m );
Przemyslaw Stekieldc6b3bf2021-11-18 11:57:07 +01004766 if (*data == NULL)
Przemyslaw Stekiel12b24712021-11-15 12:38:53 +01004767 *data = mbedtls_calloc( 1, m_bytes );
4768 if( *data == NULL )
4769 {
4770 ret = PSA_ERROR_INSUFFICIENT_MEMORY;
4771 goto cleanup;
4772 }
4773 /* 1. Draw a byte string of length ceiling(m/8) bytes. */
4774 if ( ( status = psa_key_derivation_output_bytes( operation, *data, m_bytes ) ) != 0 )
4775 {
4776 ret = status;
4777 goto cleanup;
4778 }
4779
4780 /* 2. If m is not a multiple of 8 */
4781 if (m % 8)
4782 {
4783 /* Set the most significant
4784 * (8 * ceiling(m/8) - m) bits of the first byte in
4785 * the string to zero.
4786 */
Przemyslaw Stekiel204a55d2021-11-19 12:08:38 +01004787 uint8_t clear_bit_count = (uint8_t) ( 8 * m_bytes - m );
Przemyslaw Stekiel12b24712021-11-15 12:38:53 +01004788 uint8_t clear_bit_mask = ( ( 1 << clear_bit_count ) - 1 );
4789 clear_bit_mask = ~( clear_bit_mask << ( 8 - clear_bit_count ) );
4790 *data[0] = ( *data[0] & clear_bit_mask );
4791 }
4792
4793 /* 3. Convert the string to integer k by decoding it as a
4794 * big-endian byte string.
4795 */
4796 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary( &k, *data, m_bytes));
4797
4798 /* 4. If k > N - 2, discard the result and return to step 1.
4799 * Result of comparison is returned. When it indicates error
4800 * then this fuction is called again.
4801 */
4802 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &diff_N_2, &N, 2) );
4803 MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &k, diff_N_2.n ) );
4804 MBEDTLS_MPI_CHK( mbedtls_mpi_lt_mpi_ct( &diff_N_2, &k, error ) );
4805
4806 /* 5. Output k + 1 as the private key. */
4807 MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &k, &k, 1));
4808 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &k, *data, m_bytes) );
4809
4810 ret = 0;
4811cleanup:
4812 if (ret) {
4813 mbedtls_free( *data );
4814 *data = NULL;
4815 }
4816 mbedtls_mpi_free( &k );
4817 mbedtls_mpi_free( &N );
4818 mbedtls_mpi_free( &diff_N_2 );
4819 return( ret );
4820}
Przemyslaw Stekiel14e19e22021-11-18 13:35:00 +01004821#endif
Przemyslaw Stekiel12b24712021-11-15 12:38:53 +01004822
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004823static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004824 psa_key_slot_t *slot,
4825 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004827{
4828 uint8_t *data = NULL;
4829 size_t bytes = PSA_BITS_TO_BYTES( bits );
Archanad8a83dc2021-06-14 10:04:16 +05304830 size_t storage_size = bytes;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004831 psa_status_t status;
4832
Przemyslaw Stekiel14e19e22021-11-18 13:35:00 +01004833#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
4834 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
4835 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
4836 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Przemyslaw Stekiel8d322932021-11-25 13:53:28 +01004837 if ( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Przemyslaw Stekield5927592021-11-09 10:46:40 +01004838 {
Przemyslaw Stekiel8d322932021-11-25 13:53:28 +01004839 if ( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) != PSA_ECC_FAMILY_MONTGOMERY )
4840 {
4841 /* Weierstrass elliptic curve */
4842 unsigned key_err = 0;
Przemyslaw Stekiel12b24712021-11-15 12:38:53 +01004843gen_ecc_key:
Przemyslaw Stekiel8d322932021-11-25 13:53:28 +01004844 status = psa_generate_derived_ecc_key_weierstrass_helper(slot, bits, operation, &data, &key_err);
4845 if( status != PSA_SUCCESS )
4846 goto exit;
4847 /* Key has been created, but it doesn't meet criteria. */
4848 if (key_err)
4849 goto gen_ecc_key;
4850 } else
4851 {
4852 /* Montgomery elliptic curve */
4853 size_t output_length;
4854 switch( bits )
4855 {
4856 case 255:
4857 output_length = 32;
4858 break;
4859 case 448:
4860 output_length = 56;
4861 break;
4862 default:
4863 return( PSA_ERROR_INVALID_ARGUMENT );
4864 break;
4865 }
4866
4867 data = mbedtls_calloc( 1, bytes );
4868 if( data == NULL )
4869 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4870
4871 status = psa_key_derivation_output_bytes( operation, data, output_length );
4872
4873 if( status != PSA_SUCCESS )
4874 goto exit;
4875
4876 switch( bits )
4877 {
4878 case 255:
4879 data[0] &= 248;
4880 data[31] &= 127;
4881 data[31] |= 64;
4882 break;
4883 case 448:
4884 data[0] &= 252;
4885 data[55] |= 128;
4886 break;
4887 default:
4888 /* already handled */
4889 break;
4890 }
4891 }
Przemyslaw Stekiel14e19e22021-11-18 13:35:00 +01004892 } else
4893#endif
4894 {
Przemyslaw Stekield5927592021-11-09 10:46:40 +01004895 if( ! key_type_is_raw_bytes( slot->attr.type ) )
4896 return( PSA_ERROR_INVALID_ARGUMENT );
4897 if( bits % 8 != 0 )
4898 return( PSA_ERROR_INVALID_ARGUMENT );
4899 data = mbedtls_calloc( 1, bytes );
4900 if( data == NULL )
4901 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4902
4903 status = psa_key_derivation_output_bytes( operation, data, bytes );
4904 if( status != PSA_SUCCESS )
4905 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004906#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Przemyslaw Stekield5927592021-11-09 10:46:40 +01004907 if( slot->attr.type == PSA_KEY_TYPE_DES )
4908 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07004909#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Przemyslaw Stekield5927592021-11-09 10:46:40 +01004910 }
Ronald Crondd04d422020-11-28 15:14:42 +01004911
Ronald Cronbf33c932020-11-28 18:06:53 +01004912 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004913 psa_key_attributes_t attributes = {
4914 .core = slot->attr
4915 };
4916
Archanad8a83dc2021-06-14 10:04:16 +05304917 if( psa_key_lifetime_is_external( attributes.core.lifetime ) )
4918 {
Archana449608b2021-09-08 15:36:05 +05304919 status = psa_driver_wrapper_get_key_buffer_size( &attributes,
4920 &storage_size );
Archanad8a83dc2021-06-14 10:04:16 +05304921 if( status != PSA_SUCCESS )
4922 goto exit;
4923 }
4924 status = psa_allocate_buffer_to_slot( slot, storage_size );
4925 if( status != PSA_SUCCESS )
4926 goto exit;
4927
Ronald Cronbf33c932020-11-28 18:06:53 +01004928 status = psa_driver_wrapper_import_key( &attributes,
4929 data, bytes,
4930 slot->key.data,
4931 slot->key.bytes,
4932 &slot->key.bytes, &bits );
4933 if( bits != slot->attr.bits )
4934 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004935
4936exit:
4937 mbedtls_free( data );
4938 return( status );
4939}
4940
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004941psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004942 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004943 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004944{
4945 psa_status_t status;
4946 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004947 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004948
Ronald Cron81709fc2020-11-14 12:10:32 +01004949 *key = MBEDTLS_SVC_KEY_ID_INIT;
4950
Gilles Peskine0f84d622019-09-12 19:03:13 +02004951 /* Reject any attempt to create a zero-length key so that we don't
4952 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4953 if( psa_get_key_bits( attributes ) == 0 )
4954 return( PSA_ERROR_INVALID_ARGUMENT );
4955
Dave Rodgmand69da6c2021-11-16 10:32:48 +00004956 if( operation->alg == PSA_ALG_NONE )
4957 return( PSA_ERROR_BAD_STATE );
4958
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004959 if( ! operation->can_output_key )
4960 return( PSA_ERROR_NOT_PERMITTED );
4961
Ronald Cron81709fc2020-11-14 12:10:32 +01004962 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
4963 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004964#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4965 if( driver != NULL )
4966 {
4967 /* Deriving a key in a secure element is not implemented yet. */
4968 status = PSA_ERROR_NOT_SUPPORTED;
4969 }
4970#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004971 if( status == PSA_SUCCESS )
4972 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004973 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004974 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004976 }
4977 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01004978 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004979 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004980 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004981
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004982 return( status );
4983}
4984
Gilles Peskineeab56e42018-07-12 17:12:33 +02004985
4986
4987/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004988/* Key derivation */
4989/****************************************************************/
4990
Steven Cooreman932ffb72021-02-15 12:14:32 +01004991#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004992static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004994 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004995{
John Durkop07cc04a2020-11-16 22:08:34 -08004996 int is_kdf_alg_supported;
4997
Janos Follath5fe19732019-06-20 15:09:30 +01004998 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4999 * initialisers for this union leave some bytes unspecified.) */
5000 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5001
Gilles Peskine969c5d62019-01-16 15:53:06 +01005002 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005003#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005004 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5005 is_kdf_alg_supported = 1;
5006 else
5007#endif
5008#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5009 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5010 is_kdf_alg_supported = 1;
5011 else
5012#endif
5013#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5014 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5015 is_kdf_alg_supported = 1;
5016 else
5017#endif
5018 is_kdf_alg_supported = 0;
5019
5020 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005021 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005022 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005023 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005024 if( hash_size == 0 )
5025 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005026 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5027 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005028 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005029 {
5030 return( PSA_ERROR_NOT_SUPPORTED );
5031 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005032 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005033 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005034 }
John Durkop07cc04a2020-11-16 22:08:34 -08005035
5036 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005037}
John Durkop07cc04a2020-11-16 22:08:34 -08005038#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005039
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005040psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005041 psa_algorithm_t alg )
5042{
5043 psa_status_t status;
5044
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005046 return( PSA_ERROR_BAD_STATE );
5047
Gilles Peskine6843c292019-01-18 16:44:49 +01005048 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5049 return( PSA_ERROR_INVALID_ARGUMENT );
5050 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005051 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005052#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005053 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005054 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005055#else
5056 return( PSA_ERROR_NOT_SUPPORTED );
5057#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005058 }
5059 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5060 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005061#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005062 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005063#else
5064 return( PSA_ERROR_NOT_SUPPORTED );
5065#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005066 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005067 else
5068 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005069
5070 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005071 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005072 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005073}
5074
John Durkopd0321952020-10-29 21:37:36 -07005075#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005076static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005077 psa_algorithm_t hash_alg,
5078 psa_key_derivation_step_t step,
5079 const uint8_t *data,
5080 size_t data_length )
5081{
5082 psa_status_t status;
5083 switch( step )
5084 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005085 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005086 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005087 return( PSA_ERROR_BAD_STATE );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005088 else
5089 {
Steven Cooreman094a77e2021-05-06 17:58:36 +02005090 status = psa_key_derivation_start_hmac( &hkdf->hmac,
5091 hash_alg,
5092 data, data_length );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005093 if( status != PSA_SUCCESS )
5094 return( status );
5095 hkdf->state = HKDF_STATE_STARTED;
5096 return( PSA_SUCCESS );
5097 }
Gilles Peskine03410b52019-05-16 16:05:19 +02005098 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005099 /* If no salt was provided, use an empty salt. */
5100 if( hkdf->state == HKDF_STATE_INIT )
5101 {
Steven Cooreman094a77e2021-05-06 17:58:36 +02005102 status = psa_key_derivation_start_hmac( &hkdf->hmac,
5103 hash_alg,
5104 NULL, 0 );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005105 if( status != PSA_SUCCESS )
5106 return( status );
5107 hkdf->state = HKDF_STATE_STARTED;
5108 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005109 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005110 return( PSA_ERROR_BAD_STATE );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005111 status = psa_mac_update( &hkdf->hmac,
5112 data, data_length );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005113 if( status != PSA_SUCCESS )
5114 return( status );
Steven Cooremand1ed1d92021-04-29 19:11:25 +02005115 status = psa_mac_sign_finish( &hkdf->hmac,
5116 hkdf->prk,
5117 sizeof( hkdf->prk ),
5118 &data_length );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005119 if( status != PSA_SUCCESS )
5120 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005121 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005122 hkdf->block_number = 0;
5123 hkdf->state = HKDF_STATE_KEYED;
5124 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005125 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005126 if( hkdf->state == HKDF_STATE_OUTPUT )
5127 return( PSA_ERROR_BAD_STATE );
5128 if( hkdf->info_set )
5129 return( PSA_ERROR_BAD_STATE );
5130 hkdf->info_length = data_length;
5131 if( data_length != 0 )
5132 {
5133 hkdf->info = mbedtls_calloc( 1, data_length );
5134 if( hkdf->info == NULL )
5135 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5136 memcpy( hkdf->info, data, data_length );
5137 }
5138 hkdf->info_set = 1;
5139 return( PSA_SUCCESS );
5140 default:
5141 return( PSA_ERROR_INVALID_ARGUMENT );
5142 }
5143}
John Durkop07cc04a2020-11-16 22:08:34 -08005144#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005145
John Durkop07cc04a2020-11-16 22:08:34 -08005146#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5147 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005148static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5149 const uint8_t *data,
5150 size_t data_length )
5151{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005152 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005153 return( PSA_ERROR_BAD_STATE );
5154
Janos Follathd6dce9f2019-07-04 09:11:38 +01005155 if( data_length != 0 )
5156 {
5157 prf->seed = mbedtls_calloc( 1, data_length );
5158 if( prf->seed == NULL )
5159 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005160
Janos Follathd6dce9f2019-07-04 09:11:38 +01005161 memcpy( prf->seed, data, data_length );
5162 prf->seed_length = data_length;
5163 }
Janos Follathf08e2652019-06-13 09:05:41 +01005164
Gilles Peskine43f958b2020-12-13 14:55:14 +01005165 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005166
5167 return( PSA_SUCCESS );
5168}
5169
Janos Follath81550542019-06-13 14:26:34 +01005170static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
Janos Follath81550542019-06-13 14:26:34 +01005171 const uint8_t *data,
5172 size_t data_length )
5173{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005174 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005175 return( PSA_ERROR_BAD_STATE );
5176
Steven Cooremana6df6042021-04-29 19:32:25 +02005177 if( data_length != 0 )
5178 {
5179 prf->secret = mbedtls_calloc( 1, data_length );
5180 if( prf->secret == NULL )
5181 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5182
5183 memcpy( prf->secret, data, data_length );
5184 prf->secret_length = data_length;
5185 }
Janos Follath81550542019-06-13 14:26:34 +01005186
Gilles Peskine43f958b2020-12-13 14:55:14 +01005187 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005188
5189 return( PSA_SUCCESS );
5190}
5191
Janos Follath63028dd2019-06-13 09:15:47 +01005192static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5193 const uint8_t *data,
5194 size_t data_length )
5195{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005196 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005197 return( PSA_ERROR_BAD_STATE );
5198
Janos Follathd6dce9f2019-07-04 09:11:38 +01005199 if( data_length != 0 )
5200 {
5201 prf->label = mbedtls_calloc( 1, data_length );
5202 if( prf->label == NULL )
5203 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005204
Janos Follathd6dce9f2019-07-04 09:11:38 +01005205 memcpy( prf->label, data, data_length );
5206 prf->label_length = data_length;
5207 }
Janos Follath63028dd2019-06-13 09:15:47 +01005208
Gilles Peskine43f958b2020-12-13 14:55:14 +01005209 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005210
5211 return( PSA_SUCCESS );
5212}
5213
Janos Follathb03233e2019-06-11 15:30:30 +01005214static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
Janos Follathb03233e2019-06-11 15:30:30 +01005215 psa_key_derivation_step_t step,
5216 const uint8_t *data,
5217 size_t data_length )
5218{
Janos Follathb03233e2019-06-11 15:30:30 +01005219 switch( step )
5220 {
Janos Follathf08e2652019-06-13 09:05:41 +01005221 case PSA_KEY_DERIVATION_INPUT_SEED:
5222 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005223 case PSA_KEY_DERIVATION_INPUT_SECRET:
Steven Cooremana6df6042021-04-29 19:32:25 +02005224 return( psa_tls12_prf_set_key( prf, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005225 case PSA_KEY_DERIVATION_INPUT_LABEL:
5226 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005227 default:
5228 return( PSA_ERROR_INVALID_ARGUMENT );
5229 }
5230}
John Durkop07cc04a2020-11-16 22:08:34 -08005231#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5232 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5233
5234#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5235static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5236 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08005237 const uint8_t *data,
5238 size_t data_length )
5239{
5240 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005241 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005242 uint8_t *cur = pms;
5243
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005244 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005245 return( PSA_ERROR_INVALID_ARGUMENT );
5246
5247 /* Quoting RFC 4279, Section 2:
5248 *
5249 * The premaster secret is formed as follows: if the PSK is N octets
5250 * long, concatenate a uint16 with the value N, N zero octets, a second
5251 * uint16 with the value N, and the PSK itself.
5252 */
5253
Joe Subbiani5ecac212021-06-24 13:00:03 +01005254 *cur++ = MBEDTLS_BYTE_1( data_length );
5255 *cur++ = MBEDTLS_BYTE_0( data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08005256 memset( cur, 0, data_length );
5257 cur += data_length;
5258 *cur++ = pms[0];
5259 *cur++ = pms[1];
5260 memcpy( cur, data, data_length );
5261 cur += data_length;
5262
Steven Cooremana6df6042021-04-29 19:32:25 +02005263 status = psa_tls12_prf_set_key( prf, pms, cur - pms );
John Durkop07cc04a2020-11-16 22:08:34 -08005264
5265 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5266 return( status );
5267}
Janos Follath6660f0e2019-06-17 08:44:03 +01005268
5269static psa_status_t psa_tls12_prf_psk_to_ms_input(
5270 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005271 psa_key_derivation_step_t step,
5272 const uint8_t *data,
5273 size_t data_length )
5274{
5275 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005276 {
Steven Cooremana6df6042021-04-29 19:32:25 +02005277 return( psa_tls12_prf_psk_to_ms_set_key( prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005278 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005279 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005280
Steven Cooremana6df6042021-04-29 19:32:25 +02005281 return( psa_tls12_prf_input( prf, step, data, data_length ) );
Janos Follath6660f0e2019-06-17 08:44:03 +01005282}
John Durkop07cc04a2020-11-16 22:08:34 -08005283#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005284
Gilles Peskineb8965192019-09-24 16:21:10 +02005285/** Check whether the given key type is acceptable for the given
5286 * input step of a key derivation.
5287 *
5288 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5289 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5290 * Both secret and non-secret inputs can alternatively have the type
5291 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5292 * that the input was passed as a buffer rather than via a key object.
5293 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005294static int psa_key_derivation_check_input_type(
5295 psa_key_derivation_step_t step,
5296 psa_key_type_t key_type )
5297{
5298 switch( step )
5299 {
5300 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005301 if( key_type == PSA_KEY_TYPE_DERIVE )
5302 return( PSA_SUCCESS );
5303 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005304 return( PSA_SUCCESS );
5305 break;
5306 case PSA_KEY_DERIVATION_INPUT_LABEL:
5307 case PSA_KEY_DERIVATION_INPUT_SALT:
5308 case PSA_KEY_DERIVATION_INPUT_INFO:
5309 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005310 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5311 return( PSA_SUCCESS );
5312 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005313 return( PSA_SUCCESS );
5314 break;
5315 }
5316 return( PSA_ERROR_INVALID_ARGUMENT );
5317}
5318
Janos Follathb80a94e2019-06-12 15:54:46 +01005319static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005321 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005322 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005323 const uint8_t *data,
5324 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005325{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005326 psa_status_t status;
5327 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5328
5329 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005330 if( status != PSA_SUCCESS )
5331 goto exit;
5332
John Durkopd0321952020-10-29 21:37:36 -07005333#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005334 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005335 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005337 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005338 step, data, data_length );
5339 }
John Durkop07cc04a2020-11-16 22:08:34 -08005340 else
5341#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5342#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5343 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005344 {
Janos Follathb03233e2019-06-11 15:30:30 +01005345 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
Janos Follathb03233e2019-06-11 15:30:30 +01005346 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005347 }
John Durkop07cc04a2020-11-16 22:08:34 -08005348 else
5349#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5350#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5351 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005352 {
5353 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01005354 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005355 }
5356 else
John Durkop07cc04a2020-11-16 22:08:34 -08005357#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005358 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005359 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005360 (void) data;
5361 (void) data_length;
5362 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005363 return( PSA_ERROR_BAD_STATE );
5364 }
5365
Gilles Peskine224b0d62019-09-23 18:13:17 +02005366exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005367 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005369 return( status );
5370}
5371
Janos Follath51f4a0f2019-06-14 11:35:55 +01005372psa_status_t psa_key_derivation_input_bytes(
5373 psa_key_derivation_operation_t *operation,
5374 psa_key_derivation_step_t step,
5375 const uint8_t *data,
5376 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005377{
Gilles Peskineb8965192019-09-24 16:21:10 +02005378 return( psa_key_derivation_input_internal( operation, step,
5379 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005380 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005381}
5382
Janos Follath51f4a0f2019-06-14 11:35:55 +01005383psa_status_t psa_key_derivation_input_key(
5384 psa_key_derivation_operation_t *operation,
5385 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005386 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005387{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005388 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005389 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005390 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005391
Ronald Cron5c522922020-11-14 16:35:34 +01005392 status = psa_get_and_lock_transparent_key_slot_with_policy(
5393 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005394 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005395 {
5396 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005397 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005398 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005399
5400 /* Passing a key object as a SECRET input unlocks the permission
5401 * to output to a key object. */
5402 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5403 operation->can_output_key = 1;
5404
Ronald Cronf95a2b12020-10-22 15:24:49 +02005405 status = psa_key_derivation_input_internal( operation,
5406 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005407 slot->key.data,
5408 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005409
Ronald Cron5c522922020-11-14 16:35:34 +01005410 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005411
Ronald Cron5c522922020-11-14 16:35:34 +01005412 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005413}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005414
5415
5416
5417/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005418/* Key agreement */
5419/****************************************************************/
5420
John Durkop6ba40d12020-11-10 08:50:04 -08005421#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005422static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5423 size_t peer_key_length,
5424 const mbedtls_ecp_keypair *our_key,
5425 uint8_t *shared_secret,
5426 size_t shared_secret_size,
5427 size_t *shared_secret_length )
5428{
Steven Cooremand4867872020-08-05 16:31:39 +02005429 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005430 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005431 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005432 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005433 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005434 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005435
Ronald Cron90857082020-11-25 14:58:33 +01005436 status = mbedtls_psa_ecp_load_representation(
5437 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005438 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005439 peer_key,
5440 peer_key_length,
5441 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005442 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005443 goto exit;
5444
Jaeden Amero97271b32019-01-10 19:38:51 +00005445 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005446 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005447 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005448 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005449 status = mbedtls_to_psa_error(
5450 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5451 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005452 goto exit;
5453
Jaeden Amero97271b32019-01-10 19:38:51 +00005454 status = mbedtls_to_psa_error(
5455 mbedtls_ecdh_calc_secret( &ecdh,
5456 shared_secret_length,
5457 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005458 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005459 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005460 if( status != PSA_SUCCESS )
5461 goto exit;
5462 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5463 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005464
5465exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005466 if( status != PSA_SUCCESS )
5467 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005468 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005469 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005470 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005471
Jaeden Amero97271b32019-01-10 19:38:51 +00005472 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005473}
John Durkop6ba40d12020-11-10 08:50:04 -08005474#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005475
Gilles Peskine01d718c2018-09-18 12:01:02 +02005476#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5477
Gilles Peskine0216fe12019-04-11 21:23:21 +02005478static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5479 psa_key_slot_t *private_key,
5480 const uint8_t *peer_key,
5481 size_t peer_key_length,
5482 uint8_t *shared_secret,
5483 size_t shared_secret_size,
5484 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005485{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005486 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005487 {
John Durkop6ba40d12020-11-10 08:50:04 -08005488#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005489 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005490 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005491 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005492 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005493 psa_status_t status = mbedtls_psa_ecp_load_representation(
5494 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005495 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005496 private_key->key.data,
5497 private_key->key.bytes,
5498 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005499 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005500 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005501 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005502 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005503 shared_secret, shared_secret_size,
5504 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005505 mbedtls_ecp_keypair_free( ecp );
5506 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005507 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005508#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005509 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005510 (void) private_key;
5511 (void) peer_key;
5512 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005513 (void) shared_secret;
5514 (void) shared_secret_size;
5515 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005516 return( PSA_ERROR_NOT_SUPPORTED );
5517 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005518}
5519
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005520/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005521 * to potentially free embedded data structures and wipe confidential data.
5522 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005524 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005525 psa_key_slot_t *private_key,
5526 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005527 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005528{
5529 psa_status_t status;
5530 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5531 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005532 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005533
5534 /* Step 1: run the secret agreement algorithm to generate the shared
5535 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005536 status = psa_key_agreement_raw_internal( ka_alg,
5537 private_key,
5538 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005539 shared_secret,
5540 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005541 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005542 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005543 goto exit;
5544
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005545 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005546 * the shared secret. A shared secret is permitted wherever a key
5547 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005548 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005549 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005550 shared_secret,
5551 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005552exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005553 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005554 return( status );
5555}
5556
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005557psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005558 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005559 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005560 const uint8_t *peer_key,
5561 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005562{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005563 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005564 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005565 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005566
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005567 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005568 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005569 status = psa_get_and_lock_transparent_key_slot_with_policy(
5570 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005571 if( status != PSA_SUCCESS )
5572 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005573 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005574 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005575 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005576 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005577 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005578 else
5579 {
5580 /* If a private key has been added as SECRET, we allow the derived
5581 * key material to be used as a key in PSA Crypto. */
5582 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5583 operation->can_output_key = 1;
5584 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005585
Ronald Cron5c522922020-11-14 16:35:34 +01005586 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005587
Ronald Cron5c522922020-11-14 16:35:34 +01005588 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005589}
5590
Gilles Peskinebe697d82019-05-16 18:00:41 +02005591psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005592 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005593 const uint8_t *peer_key,
5594 size_t peer_key_length,
5595 uint8_t *output,
5596 size_t output_size,
5597 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005598{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005599 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005600 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005601 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005602
5603 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5604 {
5605 status = PSA_ERROR_INVALID_ARGUMENT;
5606 goto exit;
5607 }
Ronald Cron5c522922020-11-14 16:35:34 +01005608 status = psa_get_and_lock_transparent_key_slot_with_policy(
5609 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005610 if( status != PSA_SUCCESS )
5611 goto exit;
5612
5613 status = psa_key_agreement_raw_internal( alg, slot,
5614 peer_key, peer_key_length,
5615 output, output_size,
5616 output_length );
5617
5618exit:
5619 if( status != PSA_SUCCESS )
5620 {
5621 /* If an error happens and is not handled properly, the output
5622 * may be used as a key to protect sensitive data. Arrange for such
5623 * a key to be random, which is likely to result in decryption or
5624 * verification errors. This is better than filling the buffer with
5625 * some constant data such as zeros, which would result in the data
5626 * being protected with a reproducible, easily knowable key.
5627 */
5628 psa_generate_random( output, output_size );
5629 *output_length = output_size;
5630 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005631
Ronald Cron5c522922020-11-14 16:35:34 +01005632 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005633
Ronald Cron5c522922020-11-14 16:35:34 +01005634 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005635}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005636
5637
Gilles Peskine30524eb2020-11-13 17:02:26 +01005638
Gilles Peskine86a440b2018-11-12 18:39:40 +01005639/****************************************************************/
5640/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005641/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005642
Gilles Peskine30524eb2020-11-13 17:02:26 +01005643/** Initialize the PSA random generator.
5644 */
5645static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5646{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005647#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5648 memset( rng, 0, sizeof( *rng ) );
5649#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5650
Gilles Peskine30524eb2020-11-13 17:02:26 +01005651 /* Set default configuration if
5652 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5653 if( rng->entropy_init == NULL )
5654 rng->entropy_init = mbedtls_entropy_init;
5655 if( rng->entropy_free == NULL )
5656 rng->entropy_free = mbedtls_entropy_free;
5657
5658 rng->entropy_init( &rng->entropy );
5659#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5660 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5661 /* The PSA entropy injection feature depends on using NV seed as an entropy
5662 * source. Add NV seed as an entropy source for PSA entropy injection. */
5663 mbedtls_entropy_add_source( &rng->entropy,
5664 mbedtls_nv_seed_poll, NULL,
5665 MBEDTLS_ENTROPY_BLOCK_SIZE,
5666 MBEDTLS_ENTROPY_SOURCE_STRONG );
5667#endif
5668
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005669 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005670#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005671}
5672
5673/** Deinitialize the PSA random generator.
5674 */
5675static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5676{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005677#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5678 memset( rng, 0, sizeof( *rng ) );
5679#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005680 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005681 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005682#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005683}
5684
5685/** Seed the PSA random generator.
5686 */
5687static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5688{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005689#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5690 /* Do nothing: the external RNG seeds itself. */
5691 (void) rng;
5692 return( PSA_SUCCESS );
5693#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005694 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005695 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5696 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005697 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005698#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005699}
5700
Gilles Peskinee59236f2018-01-27 23:32:46 +01005701psa_status_t psa_generate_random( uint8_t *output,
5702 size_t output_size )
5703{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005704 GUARD_MODULE_INITIALIZED;
5705
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005706#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5707
5708 size_t output_length = 0;
5709 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5710 output, output_size,
5711 &output_length );
5712 if( status != PSA_SUCCESS )
5713 return( status );
5714 /* Breaking up a request into smaller chunks is currently not supported
5715 * for the extrernal RNG interface. */
5716 if( output_length != output_size )
5717 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5718 return( PSA_SUCCESS );
5719
5720#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5721
Gilles Peskine71ddab92021-01-04 21:01:07 +01005722 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005723 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005724 size_t request_size =
5725 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5726 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5727 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005728 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
5729 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02005730 if( ret != 0 )
5731 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01005732 output_size -= request_size;
5733 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005734 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005735 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005736#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005737}
5738
Gilles Peskine8814fc42020-12-14 15:33:44 +01005739/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005740 *
5741 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5742 * `psa_generate_random(...)`. The state parameter is ignored since the
5743 * PSA API doesn't support passing an explicit state.
5744 *
5745 * In the non-external case, psa_generate_random() calls an
5746 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5747 * and semantics as mbedtls_psa_get_random(). As an optimization,
5748 * instead of doing this back-and-forth between the PSA API and the
5749 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5750 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005751 */
5752#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5753int mbedtls_psa_get_random( void *p_rng,
5754 unsigned char *output,
5755 size_t output_size )
5756{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005757 /* This function takes a pointer to the RNG state because that's what
5758 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5759 * its own state internally and doesn't let the caller access that state.
5760 * So we just ignore the state parameter, and in practice we'll pass
5761 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005762 (void) p_rng;
5763 psa_status_t status = psa_generate_random( output, output_size );
5764 if( status == PSA_SUCCESS )
5765 return( 0 );
5766 else
5767 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
5768}
5769#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5770
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005771#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
Chris Jonesea0a8652021-03-09 19:11:19 +00005772#include "entropy_poll.h"
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005773
Gilles Peskine7228da22019-07-15 11:06:15 +02005774psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005775 size_t seed_size )
5776{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005777 if( global_data.initialized )
5778 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005779
5780 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5781 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5782 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5783 return( PSA_ERROR_INVALID_ARGUMENT );
5784
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005785 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005786}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005787#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005788
Ronald Cron3772afe2021-02-08 16:10:05 +01005789/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005790 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005791 * \param type The key type
5792 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005793 *
5794 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005795 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005796 * \retval #PSA_ERROR_INVALID_ARGUMENT
5797 * The size in bits of the key is not valid.
5798 * \retval #PSA_ERROR_NOT_SUPPORTED
5799 * The type and/or the size in bits of the key or the combination of
5800 * the two is not supported.
5801 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005802static psa_status_t psa_validate_key_type_and_size_for_key_generation(
5803 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005804{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005805 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005806
Gilles Peskinee59236f2018-01-27 23:32:46 +01005807 if( key_type_is_raw_bytes( type ) )
5808 {
Archana4d7ae1d2021-07-07 02:50:22 +05305809 status = psa_validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005810 if( status != PSA_SUCCESS )
5811 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005812 }
5813 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005814#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005815 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5816 {
5817 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5818 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005819
Ronald Cron01b2aba2020-10-05 09:42:02 +02005820 /* Accept only byte-aligned keys, for the same reasons as
5821 * in psa_import_rsa_key(). */
5822 if( bits % 8 != 0 )
5823 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005824 }
5825 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005826#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005827
Ronald Cron5c4d3862020-12-07 11:07:24 +01005828#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005829 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5830 {
Ronald Crond81ab562021-02-16 09:01:16 +01005831 /* To avoid empty block, return successfully here. */
5832 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005833 }
5834 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005835#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005836 {
5837 return( PSA_ERROR_NOT_SUPPORTED );
5838 }
5839
5840 return( PSA_SUCCESS );
5841}
5842
Ronald Cron55ed0592020-10-05 10:30:40 +02005843psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005844 const psa_key_attributes_t *attributes,
5845 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005846{
5847 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005848 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005849
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005850 if( ( attributes->domain_parameters == NULL ) &&
5851 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005852 return( PSA_ERROR_INVALID_ARGUMENT );
5853
Ronald Cron01b2aba2020-10-05 09:42:02 +02005854 if( key_type_is_raw_bytes( type ) )
5855 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005856 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005857 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005858 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005859
David Brown12ca5032021-02-11 11:02:00 -07005860#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005861 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005862 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07005863#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005864 }
5865 else
5866
Jaeden Amero424fa932021-05-14 08:34:32 +01005867#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5868 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005869 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005870 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01005871 return( mbedtls_psa_rsa_generate_key( attributes,
5872 key_buffer,
5873 key_buffer_size,
5874 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005875 }
5876 else
Jaeden Amero424fa932021-05-14 08:34:32 +01005877#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5878 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005879
John Durkop9814fa22020-11-04 12:28:15 -08005880#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005881 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005882 {
Ronald Cron7023db52020-11-20 18:17:42 +01005883 return( mbedtls_psa_ecp_generate_key( attributes,
5884 key_buffer,
5885 key_buffer_size,
5886 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005887 }
5888 else
John Durkop9814fa22020-11-04 12:28:15 -08005889#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005890 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005891 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005892 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005893 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005894
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005895 return( PSA_SUCCESS );
5896}
Darryl Green0c6575a2018-11-07 16:05:30 +00005897
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005898psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005899 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005900{
5901 psa_status_t status;
5902 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005903 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005904 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005905
Ronald Cron81709fc2020-11-14 12:10:32 +01005906 *key = MBEDTLS_SVC_KEY_ID_INIT;
5907
Gilles Peskine0f84d622019-09-12 19:03:13 +02005908 /* Reject any attempt to create a zero-length key so that we don't
5909 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5910 if( psa_get_key_bits( attributes ) == 0 )
5911 return( PSA_ERROR_INVALID_ARGUMENT );
5912
Przemyslaw Stekielc0fe8202021-10-07 11:08:56 +02005913 /* Reject any attempt to create a public key. */
5914 if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) )
5915 return( PSA_ERROR_INVALID_ARGUMENT );
5916
Ronald Cron81709fc2020-11-14 12:10:32 +01005917 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
5918 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005919 if( status != PSA_SUCCESS )
5920 goto exit;
5921
Ronald Cron977c2472020-10-13 08:32:21 +02005922 /* In the case of a transparent key or an opaque key stored in local
Archana449608b2021-09-08 15:36:05 +05305923 * storage ( thus not in the case of generating a key in a secure element
5924 * with storage ( MBEDTLS_PSA_CRYPTO_SE_C ) ),we have to allocate a
5925 * buffer to hold the generated key material. */
Ronald Cron977c2472020-10-13 08:32:21 +02005926 if( slot->key.data == NULL )
5927 {
5928 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
5929 PSA_KEY_LOCATION_LOCAL_STORAGE )
5930 {
Ronald Cron3772afe2021-02-08 16:10:05 +01005931 status = psa_validate_key_type_and_size_for_key_generation(
5932 attributes->core.type, attributes->core.bits );
5933 if( status != PSA_SUCCESS )
5934 goto exit;
5935
5936 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
5937 attributes->core.type,
5938 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02005939 }
5940 else
5941 {
5942 status = psa_driver_wrapper_get_key_buffer_size(
5943 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01005944 if( status != PSA_SUCCESS )
5945 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02005946 }
Ronald Cron977c2472020-10-13 08:32:21 +02005947
5948 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
5949 if( status != PSA_SUCCESS )
5950 goto exit;
5951 }
5952
Steven Cooreman2a1664c2020-07-20 15:33:08 +02005953 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02005954 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02005955
Ronald Cron2b56bc82020-10-05 10:02:26 +02005956 if( status != PSA_SUCCESS )
5957 psa_remove_key_data_from_memory( slot );
5958
Gilles Peskine11792082019-08-06 18:36:36 +02005959exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005960 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005961 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005962 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005963 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005964
Darryl Green0c6575a2018-11-07 16:05:30 +00005965 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005966}
5967
Gilles Peskinee59236f2018-01-27 23:32:46 +01005968/****************************************************************/
5969/* Module setup */
5970/****************************************************************/
5971
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005972#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005973psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5974 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5975 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5976{
5977 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5978 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005979 global_data.rng.entropy_init = entropy_init;
5980 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01005981 return( PSA_SUCCESS );
5982}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005983#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005984
Gilles Peskinee59236f2018-01-27 23:32:46 +01005985void mbedtls_psa_crypto_free( void )
5986{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005987 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005988 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5989 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005990 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005991 }
5992 /* Wipe all remaining data, including configuration.
5993 * In particular, this sets all state indicator to the value
5994 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005995 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005996#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005997 /* Unregister all secure element drivers, so that we restart from
5998 * a pristine state. */
5999 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006000#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006001}
6002
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006003#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6004/** Recover a transaction that was interrupted by a power failure.
6005 *
6006 * This function is called during initialization, before psa_crypto_init()
6007 * returns. If this function returns a failure status, the initialization
6008 * fails.
6009 */
6010static psa_status_t psa_crypto_recover_transaction(
6011 const psa_crypto_transaction_t *transaction )
6012{
6013 switch( transaction->unknown.type )
6014 {
6015 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6016 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006017 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006018 * is implemented.
6019 * https://github.com/ARMmbed/mbed-crypto/issues/218
6020 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006021 default:
6022 /* We found an unsupported transaction in the storage.
6023 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006024 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006025 }
6026}
6027#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6028
Gilles Peskinee59236f2018-01-27 23:32:46 +01006029psa_status_t psa_crypto_init( void )
6030{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006031 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006032
Gilles Peskinec6b69072018-11-20 21:42:52 +01006033 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006034 if( global_data.initialized != 0 )
6035 return( PSA_SUCCESS );
6036
Gilles Peskine30524eb2020-11-13 17:02:26 +01006037 /* Initialize and seed the random generator. */
6038 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006039 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006040 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006041 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006042 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006043 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006044
Gilles Peskine66fb1262018-12-10 16:29:04 +01006045 status = psa_initialize_key_slots( );
6046 if( status != PSA_SUCCESS )
6047 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006048
Gilles Peskined9348f22019-10-01 15:22:29 +02006049#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6050 status = psa_init_all_se_drivers( );
6051 if( status != PSA_SUCCESS )
6052 goto exit;
6053#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6054
Gilles Peskine4b734222019-07-24 15:56:31 +02006055#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006056 status = psa_crypto_load_transaction( );
6057 if( status == PSA_SUCCESS )
6058 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006059 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6060 if( status != PSA_SUCCESS )
6061 goto exit;
6062 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006063 }
6064 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6065 {
6066 /* There's no transaction to complete. It's all good. */
6067 status = PSA_SUCCESS;
6068 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006069#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006070
Gilles Peskinec6b69072018-11-20 21:42:52 +01006071 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006072 global_data.initialized = 1;
6073
6074exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006075 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006076 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006077 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006078}
6079
6080#endif /* MBEDTLS_PSA_CRYPTO_C */