blob: c1c351b5a79054dcd948b8f588ff00e9da0ec4ae [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"
30
Ronald Crond6d28882020-12-14 14:56:02 +010031#include "psa_crypto_cipher.h"
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035#include "psa_crypto_ecp.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010036#include "psa_crypto_hash.h"
Steven Cooreman32d56942021-03-19 17:39:17 +010037#include "psa_crypto_mac.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010038#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010039#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020040#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020041#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020042#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010043#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010044/* Include internal declarations that are useful for implementing persistently
45 * stored keys. */
46#include "psa_crypto_storage.h"
47
Gilles Peskineb2b64d32020-12-14 16:43:58 +010048#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010049
Gilles Peskineb46bef22019-07-30 21:32:04 +020050#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include <stdlib.h>
52#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020054#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010055#define mbedtls_calloc calloc
56#define mbedtls_free free
57#endif
58
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010059#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020061#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000062#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020063#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/blowfish.h"
65#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020066#include "mbedtls/chacha20.h"
67#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/cipher.h"
69#include "mbedtls/ccm.h"
70#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020072#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010074#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/error.h"
76#include "mbedtls/gcm.h"
77#include "mbedtls/md2.h"
78#include "mbedtls/md4.h"
79#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010080#include "mbedtls/md.h"
81#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010083#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010084#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000085#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010087#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/sha1.h"
89#include "mbedtls/sha256.h"
90#include "mbedtls/sha512.h"
91#include "mbedtls/xtea.h"
92
Gilles Peskine996deb12018-08-01 15:45:45 +020093#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
94
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010095/****************************************************************/
96/* Global data, support functions and library management */
97/****************************************************************/
98
Gilles Peskine48c0ea12018-06-21 14:15:31 +020099static int key_type_is_raw_bytes( psa_key_type_t type )
100{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200101 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200102}
103
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100104/* Values for psa_global_data_t::rng_state */
105#define RNG_NOT_INITIALIZED 0
106#define RNG_INITIALIZED 1
107#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100108
Gilles Peskine2d277862018-06-18 15:41:12 +0200109typedef struct
110{
Gilles Peskinec6b69072018-11-20 21:42:52 +0100111 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100112 unsigned rng_state : 2;
Gilles Peskineb8006a62021-11-08 22:20:03 +0100113 mbedtls_psa_random_context_t rng;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100114} psa_global_data_t;
115
116static psa_global_data_t global_data;
117
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100118#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
119mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
120 &global_data.rng.drbg;
121#endif
122
itayzafrir0adf0fc2018-09-06 16:24:41 +0300123#define GUARD_MODULE_INITIALIZED \
124 if( global_data.initialized == 0 ) \
125 return( PSA_ERROR_BAD_STATE );
126
Steven Cooreman01164162020-07-20 15:31:37 +0200127psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100128{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100129 /* Mbed TLS error codes can combine a high-level error code and a
130 * low-level error code. The low-level error usually reflects the
131 * root cause better, so dispatch on that preferably. */
132 int low_level_ret = - ( -ret & 0x007f );
133 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100134 {
135 case 0:
136 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100137
138 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
139 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
140 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
141 return( PSA_ERROR_NOT_SUPPORTED );
142 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200145 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
149 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
150 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
151 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
152 case MBEDTLS_ERR_ASN1_INVALID_DATA:
153 return( PSA_ERROR_INVALID_ARGUMENT );
154 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
155 return( PSA_ERROR_INSUFFICIENT_MEMORY );
156 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
157 return( PSA_ERROR_BUFFER_TOO_SMALL );
158
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000160 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
162 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
163#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
165 return( PSA_ERROR_NOT_SUPPORTED );
166 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
167 return( PSA_ERROR_HARDWARE_FAILURE );
168
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000170 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
172 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
173#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CCM_BAD_INPUT:
180 return( PSA_ERROR_INVALID_ARGUMENT );
181 case MBEDTLS_ERR_CCM_AUTH_FAILED:
182 return( PSA_ERROR_INVALID_SIGNATURE );
183 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188
189 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
190 return( PSA_ERROR_BAD_STATE );
191 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
192 return( PSA_ERROR_INVALID_SIGNATURE );
193
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
201 return( PSA_ERROR_INVALID_PADDING );
202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200203 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200207 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
Gilles Peskine82e57d12020-11-13 21:31:17 +0100214#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
215 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100216 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
217 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100218 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
221 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
224 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100226
227 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
228 return( PSA_ERROR_NOT_SUPPORTED );
229 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
230 return( PSA_ERROR_HARDWARE_FAILURE );
231
Gilles Peskinee59236f2018-01-27 23:32:46 +0100232 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
233 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
234 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
235 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
237 case MBEDTLS_ERR_GCM_AUTH_FAILED:
238 return( PSA_ERROR_INVALID_SIGNATURE );
239 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200240 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100241 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskine82e57d12020-11-13 21:31:17 +0100244#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
245 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100246 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
247 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100248 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
250 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
251 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
252 return( PSA_ERROR_NOT_SUPPORTED );
253 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
254 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
255#endif
256
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200257 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
258 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
259 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
260 return( PSA_ERROR_HARDWARE_FAILURE );
261
Gilles Peskinea5905292018-02-07 20:59:33 +0100262 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
263 return( PSA_ERROR_NOT_SUPPORTED );
264 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MD_ALLOC_FAILED:
267 return( PSA_ERROR_INSUFFICIENT_MEMORY );
268 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
269 return( PSA_ERROR_STORAGE_FAILURE );
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200270 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
271 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100272
Gilles Peskinef76aa772018-10-29 19:24:33 +0100273 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
274 return( PSA_ERROR_STORAGE_FAILURE );
275 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
280 return( PSA_ERROR_BUFFER_TOO_SMALL );
281 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
288 return( PSA_ERROR_INSUFFICIENT_MEMORY );
289
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100290 case MBEDTLS_ERR_PK_ALLOC_FAILED:
291 return( PSA_ERROR_INSUFFICIENT_MEMORY );
292 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
293 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
294 return( PSA_ERROR_INVALID_ARGUMENT );
295 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100297 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
298 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
299 return( PSA_ERROR_INVALID_ARGUMENT );
300 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
301 return( PSA_ERROR_NOT_SUPPORTED );
302 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
303 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
304 return( PSA_ERROR_NOT_PERMITTED );
305 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_PK_INVALID_ALG:
308 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
309 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
312 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315
Gilles Peskineff2d2002019-05-06 15:26:23 +0200316 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
317 return( PSA_ERROR_HARDWARE_FAILURE );
318 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
319 return( PSA_ERROR_NOT_SUPPORTED );
320
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200321 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
322 return( PSA_ERROR_HARDWARE_FAILURE );
323
Gilles Peskinea5905292018-02-07 20:59:33 +0100324 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
325 return( PSA_ERROR_INVALID_ARGUMENT );
326 case MBEDTLS_ERR_RSA_INVALID_PADDING:
327 return( PSA_ERROR_INVALID_PADDING );
328 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
331 return( PSA_ERROR_INVALID_ARGUMENT );
332 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
333 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200334 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100335 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
336 return( PSA_ERROR_INVALID_SIGNATURE );
337 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
338 return( PSA_ERROR_BUFFER_TOO_SMALL );
339 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100340 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100341 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
342 return( PSA_ERROR_NOT_SUPPORTED );
343 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
344 return( PSA_ERROR_HARDWARE_FAILURE );
345
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200346 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
347 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
348 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
349 return( PSA_ERROR_HARDWARE_FAILURE );
350
Gilles Peskinea5905292018-02-07 20:59:33 +0100351 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
352 return( PSA_ERROR_INVALID_ARGUMENT );
353 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
355
itayzafrir5c753392018-05-08 11:18:38 +0300356 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300357 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300358 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300359 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
360 return( PSA_ERROR_BUFFER_TOO_SMALL );
361 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
362 return( PSA_ERROR_NOT_SUPPORTED );
363 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
364 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
365 return( PSA_ERROR_INVALID_SIGNATURE );
366 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
367 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100368 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
369 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300370 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
371 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100372
Janos Follatha13b9052019-11-22 12:48:59 +0000373 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
374 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300375
Gilles Peskinee59236f2018-01-27 23:32:46 +0100376 default:
David Saadab4ecc272019-02-14 13:48:10 +0200377 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100378 }
379}
380
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200381
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200382
383
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100384/****************************************************************/
385/* Key management */
386/****************************************************************/
387
John Durkop9814fa22020-11-04 12:28:15 -0800388#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800389 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
Ronald Cron9ecdd972021-11-19 18:02:34 +0100390 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
391 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) || \
392 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100393mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100394 size_t bits,
395 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200396{
397 switch( curve )
398 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100399 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100400 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100401 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100402#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100403 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100404 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100405#endif
406#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100408 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100409#endif
410#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100413#endif
414#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100415 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100416 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100417#endif
418#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100419 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100420 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100421 case 528:
422 if( bits_is_sloppy )
423 return( MBEDTLS_ECP_DP_SECP521R1 );
424 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100425#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 }
427 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100428
Paul Elliott8ff510a2020-06-02 17:19:28 +0100429 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100430 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100431 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100432#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100434 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100435#endif
436#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100437 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100438 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100439#endif
440#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100441 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100442 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100443#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100444 }
445 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100446
Paul Elliott8ff510a2020-06-02 17:19:28 +0100447 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100448 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100449 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100452 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100453 case 256:
454 if( bits_is_sloppy )
455 return( MBEDTLS_ECP_DP_CURVE25519 );
456 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100457#endif
458#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100460 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100461#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100462 }
463 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100464
Paul Elliott8ff510a2020-06-02 17:19:28 +0100465 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100466 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100467 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100468#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100470 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100471#endif
472#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100474 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100475#endif
476#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100477 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100478 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100479#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100480 }
481 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200482 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100483
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100484 (void) bits_is_sloppy;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100485 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200486}
John Durkop9814fa22020-11-04 12:28:15 -0800487#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
Ronald Cron9ecdd972021-11-19 18:02:34 +0100488 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
489 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
490 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) ||
491 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200492
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200493static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
494 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495{
496 /* Check that the bit size is acceptable for the key type */
497 switch( type )
498 {
499 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200500 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200501 case PSA_KEY_TYPE_DERIVE:
502 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100503#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504 case PSA_KEY_TYPE_AES:
505 if( bits != 128 && bits != 192 && bits != 256 )
506 return( PSA_ERROR_INVALID_ARGUMENT );
507 break;
508#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200509#if defined(PSA_WANT_KEY_TYPE_ARIA)
510 case PSA_KEY_TYPE_ARIA:
511 if( bits != 128 && bits != 192 && bits != 256 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100515#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200516 case PSA_KEY_TYPE_CAMELLIA:
517 if( bits != 128 && bits != 192 && bits != 256 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100521#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 case PSA_KEY_TYPE_DES:
523 if( bits != 64 && bits != 128 && bits != 192 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100527#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528 case PSA_KEY_TYPE_ARC4:
529 if( bits < 8 || bits > 2048 )
530 return( PSA_ERROR_INVALID_ARGUMENT );
531 break;
532#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100533#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200534 case PSA_KEY_TYPE_CHACHA20:
535 if( bits != 256 )
536 return( PSA_ERROR_INVALID_ARGUMENT );
537 break;
538#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200539 default:
540 return( PSA_ERROR_NOT_SUPPORTED );
541 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200542 if( bits % 8 != 0 )
543 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200545 return( PSA_SUCCESS );
546}
547
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 *
Steven Cooremancd640932021-03-08 12:00:27 +0100550 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
551 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100552 *
553 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 * \param[in] key_type The key type of the key to be used with the
555 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100556 *
557 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100558 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100559 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100560 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100561 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100562MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100563 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565{
Steven Cooremancd640932021-03-08 12:00:27 +0100566 if( PSA_ALG_IS_HMAC( algorithm ) )
567 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100568 if( key_type == PSA_KEY_TYPE_HMAC )
569 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100570 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100571
572 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100573 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100574 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
575 * key. */
576 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
577 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
578 {
579 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
580 * the block length (larger than 1) for block ciphers. */
581 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
582 return( PSA_SUCCESS );
583 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100584 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100585
586 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100587}
588
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200589psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
590 size_t buffer_length )
Steven Cooreman75b74362020-07-28 14:30:13 +0200591{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100592 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200593 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200594
Ronald Cronea0f8a62020-11-25 17:52:23 +0100595 slot->key.data = mbedtls_calloc( 1, buffer_length );
596 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200597 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200598
Ronald Cronea0f8a62020-11-25 17:52:23 +0100599 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200600 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200601}
602
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200603psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
604 const uint8_t* data,
605 size_t data_length )
606{
607 psa_status_t status = psa_allocate_buffer_to_slot( slot,
608 data_length );
609 if( status != PSA_SUCCESS )
610 return( status );
611
Ronald Cronea0f8a62020-11-25 17:52:23 +0100612 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200613 return( PSA_SUCCESS );
614}
615
Ronald Crona0fe59f2020-11-29 11:14:53 +0100616psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100617 const psa_key_attributes_t *attributes,
618 const uint8_t *data, size_t data_length,
619 uint8_t *key_buffer, size_t key_buffer_size,
620 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100622 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
623 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200625 /* zero-length keys are never supported. */
626 if( data_length == 0 )
627 return( PSA_ERROR_NOT_SUPPORTED );
628
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100629 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100631 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200632
Steven Cooreman75b74362020-07-28 14:30:13 +0200633 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
634 if( data_length > SIZE_MAX / 8 )
635 return( PSA_ERROR_NOT_SUPPORTED );
636
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200637 /* Enforce a size limit, and in particular ensure that the bit
638 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100639 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200640 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200641
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100642 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200643 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200644 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200645
Ronald Crondd04d422020-11-28 15:14:42 +0100646 /* Copy the key material. */
647 memcpy( key_buffer, data, data_length );
648 *key_buffer_length = data_length;
649 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200650
Steven Cooreman40120f62020-10-29 11:42:22 +0100651 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100653 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200654 {
John Durkop9814fa22020-11-04 12:28:15 -0800655#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
656 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100657 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200658 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100659 return( mbedtls_psa_ecp_import_key( attributes,
660 data, data_length,
661 key_buffer, key_buffer_size,
662 key_buffer_length,
663 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100664 }
John Durkop9814fa22020-11-04 12:28:15 -0800665#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
666 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700667#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
668 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100669 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200670 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100671 return( mbedtls_psa_rsa_import_key( attributes,
672 data, data_length,
673 key_buffer, key_buffer_size,
674 key_buffer_length,
675 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200676 }
John Durkop9814fa22020-11-04 12:28:15 -0800677#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
678 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100679 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100680
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100681 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100682}
683
Gilles Peskinef603c712019-01-19 13:40:11 +0100684/** Calculate the intersection of two algorithm usage policies.
685 *
686 * Return 0 (which allows no operation) on incompatibility.
687 */
688static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100689 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100690 psa_algorithm_t alg1,
691 psa_algorithm_t alg2 )
692{
Gilles Peskine549ea862019-05-22 11:45:59 +0200693 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100694 if( alg1 == alg2 )
695 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100696 /* If the policies are from the same hash-and-sign family, check
697 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine8cb22c82021-09-22 16:15:05 +0200698 if( PSA_ALG_IS_SIGN_HASH( alg1 ) &&
699 PSA_ALG_IS_SIGN_HASH( alg2 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100700 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100701 {
Steven Cooreman03488022021-02-18 12:07:20 +0100702 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
703 return( alg2 );
704 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
705 return( alg1 );
706 }
707 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100708 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100709 * restrictive tag length. */
710 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
711 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
712 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
713 {
714 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
715 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100716 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100717
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100718 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100719 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
720 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100721 {
Steven Cooremancd640932021-03-08 12:00:27 +0100722 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
723 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100724 }
725 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100726 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100727 ( alg1_len <= alg2_len ) )
728 {
729 return( alg2 );
730 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100731 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100732 ( alg2_len <= alg1_len ) )
733 {
734 return( alg1 );
735 }
736 }
737 /* If the policies are from the same MAC family, check whether one
738 * of them is a minimum-MAC-length policy. Calculate the most
739 * restrictive tag length. */
740 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
741 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
742 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
743 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100744 /* Validate the combination of key type and algorithm. Since the base
745 * algorithm of alg1 and alg2 are the same, we only need this once. */
746 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100747 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100748
Steven Cooreman31a876d2021-03-03 20:47:40 +0100749 /* Get the (exact or at-least) output lengths for both sides of the
750 * requested intersection. None of the currently supported algorithms
751 * have an output length dependent on the actual key size, so setting it
752 * to a bogus value of 0 is currently OK.
753 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100754 * Note that for at-least-this-length wildcard algorithms, the output
755 * length is set to the shortest allowed length, which allows us to
756 * calculate the most restrictive tag length for the intersection. */
757 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
758 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100759 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100760
Steven Cooremana1d83222021-02-25 10:20:29 +0100761 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100762 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
763 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100764 {
Steven Cooremancd640932021-03-08 12:00:27 +0100765 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100766 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100767
768 /* If only one is an at-least-this-length policy, the intersection would
769 * be the other (fixed-length) policy as long as said fixed length is
770 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100771 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100772 {
Steven Cooremancd640932021-03-08 12:00:27 +0100773 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100774 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100775 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100776 {
Steven Cooremancd640932021-03-08 12:00:27 +0100777 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100778 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100779
Steven Cooremancd640932021-03-08 12:00:27 +0100780 /* If none of them are wildcards, check whether they define the same tag
781 * length. This is still possible here when one is default-length and
782 * the other specific-length. Ensure to always return the
783 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100784 if( alg1_len == alg2_len )
785 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100786 }
787 /* If the policies are incompatible, allow nothing. */
788 return( 0 );
789}
790
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100791static int psa_key_algorithm_permits( psa_key_type_t key_type,
792 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200793 psa_algorithm_t requested_alg )
794{
Gilles Peskine549ea862019-05-22 11:45:59 +0200795 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200796 if( requested_alg == policy_alg )
797 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100798 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
799 * and requested_alg is the same hash-and-sign family with any hash,
800 * then requested_alg is compliant with policy_alg. */
Gilles Peskine8cb22c82021-09-22 16:15:05 +0200801 if( PSA_ALG_IS_SIGN_HASH( requested_alg ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100802 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200803 {
Steven Cooreman03488022021-02-18 12:07:20 +0100804 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
805 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
806 }
807 /* If policy_alg is a wildcard AEAD algorithm of the same base as
808 * the requested algorithm, check the requested tag length to be
809 * equal-length or longer than the wildcard-specified length. */
810 if( PSA_ALG_IS_AEAD( policy_alg ) &&
811 PSA_ALG_IS_AEAD( requested_alg ) &&
812 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
813 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100814 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100815 {
816 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
817 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
818 }
Steven Cooremancd640932021-03-08 12:00:27 +0100819 /* If policy_alg is a MAC algorithm of the same base as the requested
820 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100821 if( PSA_ALG_IS_MAC( policy_alg ) &&
822 PSA_ALG_IS_MAC( requested_alg ) &&
823 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100824 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100825 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100826 /* Validate the combination of key type and algorithm. Since the policy
827 * and requested algorithms are the same, we only need this once. */
828 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100829 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100830
Steven Cooreman31a876d2021-03-03 20:47:40 +0100831 /* Get both the requested output length for the algorithm which is to be
832 * verified, and the default output length for the base algorithm.
833 * Note that none of the currently supported algorithms have an output
834 * length dependent on actual key size, so setting it to a bogus value
835 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100836 size_t requested_output_length = PSA_MAC_LENGTH(
837 key_type, 0, requested_alg );
838 size_t default_output_length = PSA_MAC_LENGTH(
839 key_type, 0,
840 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100841
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100842 /* If the policy is default-length, only allow an algorithm with
843 * a declared exact-length matching the default. */
844 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100845 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100846
847 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100848 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100849 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
850 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
851 {
852 return( 1 );
853 }
854
Steven Cooremancd640932021-03-08 12:00:27 +0100855 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
856 * check for the requested MAC length to be equal to or longer than the
857 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100858 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
859 {
860 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100861 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100862 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200863 }
Steven Cooremance48e852020-10-05 16:02:45 +0200864 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200865 * a key derivation with that key agreement should also be allowed. This
866 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200867 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
868 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
869 {
870 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
871 policy_alg );
872 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100873 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874 return( 0 );
875}
876
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100877/** Test whether a policy permits an algorithm.
878 *
879 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100880 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100881 * \note This function requires providing the key type for which the policy is
882 * being validated, since some algorithm policy definitions (e.g. MAC)
883 * have different properties depending on what kind of cipher it is
884 * combined with.
885 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100886 * \retval PSA_SUCCESS When \p alg is a specific algorithm
887 * allowed by the \p policy.
888 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
889 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
890 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100891 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100892static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100893 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100894 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100895{
Steven Cooremand788fab2021-02-25 11:29:17 +0100896 /* '0' is not a valid algorithm */
897 if( alg == 0 )
898 return( PSA_ERROR_INVALID_ARGUMENT );
899
Steven Cooreman7e39f052021-02-23 14:18:32 +0100900 /* A requested algorithm cannot be a wildcard. */
901 if( PSA_ALG_IS_WILDCARD( alg ) )
902 return( PSA_ERROR_INVALID_ARGUMENT );
903
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100904 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
905 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100906 return( PSA_SUCCESS );
907 else
908 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100909}
910
Gilles Peskinef603c712019-01-19 13:40:11 +0100911/** Restrict a key policy based on a constraint.
912 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100913 * \note This function requires providing the key type for which the policy is
914 * being restricted, since some algorithm policy definitions (e.g. MAC)
915 * have different properties depending on what kind of cipher it is
916 * combined with.
917 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100918 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100919 * \param[in,out] policy The policy to restrict.
920 * \param[in] constraint The policy constraint to apply.
921 *
922 * \retval #PSA_SUCCESS
923 * \c *policy contains the intersection of the original value of
924 * \c *policy and \c *constraint.
925 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100926 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100927 * \c *policy is unchanged.
928 */
929static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100930 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100931 psa_key_policy_t *policy,
932 const psa_key_policy_t *constraint )
933{
934 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100935 psa_key_policy_algorithm_intersection( key_type, policy->alg,
936 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200937 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100938 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
939 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100940 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
941 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200942 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
943 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100944 policy->usage &= constraint->usage;
945 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200946 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100947 return( PSA_SUCCESS );
948}
949
Ronald Cron5c522922020-11-14 16:35:34 +0100950/** Get the description of a key given its identifier and policy constraints
951 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200952 *
Ronald Cron5c522922020-11-14 16:35:34 +0100953 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100954 * nonzero, the key must allow operations with this algorithm. If \p alg is
955 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100956 *
Ronald Cron5c522922020-11-14 16:35:34 +0100957 * In case of a persistent key, the function loads the description of the key
958 * into a key slot if not already done.
959 *
960 * On success, the returned key slot is locked. It is the responsibility of
961 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200962 */
Ronald Cron5c522922020-11-14 16:35:34 +0100963static psa_status_t psa_get_and_lock_key_slot_with_policy(
964 mbedtls_svc_key_id_t key,
965 psa_key_slot_t **p_slot,
966 psa_key_usage_t usage,
967 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100968{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200969 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
970 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100971
Ronald Cron5c522922020-11-14 16:35:34 +0100972 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100973 if( status != PSA_SUCCESS )
974 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200975 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100976
977 /* Enforce that usage policy for the key slot contains all the flags
978 * required by the usage parameter. There is one exception: public
979 * keys can always be exported, so we treat public key objects as
980 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100982 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200983
Gilles Peskine8e338702019-07-30 20:06:31 +0200984 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100985 {
986 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200987 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100988 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100989
990 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100991 if( alg != 0 )
992 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100993 status = psa_key_policy_permits( &slot->attr.policy,
994 slot->attr.type,
995 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +0100996 if( status != PSA_SUCCESS )
997 goto error;
998 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100999
Darryl Green06fd18d2018-07-16 11:21:11 +01001000 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001
1002error:
1003 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001004 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001005
1006 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001007}
Darryl Green940d72c2018-07-13 13:18:51 +01001008
Ronald Cron5c522922020-11-14 16:35:34 +01001009/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001010 *
1011 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001012 * available, as opposed to a key in a secure element and/or to be used
1013 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001015 * This is a temporary function that may be used instead of
1016 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1017 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001018 *
Ronald Cron5c522922020-11-14 16:35:34 +01001019 * On success, the returned key slot is locked. It is the responsibility of the
1020 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001021 */
Ronald Cron5c522922020-11-14 16:35:34 +01001022static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1023 mbedtls_svc_key_id_t key,
1024 psa_key_slot_t **p_slot,
1025 psa_key_usage_t usage,
1026 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001027{
Ronald Cron5c522922020-11-14 16:35:34 +01001028 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1029 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001030 if( status != PSA_SUCCESS )
1031 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001032
Ronald Cron8a0466a2021-08-24 15:39:44 +02001033 if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001034 {
Ronald Cron5c522922020-11-14 16:35:34 +01001035 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001036 *p_slot = NULL;
1037 return( PSA_ERROR_NOT_SUPPORTED );
1038 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001039
Gilles Peskine28f8f302019-07-24 13:30:31 +02001040 return( PSA_SUCCESS );
1041}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001042
Steven Cooreman7ddee7f2021-04-07 18:08:30 +02001043psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001044{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001045 /* Data pointer will always be either a valid pointer or NULL in an
1046 * initialized slot, so we can just free it. */
1047 if( slot->key.data != NULL )
1048 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1049
1050 mbedtls_free( slot->key.data );
1051 slot->key.data = NULL;
1052 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001053
1054 return( PSA_SUCCESS );
1055}
1056
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001057/** Completely wipe a slot in memory, including its policy.
1058 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001059psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001060{
1061 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001062
1063 /*
1064 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001065 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001066 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1067 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001068 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001069 */
Ronald Cron5c522922020-11-14 16:35:34 +01001070 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001071 {
1072#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001073 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001074#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001075 status = PSA_ERROR_CORRUPTION_DETECTED;
1076 }
1077
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001078 /* Multipart operations may still be using the key. This is safe
1079 * because all multipart operation objects are independent from
1080 * the key slot: if they need to access the key after the setup
1081 * phase, they have a copy of the key. Note that this means that
1082 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001083 /* At this point, key material and other type-specific content has
1084 * been wiped. Clear remaining metadata. We can call memset and not
1085 * zeroize because the metadata is not particularly sensitive. */
1086 memset( slot, 0, sizeof( *slot ) );
1087 return( status );
1088}
1089
Ronald Croncf56a0a2020-08-04 09:51:30 +02001090psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001092 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001093 psa_status_t status; /* status of the last operation */
1094 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001095#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1096 psa_se_drv_table_entry_t *driver;
1097#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098
Ronald Croncf56a0a2020-08-04 09:51:30 +02001099 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001100 return( PSA_SUCCESS );
1101
Ronald Cronf2911112020-10-29 17:51:10 +01001102 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001103 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001104 * key, this will load the key description from persistent memory if not
1105 * done yet. We cannot avoid this loading as without it we don't know if
1106 * the key is operated by an SE or not and this information is needed by
1107 * the current implementation.
1108 */
Ronald Cron5c522922020-11-14 16:35:34 +01001109 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001110 if( status != PSA_SUCCESS )
1111 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001112
Ronald Cronf2911112020-10-29 17:51:10 +01001113 /*
1114 * If the key slot containing the key description is under access by the
1115 * library (apart from the present access), the key cannot be destroyed
1116 * yet. For the time being, just return in error. Eventually (to be
1117 * implemented), the key should be destroyed when all accesses have
1118 * stopped.
1119 */
Ronald Cron5c522922020-11-14 16:35:34 +01001120 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001121 {
Ronald Cron5c522922020-11-14 16:35:34 +01001122 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001123 return( PSA_ERROR_GENERIC_ERROR );
1124 }
1125
Gilles Peskine251c7742021-04-21 22:32:05 +02001126 if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) )
1127 {
1128 /* Refuse the destruction of a read-only key (which may or may not work
1129 * if we attempt it, depending on whether the key is merely read-only
1130 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001131 * Just do the best we can, which is to wipe the copy in memory
1132 * (done in this function's cleanup code). */
1133 overall_status = PSA_ERROR_NOT_PERMITTED;
1134 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001135 }
1136
Gilles Peskine354f7672019-07-12 23:46:38 +02001137#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001138 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001139 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001140 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001141 /* For a key in a secure element, we need to do three things:
1142 * remove the key file in internal storage, destroy the
1143 * key inside the secure element, and update the driver's
1144 * persistent data. Start a transaction that will encompass these
1145 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001146 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001147 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001148 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001149 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001150 status = psa_crypto_save_transaction( );
1151 if( status != PSA_SUCCESS )
1152 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001153 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001154 /* We should still try to destroy the key in the secure
1155 * element and the key metadata in storage. This is especially
1156 * important if the error is that the storage is full.
1157 * But how to do it exactly without risking an inconsistent
1158 * state after a reset?
1159 * https://github.com/ARMmbed/mbed-crypto/issues/215
1160 */
1161 overall_status = status;
1162 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001163 }
1164
Ronald Cronea0f8a62020-11-25 17:52:23 +01001165 status = psa_destroy_se_key( driver,
1166 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001167 if( overall_status == PSA_SUCCESS )
1168 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001169 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001170#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1171
Darryl Greend49a4992018-06-18 17:27:26 +01001172#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001173 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001174 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001175 status = psa_destroy_persistent_key( slot->attr.id );
1176 if( overall_status == PSA_SUCCESS )
1177 overall_status = status;
1178
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001179 /* TODO: other slots may have a copy of the same key. We should
1180 * invalidate them.
1181 * https://github.com/ARMmbed/mbed-crypto/issues/214
1182 */
Darryl Greend49a4992018-06-18 17:27:26 +01001183 }
1184#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001185
Gilles Peskinefc762652019-07-22 19:30:34 +02001186#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1187 if( driver != NULL )
1188 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001189 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001190 if( overall_status == PSA_SUCCESS )
1191 overall_status = status;
1192 status = psa_crypto_stop_transaction( );
1193 if( overall_status == PSA_SUCCESS )
1194 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001195 }
1196#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1197
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001198exit:
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001199 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001200 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001201 if( status != PSA_SUCCESS )
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001202 overall_status = status;
1203 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001204}
1205
John Durkop07cc04a2020-11-16 22:08:34 -08001206#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001207 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001208static psa_status_t psa_get_rsa_public_exponent(
1209 const mbedtls_rsa_context *rsa,
1210 psa_key_attributes_t *attributes )
1211{
1212 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001214 uint8_t *buffer = NULL;
1215 size_t buflen;
1216 mbedtls_mpi_init( &mpi );
1217
1218 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1219 if( ret != 0 )
1220 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001221 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1222 {
1223 /* It's the default value, which is reported as an empty string,
1224 * so there's nothing to do. */
1225 goto exit;
1226 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001227
1228 buflen = mbedtls_mpi_size( &mpi );
1229 buffer = mbedtls_calloc( 1, buflen );
1230 if( buffer == NULL )
1231 {
1232 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1233 goto exit;
1234 }
1235 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1236 if( ret != 0 )
1237 goto exit;
1238 attributes->domain_parameters = buffer;
1239 attributes->domain_parameters_size = buflen;
1240
1241exit:
1242 mbedtls_mpi_free( &mpi );
1243 if( ret != 0 )
1244 mbedtls_free( buffer );
1245 return( mbedtls_to_psa_error( ret ) );
1246}
John Durkop07cc04a2020-11-16 22:08:34 -08001247#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001248 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001249
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001250/** Retrieve all the publicly-accessible attributes of a key.
1251 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001252psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001253 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001255 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001256 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001257 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001258
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001259 psa_reset_key_attributes( attributes );
1260
Ronald Cron5c522922020-11-14 16:35:34 +01001261 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001262 if( status != PSA_SUCCESS )
1263 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001264
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001265 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001266 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1267 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1268
1269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron0518f612021-08-24 15:50:05 +02001270 if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001271 psa_set_key_slot_number( attributes,
1272 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001273#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001274
Gilles Peskine8e338702019-07-30 20:06:31 +02001275 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001276 {
John Durkop07cc04a2020-11-16 22:08:34 -08001277#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001278 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001279 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001280 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001281 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001282 * is not yet implemented.
1283 * https://github.com/ARMmbed/mbed-crypto/issues/216
1284 */
Ronald Cron41e80062021-08-24 16:00:51 +02001285 if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02001286 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001287 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001288
Ronald Cron90857082020-11-25 14:58:33 +01001289 status = mbedtls_psa_rsa_load_representation(
1290 slot->attr.type,
1291 slot->key.data,
1292 slot->key.bytes,
1293 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001294 if( status != PSA_SUCCESS )
1295 break;
1296
Steven Cooremana2371e52020-07-28 14:30:39 +02001297 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001298 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001299 mbedtls_rsa_free( rsa );
1300 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001301 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001302 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001303#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001304 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001305 default:
1306 /* Nothing else to do. */
1307 break;
1308 }
1309
1310 if( status != PSA_SUCCESS )
1311 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001312
Ronald Cron5c522922020-11-14 16:35:34 +01001313 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001314
Ronald Cron5c522922020-11-14 16:35:34 +01001315 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001316}
1317
Gilles Peskinec8000c02019-08-02 20:15:51 +02001318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1319psa_status_t psa_get_key_slot_number(
1320 const psa_key_attributes_t *attributes,
1321 psa_key_slot_number_t *slot_number )
1322{
1323 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1324 {
1325 *slot_number = attributes->slot_number;
1326 return( PSA_SUCCESS );
1327 }
1328 else
1329 return( PSA_ERROR_INVALID_ARGUMENT );
1330}
1331#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1332
Ronald Crond18b5f82020-11-25 16:46:05 +01001333static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1334 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001335 uint8_t *data,
1336 size_t data_size,
1337 size_t *data_length )
1338{
Ronald Crond18b5f82020-11-25 16:46:05 +01001339 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001340 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001341 memcpy( data, key_buffer, key_buffer_size );
1342 memset( data + key_buffer_size, 0,
1343 data_size - key_buffer_size );
1344 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001345 return( PSA_SUCCESS );
1346}
1347
Ronald Cron7285cda2020-11-26 14:46:37 +01001348psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 const psa_key_attributes_t *attributes,
1350 const uint8_t *key_buffer, size_t key_buffer_size,
1351 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352{
Ronald Crond18b5f82020-11-25 16:46:05 +01001353 psa_key_type_t type = attributes->core.type;
1354
Ronald Crond18b5f82020-11-25 16:46:05 +01001355 if( key_type_is_raw_bytes( type ) ||
1356 PSA_KEY_TYPE_IS_RSA( type ) ||
1357 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001358 {
1359 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001360 key_buffer, key_buffer_size,
1361 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001362 }
1363 else
1364 {
1365 /* This shouldn't happen in the reference implementation, but
1366 it is valid for a special-purpose implementation to omit
1367 support for exporting certain key types. */
1368 return( PSA_ERROR_NOT_SUPPORTED );
1369 }
1370}
1371
1372psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1373 uint8_t *data,
1374 size_t data_size,
1375 size_t *data_length )
1376{
1377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1378 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1379 psa_key_slot_t *slot;
1380
Ronald Crone907e552021-01-18 13:22:38 +01001381 /* Reject a zero-length output buffer now, since this can never be a
1382 * valid key representation. This way we know that data must be a valid
1383 * pointer and we can do things like memset(data, ..., data_size). */
1384 if( data_size == 0 )
1385 return( PSA_ERROR_BUFFER_TOO_SMALL );
1386
Ronald Cron9486f9d2020-11-26 13:36:23 +01001387 /* Set the key to empty now, so that even when there are errors, we always
1388 * set data_length to a value between 0 and data_size. On error, setting
1389 * the key to empty is a good choice because an empty key representation is
1390 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001391 *data_length = 0;
1392
Ronald Cron9486f9d2020-11-26 13:36:23 +01001393 /* Export requires the EXPORT flag. There is an exception for public keys,
1394 * which don't require any flag, but
1395 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1396 */
1397 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1398 PSA_KEY_USAGE_EXPORT, 0 );
1399 if( status != PSA_SUCCESS )
1400 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001401
Ronald Crond18b5f82020-11-25 16:46:05 +01001402 psa_key_attributes_t attributes = {
1403 .core = slot->attr
1404 };
Ronald Cron67227982020-11-26 15:16:05 +01001405 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001406 slot->key.data, slot->key.bytes,
1407 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001408
Ronald Cron9486f9d2020-11-26 13:36:23 +01001409 unlock_status = psa_unlock_key_slot( slot );
1410
1411 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1412}
1413
Ronald Cron7285cda2020-11-26 14:46:37 +01001414psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001415 const psa_key_attributes_t *attributes,
1416 const uint8_t *key_buffer,
1417 size_t key_buffer_size,
1418 uint8_t *data,
1419 size_t data_size,
1420 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001421{
Ronald Crond18b5f82020-11-25 16:46:05 +01001422 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001423
Ronald Crond18b5f82020-11-25 16:46:05 +01001424 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001425 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001426 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001427 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001428 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001429 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001430 key_buffer, key_buffer_size,
1431 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001432 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001433
Ronald Crond18b5f82020-11-25 16:46:05 +01001434 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001435 {
John Durkop0e005192020-10-31 22:06:54 -07001436#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1437 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001438 return( mbedtls_psa_rsa_export_public_key( attributes,
1439 key_buffer,
1440 key_buffer_size,
1441 data,
1442 data_size,
1443 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001444#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001445 /* We don't know how to convert a private RSA key to public. */
1446 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001447#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1448 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001449 }
1450 else
Moran Pekera998bc62018-04-16 18:16:20 +03001451 {
John Durkop6ba40d12020-11-10 08:50:04 -08001452#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1453 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001454 return( mbedtls_psa_ecp_export_public_key( attributes,
1455 key_buffer,
1456 key_buffer_size,
1457 data,
1458 data_size,
1459 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001460#else
1461 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001462 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001463#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1464 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001465 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001466 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001467 else
1468 {
1469 /* This shouldn't happen in the reference implementation, but
1470 it is valid for a special-purpose implementation to omit
1471 support for exporting certain key types. */
1472 return( PSA_ERROR_NOT_SUPPORTED );
1473 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001474}
Ronald Crond18b5f82020-11-25 16:46:05 +01001475
Ronald Croncf56a0a2020-08-04 09:51:30 +02001476psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001477 uint8_t *data,
1478 size_t data_size,
1479 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001480{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001482 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001483 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001484
Ronald Crone907e552021-01-18 13:22:38 +01001485 /* Reject a zero-length output buffer now, since this can never be a
1486 * valid key representation. This way we know that data must be a valid
1487 * pointer and we can do things like memset(data, ..., data_size). */
1488 if( data_size == 0 )
1489 return( PSA_ERROR_BUFFER_TOO_SMALL );
1490
Darryl Greendd8fb772018-11-07 16:00:44 +00001491 /* Set the key to empty now, so that even when there are errors, we always
1492 * set data_length to a value between 0 and data_size. On error, setting
1493 * the key to empty is a good choice because an empty key representation is
1494 * unlikely to be accepted anywhere. */
1495 *data_length = 0;
1496
1497 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001498 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001499 if( status != PSA_SUCCESS )
1500 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001501
Ronald Cron9486f9d2020-11-26 13:36:23 +01001502 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1503 {
1504 status = PSA_ERROR_INVALID_ARGUMENT;
1505 goto exit;
1506 }
1507
Ronald Crond18b5f82020-11-25 16:46:05 +01001508 psa_key_attributes_t attributes = {
1509 .core = slot->attr
1510 };
Ronald Cron67227982020-11-26 15:16:05 +01001511 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001512 &attributes, slot->key.data, slot->key.bytes,
1513 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001514
1515exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001516 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001517
Ronald Cron5c522922020-11-14 16:35:34 +01001518 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001519}
1520
Gilles Peskine91e8c332019-08-02 19:19:39 +02001521#if defined(static_assert)
1522static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1523 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001524static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001525 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001526static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001527 "One or more key attribute flag is listed as both internal-only and external-only" );
1528#endif
1529
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001530/** Validate that a key policy is internally well-formed.
1531 *
1532 * This function only rejects invalid policies. It does not validate the
1533 * consistency of the policy with respect to other attributes of the key
1534 * such as the key type.
1535 */
1536static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001537{
1538 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001539 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001540 PSA_KEY_USAGE_ENCRYPT |
1541 PSA_KEY_USAGE_DECRYPT |
gabor-mezei-arme8efa392021-04-14 21:14:28 +02001542 PSA_KEY_USAGE_SIGN_MESSAGE |
1543 PSA_KEY_USAGE_VERIFY_MESSAGE |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001544 PSA_KEY_USAGE_SIGN_HASH |
1545 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001546 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1547 return( PSA_ERROR_INVALID_ARGUMENT );
1548
Gilles Peskine4747d192019-04-17 15:05:45 +02001549 return( PSA_SUCCESS );
1550}
1551
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001552/** Validate the internal consistency of key attributes.
1553 *
1554 * This function only rejects invalid attribute values. If does not
1555 * validate the consistency of the attributes with any key data that may
1556 * be involved in the creation of the key.
1557 *
1558 * Call this function early in the key creation process.
1559 *
1560 * \param[in] attributes Key attributes for the new key.
1561 * \param[out] p_drv On any return, the driver for the key, if any.
1562 * NULL for a transparent key.
1563 *
1564 */
1565static psa_status_t psa_validate_key_attributes(
1566 const psa_key_attributes_t *attributes,
1567 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001568{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001569 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001570 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001571 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001572
Ronald Cron54b90082020-10-29 15:26:43 +01001573 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001574 if( status != PSA_SUCCESS )
1575 return( status );
1576
Ronald Crond2ed4812020-07-17 16:11:30 +02001577 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001578 if( status != PSA_SUCCESS )
1579 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001580
Ronald Cron65f38a32020-10-23 17:11:13 +02001581 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1582 {
1583 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1584 return( PSA_ERROR_INVALID_ARGUMENT );
1585 }
1586 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001587 {
Ronald Cron77e412c2021-03-31 17:36:31 +02001588 if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
1589 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Crond2ed4812020-07-17 16:11:30 +02001590 }
1591
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001592 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001593 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001594 return( status );
1595
1596 /* Refuse to create overly large keys.
1597 * Note that this doesn't trigger on import if the attributes don't
1598 * explicitly specify a size (so psa_get_key_bits returns 0), so
1599 * psa_import_key() needs its own checks. */
1600 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1601 return( PSA_ERROR_NOT_SUPPORTED );
1602
Gilles Peskine91e8c332019-08-02 19:19:39 +02001603 /* Reject invalid flags. These should not be reachable through the API. */
1604 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1605 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1606 return( PSA_ERROR_INVALID_ARGUMENT );
1607
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001608 return( PSA_SUCCESS );
1609}
1610
Gilles Peskine4747d192019-04-17 15:05:45 +02001611/** Prepare a key slot to receive key material.
1612 *
1613 * This function allocates a key slot and sets its metadata.
1614 *
1615 * If this function fails, call psa_fail_key_creation().
1616 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001617 * This function is intended to be used as follows:
1618 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001619 * it with the specified attributes, and in case of a volatile key assign it
1620 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001621 * -# Populate the slot with the key material.
1622 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1623 * In case of failure at any step, stop the sequence and call
1624 * psa_fail_key_creation().
1625 *
Ronald Cron5c522922020-11-14 16:35:34 +01001626 * On success, the key slot is locked. It is the responsibility of the caller
1627 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001628 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001629 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001630 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001631 * \param[out] p_slot On success, a pointer to the prepared slot.
1632 * \param[out] p_drv On any return, the driver for the key, if any.
1633 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001634 *
1635 * \retval #PSA_SUCCESS
1636 * The key slot is ready to receive key material.
1637 * \return If this function fails, the key slot is an invalid state.
1638 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001639 */
1640static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001641 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001642 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001643 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001644 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001645{
1646 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001647 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001648 psa_key_slot_t *slot;
1649
Gilles Peskinedf179142019-07-15 22:02:14 +02001650 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001651 *p_drv = NULL;
1652
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001653 status = psa_validate_key_attributes( attributes, p_drv );
1654 if( status != PSA_SUCCESS )
1655 return( status );
1656
Ronald Cronc4d1b512020-07-31 11:26:37 +02001657 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 if( status != PSA_SUCCESS )
1659 return( status );
1660 slot = *p_slot;
1661
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001662 /* We're storing the declared bit-size of the key. It's up to each
1663 * creation mechanism to verify that this information is correct.
1664 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001665 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001666 * is optional (import, copy). In case of a volatile key, assign it the
1667 * volatile key identifier associated to the slot returned to contain its
1668 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001669
1670 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001671 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1672 {
1673#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1674 slot->attr.id = volatile_key_id;
1675#else
1676 slot->attr.id.key_id = volatile_key_id;
1677#endif
1678 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001679
Gilles Peskine91e8c332019-08-02 19:19:39 +02001680 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001681 * external-only flags, query `attributes`. Thanks to the check
1682 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001683 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001684 * may have set. */
1685 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001686
Gilles Peskinecbaff462019-07-12 23:46:04 +02001687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001688 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001689 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001690 * create the key file in internal storage, create the
1691 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001692 * persistent data. This is done by starting a transaction that will
1693 * encompass these three actions.
1694 * For registering a volatile key, we just need to find an appropriate
1695 * slot number inside the SE. Since the key is designated volatile, creating
1696 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001697 /* The first thing to do is to find a slot number for the new key.
1698 * We save the slot number in persistent storage as part of the
1699 * transaction data. It will be needed to recover if the power
1700 * fails during the key creation process, to clean up on the secure
1701 * element side after restarting. Obtaining a slot number from the
1702 * secure element driver updates its persistent state, but we do not yet
1703 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001704 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001705 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001706 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001707 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001708 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001709 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001710 if( status != PSA_SUCCESS )
1711 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001712
1713 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001714 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001715 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1716 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001717 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001718 psa_crypto_transaction.key.id = slot->attr.id;
1719 status = psa_crypto_save_transaction( );
1720 if( status != PSA_SUCCESS )
1721 {
1722 (void) psa_crypto_stop_transaction( );
1723 return( status );
1724 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001725 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001726
1727 status = psa_copy_key_material_into_slot(
1728 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001729 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001730
1731 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1732 {
1733 /* Key registration only makes sense with a secure element. */
1734 return( PSA_ERROR_INVALID_ARGUMENT );
1735 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001736#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1737
Ronald Cronc4d1b512020-07-31 11:26:37 +02001738 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001739}
Gilles Peskine4747d192019-04-17 15:05:45 +02001740
1741/** Finalize the creation of a key once its key material has been set.
1742 *
1743 * This entails writing the key to persistent storage.
1744 *
1745 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001746 * See the documentation of psa_start_key_creation() for the intended use
1747 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 *
Ronald Cron5c522922020-11-14 16:35:34 +01001749 * If the finalization succeeds, the function unlocks the key slot (it was
1750 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1751 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001752 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001753 * \param[in,out] slot Pointer to the slot with key material.
1754 * \param[in] driver The secure element driver for the key,
1755 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001756 * \param[out] key On success, identifier of the key. Note that the
1757 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001758 *
1759 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001760 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001761 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1762 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1763 * \retval #PSA_ERROR_ALREADY_EXISTS
1764 * \retval #PSA_ERROR_DATA_INVALID
1765 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001766 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001767 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001768 * \return If this function fails, the key slot is an invalid state.
1769 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001771static psa_status_t psa_finish_key_creation(
1772 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001773 psa_se_drv_table_entry_t *driver,
1774 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001775{
1776 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001777 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001778 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001779
1780#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001781 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001782 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001783#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1784 if( driver != NULL )
1785 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001786 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001787 psa_key_slot_number_t slot_number =
1788 psa_key_slot_get_slot_number( slot ) ;
1789
Gilles Peskineb46bef22019-07-30 21:32:04 +02001790#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001791 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001792 sizeof( data.slot_number ),
1793 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001794#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001795 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001796 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001797 (uint8_t*) &data,
1798 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001799 }
1800 else
1801#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1802 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001803 /* Key material is saved in export representation in the slot, so
1804 * just pass the slot buffer for storage. */
1805 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001806 slot->key.data,
1807 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001808 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001809 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001810#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1811
Gilles Peskinecbaff462019-07-12 23:46:04 +02001812#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001813 /* Finish the transaction for a key creation. This does not
1814 * happen when registering an existing key. Detect this case
1815 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001816 * creation of a persistent key in a secure element requires a transaction,
1817 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001818 if( driver != NULL &&
1819 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001820 {
1821 status = psa_save_se_persistent_data( driver );
1822 if( status != PSA_SUCCESS )
1823 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001824 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001825 return( status );
1826 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001827 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001828 }
1829#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1830
Ronald Cron50972942020-11-14 11:28:25 +01001831 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001832 {
1833 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001834 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001835 if( status != PSA_SUCCESS )
1836 *key = MBEDTLS_SVC_KEY_ID_INIT;
1837 }
Ronald Cron50972942020-11-14 11:28:25 +01001838
Gilles Peskine4747d192019-04-17 15:05:45 +02001839 return( status );
1840}
1841
1842/** Abort the creation of a key.
1843 *
1844 * You may call this function after calling psa_start_key_creation(),
1845 * or after psa_finish_key_creation() fails. In other circumstances, this
1846 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001847 * See the documentation of psa_start_key_creation() for the intended use
1848 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001849 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001850 * \param[in,out] slot Pointer to the slot with key material.
1851 * \param[in] driver The secure element driver for the key,
1852 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001853 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001854static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001855 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001856{
Gilles Peskine011e4282019-06-26 18:34:38 +02001857 (void) driver;
1858
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 if( slot == NULL )
1860 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001861
1862#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001863 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001864 * element, and the failure happened later (when saving metadata
1865 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001866 * element.
1867 * https://github.com/ARMmbed/mbed-crypto/issues/217
1868 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001869
Gilles Peskined7729582019-08-05 15:55:54 +02001870 /* Abort the ongoing transaction if any (there may not be one if
1871 * the creation process failed before starting one, or if the
1872 * key creation is a registration of a key in a secure element).
1873 * Earlier functions must already have done what it takes to undo any
1874 * partial creation. All that's left is to update the transaction data
1875 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001876 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001877#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1878
Gilles Peskine4747d192019-04-17 15:05:45 +02001879 psa_wipe_key_slot( slot );
1880}
1881
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001882/** Validate optional attributes during key creation.
1883 *
1884 * Some key attributes are optional during key creation. If they are
1885 * specified in the attributes structure, check that they are consistent
1886 * with the data in the slot.
1887 *
1888 * This function should be called near the end of key creation, after
1889 * the slot in memory is fully populated but before saving persistent data.
1890 */
1891static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001892 const psa_key_slot_t *slot,
1893 const psa_key_attributes_t *attributes )
1894{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001895 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001897 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 return( PSA_ERROR_INVALID_ARGUMENT );
1899 }
1900
1901 if( attributes->domain_parameters_size != 0 )
1902 {
John Durkop0e005192020-10-31 22:06:54 -07001903#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1904 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001905 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001906 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001907 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001908 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001910
Ronald Cron90857082020-11-25 14:58:33 +01001911 psa_status_t status = mbedtls_psa_rsa_load_representation(
1912 slot->attr.type,
1913 slot->key.data,
1914 slot->key.bytes,
1915 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001916 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001917 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001918
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001919 mbedtls_mpi_init( &actual );
1920 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001921 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001922 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001923 mbedtls_rsa_free( rsa );
1924 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001925 if( ret != 0 )
1926 goto rsa_exit;
1927 ret = mbedtls_mpi_read_binary( &required,
1928 attributes->domain_parameters,
1929 attributes->domain_parameters_size );
1930 if( ret != 0 )
1931 goto rsa_exit;
1932 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1933 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1934 rsa_exit:
1935 mbedtls_mpi_free( &actual );
1936 mbedtls_mpi_free( &required );
1937 if( ret != 0)
1938 return( mbedtls_to_psa_error( ret ) );
1939 }
1940 else
John Durkop9814fa22020-11-04 12:28:15 -08001941#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1942 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001943 {
1944 return( PSA_ERROR_INVALID_ARGUMENT );
1945 }
1946 }
1947
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001948 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001950 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 return( PSA_ERROR_INVALID_ARGUMENT );
1952 }
1953
1954 return( PSA_SUCCESS );
1955}
1956
Gilles Peskine4747d192019-04-17 15:05:45 +02001957psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001958 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001959 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001960 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001961{
1962 psa_status_t status;
1963 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001964 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001965 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001966
Ronald Cron81709fc2020-11-14 12:10:32 +01001967 *key = MBEDTLS_SVC_KEY_ID_INIT;
1968
Gilles Peskine0f84d622019-09-12 19:03:13 +02001969 /* Reject zero-length symmetric keys (including raw data key objects).
1970 * This also rejects any key which might be encoded as an empty string,
1971 * which is never valid. */
1972 if( data_length == 0 )
1973 return( PSA_ERROR_INVALID_ARGUMENT );
1974
Gilles Peskinedf179142019-07-15 22:02:14 +02001975 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001976 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001977 if( status != PSA_SUCCESS )
1978 goto exit;
1979
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001980 /* In the case of a transparent key or an opaque key stored in local
1981 * storage (thus not in the case of generating a key in a secure element
1982 * or cryptoprocessor with storage), we have to allocate a buffer to
1983 * hold the generated key material. */
1984 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001985 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001986 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001987 if( status != PSA_SUCCESS )
1988 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001989 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001990
1991 bits = slot->attr.bits;
1992 status = psa_driver_wrapper_import_key( attributes,
1993 data, data_length,
1994 slot->key.data,
1995 slot->key.bytes,
1996 &slot->key.bytes, &bits );
1997 if( status != PSA_SUCCESS )
1998 goto exit;
1999
2000 if( slot->attr.bits == 0 )
2001 slot->attr.bits = (psa_key_bits_t) bits;
2002 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01002003 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002004 status = PSA_ERROR_INVALID_ARGUMENT;
2005 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002006 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002007
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002008 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002009 if( status != PSA_SUCCESS )
2010 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011
Ronald Cron81709fc2020-11-14 12:10:32 +01002012 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002013exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002014 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002015 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002016
Gilles Peskine4747d192019-04-17 15:05:45 +02002017 return( status );
2018}
2019
Gilles Peskined7729582019-08-05 15:55:54 +02002020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2021psa_status_t mbedtls_psa_register_se_key(
2022 const psa_key_attributes_t *attributes )
2023{
2024 psa_status_t status;
2025 psa_key_slot_t *slot = NULL;
2026 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002027 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002028
2029 /* Leaving attributes unspecified is not currently supported.
2030 * It could make sense to query the key type and size from the
2031 * secure element, but not all secure elements support this
2032 * and the driver HAL doesn't currently support it. */
2033 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2034 return( PSA_ERROR_NOT_SUPPORTED );
2035 if( psa_get_key_bits( attributes ) == 0 )
2036 return( PSA_ERROR_NOT_SUPPORTED );
2037
2038 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002039 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002040 if( status != PSA_SUCCESS )
2041 goto exit;
2042
Ronald Cron81709fc2020-11-14 12:10:32 +01002043 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002044
2045exit:
2046 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002047 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002048
Gilles Peskined7729582019-08-05 15:55:54 +02002049 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002050 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002051 return( status );
2052}
2053#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2054
Gilles Peskinef603c712019-01-19 13:40:11 +01002055static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002056 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002057{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002058 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002059 source->key.data,
2060 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002061 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002062 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002063
Steven Cooreman398aee52020-10-13 14:35:45 +02002064 target->attr.type = source->attr.type;
2065 target->attr.bits = source->attr.bits;
2066
2067 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002068}
2069
Ronald Croncf56a0a2020-08-04 09:51:30 +02002070psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002071 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002072 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002073{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002075 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002076 psa_key_slot_t *source_slot = NULL;
2077 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002079 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002080
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2082
Ronald Cron5c522922020-11-14 16:35:34 +01002083 status = psa_get_and_lock_transparent_key_slot_with_policy(
2084 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002085 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002086 goto exit;
2087
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002088 status = psa_validate_optional_attributes( source_slot,
2089 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002090 if( status != PSA_SUCCESS )
2091 goto exit;
2092
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002093 status = psa_restrict_key_policy( source_slot->attr.type,
2094 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002095 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002096 if( status != PSA_SUCCESS )
2097 goto exit;
2098
Ronald Cron81709fc2020-11-14 12:10:32 +01002099 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2100 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002101 if( status != PSA_SUCCESS )
2102 goto exit;
2103
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002104#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2105 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002106 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002107 /* Copying to a secure element is not implemented yet. */
2108 status = PSA_ERROR_NOT_SUPPORTED;
2109 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002110 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002111#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002112
Ronald Cron6cc66312021-04-02 12:27:47 +02002113 if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
2114 {
2115 /*
2116 * Copying through an opaque driver is not implemented yet, consider
2117 * a lifetime with an external location as an invalid parameter for
2118 * now.
2119 */
2120 status = PSA_ERROR_INVALID_ARGUMENT;
2121 goto exit;
2122 }
2123
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002124 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002125 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002126 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002127
Ronald Cron81709fc2020-11-14 12:10:32 +01002128 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002129exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002130 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002131 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002132
Ronald Cron5c522922020-11-14 16:35:34 +01002133 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002134
Ronald Cron5c522922020-11-14 16:35:34 +01002135 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002136}
2137
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002138
2139
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002140/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002141/* Message digests */
2142/****************************************************************/
2143
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002144psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2145{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002146 /* Aborting a non-active operation is allowed */
2147 if( operation->id == 0 )
2148 return( PSA_SUCCESS );
2149
2150 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2151 operation->id = 0;
2152
2153 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002154}
2155
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002156psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002157 psa_algorithm_t alg )
2158{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002159 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002160
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002161 /* A context must be freshly initialized before it can be set up. */
2162 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002163 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002164 status = PSA_ERROR_BAD_STATE;
2165 goto exit;
2166 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002167
2168 if( !PSA_ALG_IS_HASH( alg ) )
Dave Rodgmandb861792021-06-24 16:17:43 +01002169 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002170 status = PSA_ERROR_INVALID_ARGUMENT;
2171 goto exit;
2172 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002173
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002174 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2175 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002176 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2177
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002178 status = psa_driver_wrapper_hash_setup( operation, alg );
2179
2180exit:
2181 if( status != PSA_SUCCESS )
Dave Rodgmandb861792021-06-24 16:17:43 +01002182 psa_hash_abort( operation );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002183
2184 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002185}
2186
2187psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2188 const uint8_t *input,
2189 size_t input_length )
2190{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2192
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002193 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002194 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002195 status = PSA_ERROR_BAD_STATE;
2196 goto exit;
2197 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002198
Steven Cooremanc8288352021-03-04 14:02:19 +01002199 /* Don't require hash implementations to behave correctly on a
2200 * zero-length input, which may have an invalid pointer. */
2201 if( input_length == 0 )
2202 return( PSA_SUCCESS );
2203
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002204 status = psa_driver_wrapper_hash_update( operation, input, input_length );
2205
2206exit:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002207 if( status != PSA_SUCCESS )
2208 psa_hash_abort( operation );
2209
2210 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002211}
2212
2213psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2214 uint8_t *hash,
2215 size_t hash_size,
2216 size_t *hash_length )
2217{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002218 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002219 if( operation->id == 0 )
2220 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221
Steven Cooreman1e582352021-02-18 17:24:37 +01002222 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002223 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002224 psa_hash_abort( operation );
2225 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002226}
2227
Gilles Peskine2d277862018-06-18 15:41:12 +02002228psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2229 const uint8_t *hash,
2230 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002231{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002232 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002233 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002234 psa_status_t status = psa_hash_finish(
2235 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002236 actual_hash, sizeof( actual_hash ),
2237 &actual_hash_length );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002238
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 if( status != PSA_SUCCESS )
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002240 goto exit;
2241
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242 if( actual_hash_length != hash_length )
Dave Rodgmandb861792021-06-24 16:17:43 +01002243 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002244 status = PSA_ERROR_INVALID_SIGNATURE;
2245 goto exit;
2246 }
2247
Steven Cooremana2a1b802021-04-29 16:37:59 +02002248 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002249 status = PSA_ERROR_INVALID_SIGNATURE;
2250
2251exit:
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002252 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002253 if( status != PSA_SUCCESS )
2254 psa_hash_abort(operation);
2255
2256 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257}
2258
Gilles Peskine0a749c82019-11-28 19:33:58 +01002259psa_status_t psa_hash_compute( psa_algorithm_t alg,
2260 const uint8_t *input, size_t input_length,
2261 uint8_t *hash, size_t hash_size,
2262 size_t *hash_length )
2263{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002264 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002265 if( !PSA_ALG_IS_HASH( alg ) )
2266 return( PSA_ERROR_INVALID_ARGUMENT );
2267
Steven Cooreman1e582352021-02-18 17:24:37 +01002268 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2269 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002270}
2271
2272psa_status_t psa_hash_compare( psa_algorithm_t alg,
2273 const uint8_t *input, size_t input_length,
2274 const uint8_t *hash, size_t hash_length )
2275{
Ronald Cron3a95d2b2021-10-18 09:47:58 +02002276 uint8_t actual_hash[PSA_HASH_MAX_SIZE];
Steven Cooreman84d670d2021-02-18 16:22:53 +01002277 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002278
2279 if( !PSA_ALG_IS_HASH( alg ) )
2280 return( PSA_ERROR_INVALID_ARGUMENT );
2281
Steven Cooreman1e582352021-02-18 17:24:37 +01002282 psa_status_t status = psa_driver_wrapper_hash_compute(
2283 alg, input, input_length,
2284 actual_hash, sizeof(actual_hash),
2285 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002286 if( status != PSA_SUCCESS )
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002287 goto exit;
Steven Cooreman84d670d2021-02-18 16:22:53 +01002288 if( actual_hash_length != hash_length )
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002289 {
2290 status = PSA_ERROR_INVALID_SIGNATURE;
2291 goto exit;
2292 }
Steven Cooremana2a1b802021-04-29 16:37:59 +02002293 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Gilles Peskineb3f4e5b2021-12-13 12:33:18 +01002294 status = PSA_ERROR_INVALID_SIGNATURE;
2295
2296exit:
2297 mbedtls_platform_zeroize( actual_hash, sizeof( actual_hash ) );
2298 return( status );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002299}
2300
Gilles Peskineeb35d782019-01-22 17:56:16 +01002301psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2302 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002303{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002304 if( source_operation->id == 0 ||
2305 target_operation->id != 0 )
2306 {
2307 return( PSA_ERROR_BAD_STATE );
2308 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002309
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002310 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2311 target_operation );
2312 if( status != PSA_SUCCESS )
2313 psa_hash_abort( target_operation );
2314
2315 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002316}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317
2318
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002320/* MAC */
2321/****************************************************************/
2322
Steven Cooreman07897832021-03-19 18:28:56 +01002323psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002324{
Steven Cooreman07897832021-03-19 18:28:56 +01002325 /* Aborting a non-active operation is allowed */
2326 if( operation->id == 0 )
2327 return( PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002328
Steven Cooreman07897832021-03-19 18:28:56 +01002329 psa_status_t status = psa_driver_wrapper_mac_abort( operation );
Steven Cooreman15f0d922021-05-07 14:14:37 +02002330 operation->mac_size = 0;
2331 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002332 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002333
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002334 return( status );
2335}
2336
Ronald Cron1c650a12021-06-17 16:33:22 +02002337static psa_status_t psa_mac_finalize_alg_and_key_validation(
2338 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002339 const psa_key_attributes_t *attributes,
2340 uint8_t *mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002341{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondef68e72021-06-17 16:46:44 +02002343 psa_key_type_t key_type = psa_get_key_type( attributes );
2344 size_t key_bits = psa_get_key_bits( attributes );
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002345
Ronald Cron48f875e2021-06-17 16:10:24 +02002346 if( ! PSA_ALG_IS_MAC( alg ) )
Ronald Crondef68e72021-06-17 16:46:44 +02002347 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman07897832021-03-19 18:28:56 +01002348
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002349 /* Validate the combination of key type and algorithm */
Ronald Crondef68e72021-06-17 16:46:44 +02002350 status = psa_mac_key_can_do( alg, key_type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002351 if( status != PSA_SUCCESS )
Ronald Crondef68e72021-06-17 16:46:44 +02002352 return( status );
Steven Cooreman15472f82021-03-02 16:16:22 +01002353
Steven Cooremana6474de2021-05-10 11:13:41 +02002354 /* Get the output length for the algorithm and key combination */
Ronald Crondef68e72021-06-17 16:46:44 +02002355 *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002356
Ronald Crondef68e72021-06-17 16:46:44 +02002357 if( *mac_size < 4 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002358 {
2359 /* A very short MAC is too short for security since it can be
2360 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2361 * so we make this our minimum, even though 32 bits is still
2362 * too small for security. */
Ronald Crondef68e72021-06-17 16:46:44 +02002363 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman15472f82021-03-02 16:16:22 +01002364 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002365
Ronald Crondef68e72021-06-17 16:46:44 +02002366 if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits,
2367 PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002368 {
2369 /* It's impossible to "truncate" to a larger length than the full length
2370 * of the algorithm. */
Ronald Crondef68e72021-06-17 16:46:44 +02002371 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002372 }
2373
Ronald Crondef68e72021-06-17 16:46:44 +02002374 return( PSA_SUCCESS );
Ronald Cron1c650a12021-06-17 16:33:22 +02002375}
2376
2377static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
2378 mbedtls_svc_key_id_t key,
2379 psa_algorithm_t alg,
2380 int is_sign )
2381{
2382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2383 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002384 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002385
2386 /* A context must be freshly initialized before it can be set up. */
2387 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002388 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002389 status = PSA_ERROR_BAD_STATE;
2390 goto exit;
2391 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002392
2393 status = psa_get_and_lock_key_slot_with_policy(
2394 key,
2395 &slot,
Mateusz Starzykdd55b252021-08-17 15:24:32 +02002396 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Cron1c650a12021-06-17 16:33:22 +02002397 alg );
2398 if( status != PSA_SUCCESS )
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002399 goto exit;
Ronald Cron1c650a12021-06-17 16:33:22 +02002400
2401 psa_key_attributes_t attributes = {
2402 .core = slot->attr
2403 };
2404
Ronald Crondef68e72021-06-17 16:46:44 +02002405 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2406 &operation->mac_size );
Ronald Cron1c650a12021-06-17 16:33:22 +02002407 if( status != PSA_SUCCESS )
2408 goto exit;
2409
2410 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002411 /* Dispatch the MAC setup call with validated input */
2412 if( is_sign )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002413 {
Steven Cooreman07897832021-03-19 18:28:56 +01002414 status = psa_driver_wrapper_mac_sign_setup( operation,
Ronald Cron1c650a12021-06-17 16:33:22 +02002415 &attributes,
Steven Cooreman07897832021-03-19 18:28:56 +01002416 slot->key.data,
2417 slot->key.bytes,
2418 alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002419 }
2420 else
Gilles Peskinefbfac682018-07-08 20:51:54 +02002421 {
Steven Cooreman07897832021-03-19 18:28:56 +01002422 status = psa_driver_wrapper_mac_verify_setup( operation,
Ronald Cron1c650a12021-06-17 16:33:22 +02002423 &attributes,
Steven Cooreman07897832021-03-19 18:28:56 +01002424 slot->key.data,
2425 slot->key.bytes,
2426 alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002427 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002428
Gilles Peskinefbfac682018-07-08 20:51:54 +02002429exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002430 if( status != PSA_SUCCESS )
Steven Cooreman07897832021-03-19 18:28:56 +01002431 psa_mac_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002432
Ronald Cron5c522922020-11-14 16:35:34 +01002433 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002434
Ronald Cron5c522922020-11-14 16:35:34 +01002435 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002436}
2437
Gilles Peskine89167cb2018-07-08 20:12:23 +02002438psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002439 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002440 psa_algorithm_t alg )
2441{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002442 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002443}
2444
2445psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002446 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002447 psa_algorithm_t alg )
2448{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002449 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002450}
2451
Steven Cooreman11743f92021-03-19 18:38:46 +01002452psa_status_t psa_mac_update( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002453 const uint8_t *input,
2454 size_t input_length )
2455{
Steven Cooreman11743f92021-03-19 18:38:46 +01002456 if( operation->id == 0 )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002457 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002458
Steven Cooreman11743f92021-03-19 18:38:46 +01002459 /* Don't require hash implementations to behave correctly on a
2460 * zero-length input, which may have an invalid pointer. */
2461 if( input_length == 0 )
2462 return( PSA_SUCCESS );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002463
Steven Cooreman11743f92021-03-19 18:38:46 +01002464 psa_status_t status = psa_driver_wrapper_mac_update( operation,
2465 input, input_length );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002466 if( status != PSA_SUCCESS )
Steven Cooreman11743f92021-03-19 18:38:46 +01002467 psa_mac_abort( operation );
2468
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002469 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002470}
2471
Steven Cooreman87885df2021-03-19 19:04:39 +01002472psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002473 uint8_t *mac,
2474 size_t mac_size,
2475 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002476{
Steven Cooreman87885df2021-03-19 19:04:39 +01002477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2478 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002479
Steven Cooreman87885df2021-03-19 19:04:39 +01002480 if( operation->id == 0 )
Dave Rodgmand73e1b02021-06-24 16:19:08 +01002481 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002482 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002483 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002484 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002485
Steven Cooreman15f0d922021-05-07 14:14:37 +02002486 if( ! operation->is_sign )
Dave Rodgmandb861792021-06-24 16:17:43 +01002487 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002488 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002489 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002490 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002491
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002492 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2493 * once all the error checks are done. */
2494 if( operation->mac_size == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002495 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002496 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002497 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002498 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002499
2500 if( mac_size < operation->mac_size )
Dave Rodgmandb861792021-06-24 16:17:43 +01002501 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002502 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002503 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002504 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002505
Steven Cooreman87885df2021-03-19 19:04:39 +01002506 status = psa_driver_wrapper_mac_sign_finish( operation,
Steven Cooreman15f0d922021-05-07 14:14:37 +02002507 mac, operation->mac_size,
2508 mac_length );
2509
Dave Rodgman478ab542021-06-25 09:09:02 +01002510exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002511 /* In case of success, set the potential excess room in the output buffer
2512 * to an invalid value, to avoid potentially leaking a longer MAC.
2513 * In case of error, set the output length and content to a safe default,
2514 * such that in case the caller misses an error check, the output would be
2515 * an unachievable MAC.
2516 */
2517 if( status != PSA_SUCCESS )
Steven Cooreman15f0d922021-05-07 14:14:37 +02002518 {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002519 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002520 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002521 }
mohammad16036df908f2018-04-02 08:34:15 -07002522
Ronald Cron882eb782021-06-18 13:05:48 +02002523 if( mac_size > operation->mac_size )
2524 memset( &mac[operation->mac_size], '!',
2525 mac_size - operation->mac_size );
2526
Steven Cooreman87885df2021-03-19 19:04:39 +01002527 abort_status = psa_mac_abort( operation );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002528
Steven Cooreman87885df2021-03-19 19:04:39 +01002529 return( status == PSA_SUCCESS ? abort_status : status );
mohammad16036df908f2018-04-02 08:34:15 -07002530}
2531
Steven Cooreman87885df2021-03-19 19:04:39 +01002532psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002533 const uint8_t *mac,
2534 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535{
Steven Cooreman87885df2021-03-19 19:04:39 +01002536 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2537 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002538
Steven Cooreman87885df2021-03-19 19:04:39 +01002539 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002540 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002541 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002542 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002543 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002544
Steven Cooreman15f0d922021-05-07 14:14:37 +02002545 if( operation->is_sign )
Dave Rodgmandb861792021-06-24 16:17:43 +01002546 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002547 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002548 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002549 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002550
2551 if( operation->mac_size != mac_length )
2552 {
2553 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002554 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002555 }
2556
Steven Cooreman87885df2021-03-19 19:04:39 +01002557 status = psa_driver_wrapper_mac_verify_finish( operation,
2558 mac, mac_length );
Steven Cooreman15f0d922021-05-07 14:14:37 +02002559
Dave Rodgman478ab542021-06-25 09:09:02 +01002560exit:
Steven Cooreman87885df2021-03-19 19:04:39 +01002561 abort_status = psa_mac_abort( operation );
mohammad16036df908f2018-04-02 08:34:15 -07002562
Steven Cooreman87885df2021-03-19 19:04:39 +01002563 return( status == PSA_SUCCESS ? abort_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564}
2565
Ronald Cronef0d8f12021-06-18 14:23:33 +02002566static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key,
2567 psa_algorithm_t alg,
2568 const uint8_t *input,
2569 size_t input_length,
2570 uint8_t *mac,
2571 size_t mac_size,
2572 size_t *mac_length,
2573 int is_sign )
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002574{
2575 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002576 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2577 psa_key_slot_t *slot;
2578 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579
Ronald Crondbb86462021-06-17 17:17:20 +02002580 status = psa_get_and_lock_key_slot_with_policy(
Mateusz Starzykdd55b252021-08-17 15:24:32 +02002581 key,
2582 &slot,
2583 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Cronef0d8f12021-06-18 14:23:33 +02002584 alg );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002585 if( status != PSA_SUCCESS )
2586 goto exit;
2587
Ronald Crondbb86462021-06-17 17:17:20 +02002588 psa_key_attributes_t attributes = {
2589 .core = slot->attr
2590 };
2591
2592 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2593 &operation_mac_size );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002594 if( status != PSA_SUCCESS )
2595 goto exit;
2596
Ronald Crondbb86462021-06-17 17:17:20 +02002597 if( mac_size < operation_mac_size )
2598 {
2599 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002600 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002601 }
2602
2603 status = psa_driver_wrapper_mac_compute(
2604 &attributes,
2605 slot->key.data, slot->key.bytes,
2606 alg,
2607 input, input_length,
2608 mac, operation_mac_size, mac_length );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002609
2610exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002611 /* In case of success, set the potential excess room in the output buffer
2612 * to an invalid value, to avoid potentially leaking a longer MAC.
2613 * In case of error, set the output length and content to a safe default,
2614 * such that in case the caller misses an error check, the output would be
2615 * an unachievable MAC.
2616 */
2617 if( status != PSA_SUCCESS )
Ronald Crondbb86462021-06-17 17:17:20 +02002618 {
Ronald Crondbb86462021-06-17 17:17:20 +02002619 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002620 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002621 }
Ronald Cron882eb782021-06-18 13:05:48 +02002622 if( mac_size > operation_mac_size )
2623 memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002624
Ronald Crondbb86462021-06-17 17:17:20 +02002625 unlock_status = psa_unlock_key_slot( slot );
2626
2627 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002628}
2629
Ronald Cronef0d8f12021-06-18 14:23:33 +02002630psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key,
2631 psa_algorithm_t alg,
2632 const uint8_t *input,
2633 size_t input_length,
2634 uint8_t *mac,
2635 size_t mac_size,
2636 size_t *mac_length)
2637{
2638 return( psa_mac_compute_internal( key, alg,
2639 input, input_length,
2640 mac, mac_size, mac_length, 1 ) );
2641}
2642
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002643psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key,
2644 psa_algorithm_t alg,
2645 const uint8_t *input,
2646 size_t input_length,
2647 const uint8_t *mac,
2648 size_t mac_length)
2649{
2650 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002651 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2652 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002653
Ronald Cron4ed83682021-06-18 14:51:29 +02002654 status = psa_mac_compute_internal( key, alg,
2655 input, input_length,
2656 actual_mac, sizeof( actual_mac ),
2657 &actual_mac_length, 0 );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002658 if( status != PSA_SUCCESS )
2659 goto exit;
2660
Ronald Cron4ed83682021-06-18 14:51:29 +02002661 if( mac_length != actual_mac_length )
2662 {
2663 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002664 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002665 }
2666 if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
2667 {
2668 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002669 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002670 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002671
2672exit:
Ronald Cron4ed83682021-06-18 14:51:29 +02002673 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002674
2675 return ( status );
2676}
Gilles Peskine20035e32018-02-03 22:44:14 +01002677
Gilles Peskine20035e32018-02-03 22:44:14 +01002678/****************************************************************/
2679/* Asymmetric cryptography */
2680/****************************************************************/
2681
gabor-mezei-armc9795782021-05-12 11:03:09 +02002682static psa_status_t psa_sign_verify_check_alg( int input_is_message,
gabor-mezei-arm56980482021-05-05 13:56:27 +02002683 psa_algorithm_t alg )
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002684{
gabor-mezei-armc9795782021-05-12 11:03:09 +02002685 if( input_is_message )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002686 {
2687 if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) )
2688 return( PSA_ERROR_INVALID_ARGUMENT );
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002689
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002690 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002691 {
2692 if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) )
2693 return( PSA_ERROR_INVALID_ARGUMENT );
2694 }
2695 }
2696 else
2697 {
Mateusz Starzykd22362c2021-08-26 11:46:14 +02002698 if( ! PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002699 return( PSA_ERROR_INVALID_ARGUMENT );
2700 }
2701
2702 return( PSA_SUCCESS );
2703}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002704
2705static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002706 int input_is_message,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002707 psa_algorithm_t alg,
2708 const uint8_t * input,
2709 size_t input_length,
2710 uint8_t * signature,
2711 size_t signature_size,
2712 size_t * signature_length )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002713{
2714 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2715 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2716 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002717
2718 *signature_length = 0;
2719
gabor-mezei-armc9795782021-05-12 11:03:09 +02002720 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm56980482021-05-05 13:56:27 +02002721 if( status != PSA_SUCCESS )
2722 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002723
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002724 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002725 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002726 * buffer can in principle be empty since it doesn't actually have
2727 * to be a hash.) */
2728 if( signature_size == 0 )
2729 return( PSA_ERROR_BUFFER_TOO_SMALL );
2730
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002731 status = psa_get_and_lock_key_slot_with_policy(
2732 key, &slot,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002733 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2734 PSA_KEY_USAGE_SIGN_HASH,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002735 alg );
2736
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002737 if( status != PSA_SUCCESS )
2738 goto exit;
2739
2740 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
2741 {
2742 status = PSA_ERROR_INVALID_ARGUMENT;
2743 goto exit;
2744 }
2745
2746 psa_key_attributes_t attributes = {
2747 .core = slot->attr
2748 };
2749
gabor-mezei-armc9795782021-05-12 11:03:09 +02002750 if( input_is_message )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002751 {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002752 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002753 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002754 alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002755 signature, signature_size, signature_length );
2756 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002757 else
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002758 {
2759
2760 status = psa_driver_wrapper_sign_hash(
2761 &attributes, slot->key.data, slot->key.bytes,
2762 alg, input, input_length,
2763 signature, signature_size, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002764 }
2765
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002766
2767exit:
2768 /* Fill the unused part of the output buffer (the whole buffer on error,
2769 * the trailing part on success) with something that isn't a valid signature
2770 * (barring an attack on the signature and deliberately-crafted input),
2771 * in case the caller doesn't check the return status properly. */
2772 if( status == PSA_SUCCESS )
2773 memset( signature + *signature_length, '!',
2774 signature_size - *signature_length );
2775 else
2776 memset( signature, '!', signature_size );
2777 /* If signature_size is 0 then we have nothing to do. We must not call
2778 * memset because signature may be NULL in this case. */
2779
2780 unlock_status = psa_unlock_key_slot( slot );
2781
2782 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
2783}
2784
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002785static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002786 int input_is_message,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002787 psa_algorithm_t alg,
2788 const uint8_t * input,
2789 size_t input_length,
2790 const uint8_t * signature,
2791 size_t signature_length )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002792{
2793 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2794 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2795 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002796
gabor-mezei-armc9795782021-05-12 11:03:09 +02002797 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm56980482021-05-05 13:56:27 +02002798 if( status != PSA_SUCCESS )
2799 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002800
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002801 status = psa_get_and_lock_key_slot_with_policy(
2802 key, &slot,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002803 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2804 PSA_KEY_USAGE_VERIFY_HASH,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002805 alg );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002806
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002807 if( status != PSA_SUCCESS )
2808 return( status );
2809
2810 psa_key_attributes_t attributes = {
2811 .core = slot->attr
2812 };
2813
gabor-mezei-armc9795782021-05-12 11:03:09 +02002814 if( input_is_message )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002815 {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002816 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002817 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002818 alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002819 signature, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002820 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002821 else
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002822 {
2823 status = psa_driver_wrapper_verify_hash(
2824 &attributes, slot->key.data, slot->key.bytes,
2825 alg, input, input_length,
2826 signature, signature_length );
2827 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002828
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002829 unlock_status = psa_unlock_key_slot( slot );
2830
2831 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002832
2833}
2834
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002835psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002836 const psa_key_attributes_t *attributes,
2837 const uint8_t *key_buffer,
2838 size_t key_buffer_size,
2839 psa_algorithm_t alg,
2840 const uint8_t *input,
2841 size_t input_length,
2842 uint8_t *signature,
2843 size_t signature_size,
2844 size_t *signature_length )
2845{
2846 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002847
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002848 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002849 {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002850 size_t hash_length;
2851 uint8_t hash[PSA_HASH_MAX_SIZE];
2852
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002853 status = psa_driver_wrapper_hash_compute(
2854 PSA_ALG_SIGN_GET_HASH( alg ),
2855 input, input_length,
2856 hash, sizeof( hash ), &hash_length );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002857
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002858 if( status != PSA_SUCCESS )
2859 return status;
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002860
2861 return psa_driver_wrapper_sign_hash(
2862 attributes, key_buffer, key_buffer_size,
2863 alg, hash, hash_length,
2864 signature, signature_size, signature_length );
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002865 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002866
gabor-mezei-arm56980482021-05-05 13:56:27 +02002867 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002868}
2869
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002870psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2871 psa_algorithm_t alg,
2872 const uint8_t * input,
2873 size_t input_length,
2874 uint8_t * signature,
2875 size_t signature_size,
2876 size_t * signature_length )
2877{
2878 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002879 key, 1, alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002880 signature, signature_size, signature_length );
2881}
2882
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002883psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002884 const psa_key_attributes_t *attributes,
2885 const uint8_t *key_buffer,
2886 size_t key_buffer_size,
2887 psa_algorithm_t alg,
2888 const uint8_t *input,
2889 size_t input_length,
2890 const uint8_t *signature,
2891 size_t signature_length )
2892{
2893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002894
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002895 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002896 {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002897 size_t hash_length;
2898 uint8_t hash[PSA_HASH_MAX_SIZE];
2899
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002900 status = psa_driver_wrapper_hash_compute(
2901 PSA_ALG_SIGN_GET_HASH( alg ),
2902 input, input_length,
2903 hash, sizeof( hash ), &hash_length );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002904
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002905 if( status != PSA_SUCCESS )
2906 return status;
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002907
2908 return psa_driver_wrapper_verify_hash(
2909 attributes, key_buffer, key_buffer_size,
2910 alg, hash, hash_length,
2911 signature, signature_length );
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002912 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002913
gabor-mezei-armef6f2aa2021-05-05 13:59:01 +02002914 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002915}
2916
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002917psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
2918 psa_algorithm_t alg,
2919 const uint8_t * input,
2920 size_t input_length,
2921 const uint8_t * signature,
2922 size_t signature_length )
2923{
2924 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002925 key, 1, alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002926 signature, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002927}
2928
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002929psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002930 const psa_key_attributes_t *attributes,
2931 const uint8_t *key_buffer, size_t key_buffer_size,
2932 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2933 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002934{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002935 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002936 {
Ronald Cron4501c982021-03-19 20:09:32 +01002937 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2938 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002939 {
Ronald Cron4501c982021-03-19 20:09:32 +01002940#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2941 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2942 return( mbedtls_psa_rsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002943 attributes,
2944 key_buffer, key_buffer_size,
2945 alg, hash, hash_length,
2946 signature, signature_size, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002947#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2948 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002949 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002950 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002951 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002952 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002953 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002954 }
Ronald Cron4bcccc62021-04-09 15:32:03 +02002955 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Ronald Cron4501c982021-03-19 20:09:32 +01002956 {
2957 if( PSA_ALG_IS_ECDSA( alg ) )
2958 {
2959#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2960 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2961 return( mbedtls_psa_ecdsa_sign_hash(
2962 attributes,
2963 key_buffer, key_buffer_size,
2964 alg, hash, hash_length,
2965 signature, signature_size, signature_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002966#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2967 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron4501c982021-03-19 20:09:32 +01002968 }
2969 else
2970 {
2971 return( PSA_ERROR_INVALID_ARGUMENT );
2972 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002973 }
Ronald Cron4501c982021-03-19 20:09:32 +01002974
2975 (void)key_buffer;
2976 (void)key_buffer_size;
2977 (void)hash;
2978 (void)hash_length;
2979 (void)signature;
2980 (void)signature_size;
2981 (void)signature_length;
2982
2983 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002984}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002985
2986psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
2987 psa_algorithm_t alg,
2988 const uint8_t *hash,
2989 size_t hash_length,
2990 uint8_t *signature,
2991 size_t signature_size,
2992 size_t *signature_length )
2993{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002994 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002995 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01002996 signature, signature_size, signature_length );
itayzafrir5c753392018-05-08 11:18:38 +03002997}
2998
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002999psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003000 const psa_key_attributes_t *attributes,
3001 const uint8_t *key_buffer, size_t key_buffer_size,
3002 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
3003 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003004{
Ronald Cron99b8ed72021-02-17 10:33:32 +01003005 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003006 {
Ronald Cron4501c982021-03-19 20:09:32 +01003007 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
3008 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003009 {
Ronald Cron4501c982021-03-19 20:09:32 +01003010#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3011 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3012 return( mbedtls_psa_rsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01003013 attributes,
3014 key_buffer, key_buffer_size,
3015 alg, hash, hash_length,
3016 signature, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01003017#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3018 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02003019 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003020 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003021 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003022 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003023 }
itayzafrir5c753392018-05-08 11:18:38 +03003024 }
Ronald Cron4bcccc62021-04-09 15:32:03 +02003025 else if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003026 {
Ronald Cron4501c982021-03-19 20:09:32 +01003027 if( PSA_ALG_IS_ECDSA( alg ) )
3028 {
3029#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3030 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3031 return( mbedtls_psa_ecdsa_verify_hash(
3032 attributes,
3033 key_buffer, key_buffer_size,
3034 alg, hash, hash_length,
3035 signature, signature_length ) );
3036#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3037 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3038 }
3039 else
3040 {
3041 return( PSA_ERROR_INVALID_ARGUMENT );
3042 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003043 }
Ronald Cron4501c982021-03-19 20:09:32 +01003044
3045 (void)key_buffer;
3046 (void)key_buffer_size;
3047 (void)hash;
3048 (void)hash_length;
3049 (void)signature;
3050 (void)signature_length;
3051
3052 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01003053}
3054
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003055psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3056 psa_algorithm_t alg,
3057 const uint8_t *hash,
3058 size_t hash_length,
3059 const uint8_t *signature,
3060 size_t signature_length )
3061{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003062 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003063 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003064 signature, signature_length );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003065}
3066
John Durkop0e005192020-10-31 22:06:54 -07003067#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003068static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3069 mbedtls_rsa_context *rsa )
3070{
3071 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3072 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3073 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3074 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3075}
John Durkop0e005192020-10-31 22:06:54 -07003076#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003077
Ronald Croncf56a0a2020-08-04 09:51:30 +02003078psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003079 psa_algorithm_t alg,
3080 const uint8_t *input,
3081 size_t input_length,
3082 const uint8_t *salt,
3083 size_t salt_length,
3084 uint8_t *output,
3085 size_t output_size,
3086 size_t *output_length )
3087{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003088 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003089 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003090 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003091
Darryl Green5cc689a2018-07-24 15:34:10 +01003092 (void) input;
3093 (void) input_length;
3094 (void) salt;
3095 (void) output;
3096 (void) output_size;
3097
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003098 *output_length = 0;
3099
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003100 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3101 return( PSA_ERROR_INVALID_ARGUMENT );
3102
Ronald Cron5c522922020-11-14 16:35:34 +01003103 status = psa_get_and_lock_transparent_key_slot_with_policy(
3104 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003105 if( status != PSA_SUCCESS )
3106 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003107 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3108 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003109 {
3110 status = PSA_ERROR_INVALID_ARGUMENT;
3111 goto exit;
3112 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003113
Gilles Peskine8e338702019-07-30 20:06:31 +02003114 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115 {
Ronald Cron7207d572021-09-08 14:28:35 +02003116#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3117 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003118 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003119 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3120 slot->key.data,
3121 slot->key.bytes,
3122 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003123 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003124 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003125
3126 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003127 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003128 status = PSA_ERROR_BUFFER_TOO_SMALL;
3129 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003130 }
Ronald Cron7207d572021-09-08 14:28:35 +02003131#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3132 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003133 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
3134 {
Ronald Cron7207d572021-09-08 14:28:35 +02003135#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003136 status = mbedtls_to_psa_error(
3137 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003138 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003139 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003140 MBEDTLS_RSA_PUBLIC,
3141 input_length,
3142 input,
3143 output ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003144#else
3145 status = PSA_ERROR_NOT_SUPPORTED;
3146#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147 }
3148 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003149 if( PSA_ALG_IS_RSA_OAEP( alg ) )
3150 {
Ronald Cron7207d572021-09-08 14:28:35 +02003151#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003152 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003153 status = mbedtls_to_psa_error(
3154 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003155 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003156 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003157 MBEDTLS_RSA_PUBLIC,
3158 salt, salt_length,
3159 input_length,
3160 input,
3161 output ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003162#else
3163 status = PSA_ERROR_NOT_SUPPORTED;
3164#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003165 }
3166 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003167 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003168 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003169 }
Ronald Cron7207d572021-09-08 14:28:35 +02003170#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3171 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003172rsa_exit:
3173 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003174 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003175
Steven Cooremana2371e52020-07-28 14:30:39 +02003176 mbedtls_rsa_free( rsa );
3177 mbedtls_free( rsa );
John Durkop9814fa22020-11-04 12:28:15 -08003178#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003179 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Ronald Cron7207d572021-09-08 14:28:35 +02003180 }
3181 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003183 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003184 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003185
3186exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003187 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003188
Ronald Cron5c522922020-11-14 16:35:34 +01003189 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003190}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191
Ronald Croncf56a0a2020-08-04 09:51:30 +02003192psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003193 psa_algorithm_t alg,
3194 const uint8_t *input,
3195 size_t input_length,
3196 const uint8_t *salt,
3197 size_t salt_length,
3198 uint8_t *output,
3199 size_t output_size,
3200 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003202 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003203 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003204 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003205
Darryl Green5cc689a2018-07-24 15:34:10 +01003206 (void) input;
3207 (void) input_length;
3208 (void) salt;
3209 (void) output;
3210 (void) output_size;
3211
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003212 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003213
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003214 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3215 return( PSA_ERROR_INVALID_ARGUMENT );
3216
Ronald Cron5c522922020-11-14 16:35:34 +01003217 status = psa_get_and_lock_transparent_key_slot_with_policy(
3218 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003219 if( status != PSA_SUCCESS )
3220 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003221 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003222 {
3223 status = PSA_ERROR_INVALID_ARGUMENT;
3224 goto exit;
3225 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226
Gilles Peskine8e338702019-07-30 20:06:31 +02003227 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228 {
Ronald Cron7207d572021-09-08 14:28:35 +02003229#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3230 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003231 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003232 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3233 slot->key.data,
3234 slot->key.bytes,
3235 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003236 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003237 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238
Steven Cooremana2371e52020-07-28 14:30:39 +02003239 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003240 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003241 status = PSA_ERROR_INVALID_ARGUMENT;
3242 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003243 }
Ronald Cron7207d572021-09-08 14:28:35 +02003244#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
Gilles Peskine6afe7892018-05-31 13:16:08 +02003247 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003248 {
Ronald Cron7207d572021-09-08 14:28:35 +02003249#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003250 status = mbedtls_to_psa_error(
3251 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003252 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003253 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003254 MBEDTLS_RSA_PRIVATE,
3255 output_length,
3256 input,
3257 output,
3258 output_size ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003259#else
3260 status = PSA_ERROR_NOT_SUPPORTED;
3261#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 }
3263 else
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003264 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265 {
Ronald Cron7207d572021-09-08 14:28:35 +02003266#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooremana2371e52020-07-28 14:30:39 +02003267 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003268 status = mbedtls_to_psa_error(
3269 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003270 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003271 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003272 MBEDTLS_RSA_PRIVATE,
3273 salt, salt_length,
3274 output_length,
3275 input,
3276 output,
3277 output_size ) );
Ronald Cron7207d572021-09-08 14:28:35 +02003278#else
3279 status = PSA_ERROR_NOT_SUPPORTED;
3280#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281 }
3282 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003284 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003286
Ronald Cron7207d572021-09-08 14:28:35 +02003287#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3288 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Steven Cooreman4fed4552020-08-03 14:46:03 +02003289rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003290 mbedtls_rsa_free( rsa );
3291 mbedtls_free( rsa );
John Durkop6ba40d12020-11-10 08:50:04 -08003292#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3293 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Ronald Cron7207d572021-09-08 14:28:35 +02003294 }
3295 else
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003297 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003298 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003299
3300exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003301 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003302
Ronald Cron5c522922020-11-14 16:35:34 +01003303 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003304}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003305
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003306
3307
mohammad1603503973b2018-03-12 15:59:30 +02003308/****************************************************************/
3309/* Symmetric cryptography */
3310/****************************************************************/
3311
Ronald Cronab99ac22020-10-01 16:38:28 +02003312static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
3313 mbedtls_svc_key_id_t key,
3314 psa_algorithm_t alg,
3315 mbedtls_operation_t cipher_operation )
3316{
3317 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3318 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003319 psa_key_slot_t *slot = NULL;
Ronald Cronab99ac22020-10-01 16:38:28 +02003320 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3321 PSA_KEY_USAGE_ENCRYPT :
3322 PSA_KEY_USAGE_DECRYPT );
3323
3324 /* A context must be freshly initialized before it can be set up. */
Ronald Cron49fafa92021-03-10 08:34:23 +01003325 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01003326 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003327 status = PSA_ERROR_BAD_STATE;
3328 goto exit;
3329 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003330
Ronald Cronab99ac22020-10-01 16:38:28 +02003331 if( ! PSA_ALG_IS_CIPHER( alg ) )
Dave Rodgmandb861792021-06-24 16:17:43 +01003332 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003333 status = PSA_ERROR_INVALID_ARGUMENT;
3334 goto exit;
3335 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003336
Ronald Cronab99ac22020-10-01 16:38:28 +02003337 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
3338 if( status != PSA_SUCCESS )
3339 goto exit;
3340
Ronald Cron49fafa92021-03-10 08:34:23 +01003341 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003342 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003343 * so we only set it (in the driver wrapper) after resources have been
3344 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003345 operation->iv_set = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003346 if( alg == PSA_ALG_ECB_NO_PADDING )
3347 operation->iv_required = 0;
3348 else
3349 operation->iv_required = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003350 operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
Ronald Cronab99ac22020-10-01 16:38:28 +02003351
Ronald Crona4af55f2020-12-14 14:36:06 +01003352 psa_key_attributes_t attributes = {
3353 .core = slot->attr
3354 };
3355
Ronald Cronab99ac22020-10-01 16:38:28 +02003356 /* Try doing the operation through a driver before using software fallback. */
3357 if( cipher_operation == MBEDTLS_ENCRYPT )
Ronald Crona4af55f2020-12-14 14:36:06 +01003358 status = psa_driver_wrapper_cipher_encrypt_setup( operation,
3359 &attributes,
3360 slot->key.data,
3361 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003362 alg );
3363 else
Ronald Crona4af55f2020-12-14 14:36:06 +01003364 status = psa_driver_wrapper_cipher_decrypt_setup( operation,
3365 &attributes,
3366 slot->key.data,
3367 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003368 alg );
3369
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003370exit:
Ronald Cron06aa4422021-03-09 17:32:57 +01003371 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003372 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003373
Ronald Cron5c522922020-11-14 16:35:34 +01003374 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003375
Ronald Cron5c522922020-11-14 16:35:34 +01003376 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003377}
3378
Gilles Peskinefe119512018-07-08 21:39:34 +02003379psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003380 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003381 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003382{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003383 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003384}
3385
Gilles Peskinefe119512018-07-08 21:39:34 +02003386psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003387 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003388 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003389{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003390 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003391}
3392
Gilles Peskinefe119512018-07-08 21:39:34 +02003393psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003394 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003395 size_t iv_size,
3396 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003397{
Ronald Cron6d051732020-10-01 14:10:20 +02003398 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc423acb2021-07-05 12:31:44 +02003399 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3400 size_t default_iv_length;
Ronald Cron5618a392021-03-26 09:52:26 +01003401
Ronald Cron49fafa92021-03-10 08:34:23 +01003402 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003403 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003404 status = PSA_ERROR_BAD_STATE;
3405 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003406 }
3407
Steven Cooreman7df02922020-09-09 15:28:49 +02003408 if( operation->iv_set || ! operation->iv_required )
3409 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003410 status = PSA_ERROR_BAD_STATE;
3411 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003412 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003413
Ronald Cronc423acb2021-07-05 12:31:44 +02003414 default_iv_length = operation->default_iv_length;
3415 if( iv_size < default_iv_length )
Ronald Cron5618a392021-03-26 09:52:26 +01003416 {
3417 status = PSA_ERROR_BUFFER_TOO_SMALL;
3418 goto exit;
3419 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003420
Ronald Cronc423acb2021-07-05 12:31:44 +02003421 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
3422 {
3423 status = PSA_ERROR_GENERIC_ERROR;
3424 goto exit;
3425 }
3426
3427 status = psa_generate_random( local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003428 if( status != PSA_SUCCESS )
3429 goto exit;
3430
3431 status = psa_driver_wrapper_cipher_set_iv( operation,
Ronald Cronc423acb2021-07-05 12:31:44 +02003432 local_iv, default_iv_length );
Ronald Cron5618a392021-03-26 09:52:26 +01003433
3434exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003435 if( status == PSA_SUCCESS )
Ronald Cron5618a392021-03-26 09:52:26 +01003436 {
Ronald Cronc423acb2021-07-05 12:31:44 +02003437 memcpy( iv, local_iv, default_iv_length );
3438 *iv_length = default_iv_length;
Steven Cooreman7df02922020-09-09 15:28:49 +02003439 operation->iv_set = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003440 }
Steven Cooreman7df02922020-09-09 15:28:49 +02003441 else
Ronald Cronc423acb2021-07-05 12:31:44 +02003442 {
3443 *iv_length = 0;
Moran Peker395db872018-05-31 14:07:14 +03003444 psa_cipher_abort( operation );
Ronald Cronc423acb2021-07-05 12:31:44 +02003445 }
Ronald Cron6d051732020-10-01 14:10:20 +02003446
itayzafrir534bd7c2018-08-02 13:56:32 +03003447 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003448}
3449
Gilles Peskinefe119512018-07-08 21:39:34 +02003450psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003451 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003452 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003453{
Ronald Cron6d051732020-10-01 14:10:20 +02003454 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003455
Ronald Cron49fafa92021-03-10 08:34:23 +01003456 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01003457 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003458 status = PSA_ERROR_BAD_STATE;
3459 goto exit;
3460 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003461
Steven Cooreman7df02922020-09-09 15:28:49 +02003462 if( operation->iv_set || ! operation->iv_required )
Dave Rodgmandb861792021-06-24 16:17:43 +01003463 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003464 status = PSA_ERROR_BAD_STATE;
3465 goto exit;
3466 }
Ronald Crona0d68172021-03-26 10:15:08 +01003467
3468 if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
Dave Rodgmandb861792021-06-24 16:17:43 +01003469 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003470 status = PSA_ERROR_INVALID_ARGUMENT;
3471 goto exit;
3472 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003473
Ronald Crondd24c9b2020-12-15 14:10:01 +01003474 status = psa_driver_wrapper_cipher_set_iv( operation,
3475 iv,
3476 iv_length );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003477
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003478exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003479 if( status == PSA_SUCCESS )
3480 operation->iv_set = 1;
3481 else
mohammad1603503973b2018-03-12 15:59:30 +02003482 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003483 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003484}
3485
Gilles Peskinee553c652018-06-04 16:22:46 +02003486psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3487 const uint8_t *input,
3488 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003489 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003490 size_t output_size,
3491 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003492{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003493 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003494
Ronald Cron49fafa92021-03-10 08:34:23 +01003495 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003496 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003497 status = PSA_ERROR_BAD_STATE;
3498 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003499 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003500
Steven Cooreman7df02922020-09-09 15:28:49 +02003501 if( operation->iv_required && ! operation->iv_set )
3502 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003503 status = PSA_ERROR_BAD_STATE;
3504 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003505 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003506
Ronald Crondd24c9b2020-12-15 14:10:01 +01003507 status = psa_driver_wrapper_cipher_update( operation,
3508 input,
3509 input_length,
3510 output,
3511 output_size,
3512 output_length );
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003513
3514exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003515 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003516 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003517
itayzafrir534bd7c2018-08-02 13:56:32 +03003518 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003519}
3520
Gilles Peskinee553c652018-06-04 16:22:46 +02003521psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3522 uint8_t *output,
3523 size_t output_size,
3524 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003525{
David Saadab4ecc272019-02-14 13:48:10 +02003526 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003527
Ronald Cron49fafa92021-03-10 08:34:23 +01003528 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003529 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003530 status = PSA_ERROR_BAD_STATE;
3531 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003532 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003533
Steven Cooreman7df02922020-09-09 15:28:49 +02003534 if( operation->iv_required && ! operation->iv_set )
3535 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003536 status = PSA_ERROR_BAD_STATE;
3537 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003538 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003539
Ronald Crondd24c9b2020-12-15 14:10:01 +01003540 status = psa_driver_wrapper_cipher_finish( operation,
3541 output,
3542 output_size,
3543 output_length );
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003544
3545exit:
Steven Cooremanef8575e2020-09-11 11:44:50 +02003546 if( status == PSA_SUCCESS )
3547 return( psa_cipher_abort( operation ) );
3548 else
3549 {
3550 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003551 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003552
Steven Cooremanef8575e2020-09-11 11:44:50 +02003553 return( status );
3554 }
mohammad1603503973b2018-03-12 15:59:30 +02003555}
3556
Gilles Peskinee553c652018-06-04 16:22:46 +02003557psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3558{
Ronald Cron49fafa92021-03-10 08:34:23 +01003559 if( operation->id == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003560 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003561 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003562 * in use. It's ok to call abort on such an object, and there's
3563 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003564 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003565 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003566
Ronald Crondd24c9b2020-12-15 14:10:01 +01003567 psa_driver_wrapper_cipher_abort( operation );
Gilles Peskinee553c652018-06-04 16:22:46 +02003568
Ronald Cron49fafa92021-03-10 08:34:23 +01003569 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003570 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003571 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003572
Moran Peker395db872018-05-31 14:07:14 +03003573 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003574}
3575
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003576psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key,
3577 psa_algorithm_t alg,
3578 const uint8_t *input,
3579 size_t input_length,
3580 uint8_t *output,
3581 size_t output_size,
3582 size_t *output_length )
3583{
3584 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003585 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron16377072021-07-08 19:00:07 +02003586 psa_key_slot_t *slot = NULL;
Ronald Cron4c224fe2021-07-09 18:44:58 +02003587 uint8_t local_iv[PSA_CIPHER_IV_MAX_SIZE];
3588 size_t default_iv_length = 0;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003589
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003590 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron16377072021-07-08 19:00:07 +02003591 {
3592 status = PSA_ERROR_INVALID_ARGUMENT;
3593 goto exit;
3594 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003595
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003596 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3597 PSA_KEY_USAGE_ENCRYPT,
3598 alg );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003599 if( status != PSA_SUCCESS )
Ronald Cron16377072021-07-08 19:00:07 +02003600 goto exit;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003601
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003602 psa_key_attributes_t attributes = {
3603 .core = slot->attr
3604 };
3605
Ronald Cron4c224fe2021-07-09 18:44:58 +02003606 default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
3607 if( default_iv_length > PSA_CIPHER_IV_MAX_SIZE )
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003608 {
Ronald Cron4c224fe2021-07-09 18:44:58 +02003609 status = PSA_ERROR_GENERIC_ERROR;
3610 goto exit;
3611 }
3612
3613 if( default_iv_length > 0 )
3614 {
3615 if( output_size < default_iv_length )
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003616 {
3617 status = PSA_ERROR_BUFFER_TOO_SMALL;
3618 goto exit;
3619 }
3620
Ronald Cron4c224fe2021-07-09 18:44:58 +02003621 status = psa_generate_random( local_iv, default_iv_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003622 if( status != PSA_SUCCESS )
3623 goto exit;
3624 }
3625
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003626 status = psa_driver_wrapper_cipher_encrypt(
3627 &attributes, slot->key.data, slot->key.bytes,
Ronald Cron4c224fe2021-07-09 18:44:58 +02003628 alg, local_iv, default_iv_length, input, input_length,
3629 output + default_iv_length, output_size - default_iv_length,
3630 output_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003631
3632exit:
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003633 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron16377072021-07-08 19:00:07 +02003634 if( status == PSA_SUCCESS )
3635 status = unlock_status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003636
Ronald Crona8331692021-07-09 09:19:35 +02003637 if( status == PSA_SUCCESS )
Ronald Cron4c224fe2021-07-09 18:44:58 +02003638 {
3639 if( default_iv_length > 0 )
3640 memcpy( output, local_iv, default_iv_length );
3641 *output_length += default_iv_length;
3642 }
Ronald Crona8331692021-07-09 09:19:35 +02003643 else
Ronald Cron16377072021-07-08 19:00:07 +02003644 *output_length = 0;
3645
3646 return( status );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003647}
3648
3649psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key,
3650 psa_algorithm_t alg,
3651 const uint8_t *input,
3652 size_t input_length,
3653 uint8_t *output,
3654 size_t output_size,
3655 size_t *output_length )
3656{
3657 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003658 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron16377072021-07-08 19:00:07 +02003659 psa_key_slot_t *slot = NULL;
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003660
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003661 if( ! PSA_ALG_IS_CIPHER( alg ) )
Ronald Cron16377072021-07-08 19:00:07 +02003662 {
3663 status = PSA_ERROR_INVALID_ARGUMENT;
3664 goto exit;
3665 }
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003666
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003667 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3668 PSA_KEY_USAGE_DECRYPT,
3669 alg );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003670 if( status != PSA_SUCCESS )
Ronald Cron16377072021-07-08 19:00:07 +02003671 goto exit;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003672
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003673 psa_key_attributes_t attributes = {
3674 .core = slot->attr
3675 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003676
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003677 if( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) )
3678 {
3679 status = PSA_ERROR_INVALID_ARGUMENT;
3680 goto exit;
3681 }
3682
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003683 status = psa_driver_wrapper_cipher_decrypt(
3684 &attributes, slot->key.data, slot->key.bytes,
3685 alg, input, input_length,
3686 output, output_size, output_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003687
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003688exit:
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003689 unlock_status = psa_unlock_key_slot( slot );
Ronald Cron16377072021-07-08 19:00:07 +02003690 if( status == PSA_SUCCESS )
3691 status = unlock_status;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003692
Ronald Cron16377072021-07-08 19:00:07 +02003693 if( status != PSA_SUCCESS )
3694 *output_length = 0;
3695
3696 return( status );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003697}
3698
3699
mohammad16035955c982018-04-26 00:53:03 +03003700/****************************************************************/
3701/* AEAD */
3702/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003703
Ronald Croncf56a0a2020-08-04 09:51:30 +02003704psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003705 psa_algorithm_t alg,
3706 const uint8_t *nonce,
3707 size_t nonce_length,
3708 const uint8_t *additional_data,
3709 size_t additional_data_length,
3710 const uint8_t *plaintext,
3711 size_t plaintext_length,
3712 uint8_t *ciphertext,
3713 size_t ciphertext_size,
3714 size_t *ciphertext_length )
3715{
Ronald Cron215633c2021-03-16 17:15:37 +01003716 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3717 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003718
mohammad1603f08a5502018-06-03 15:05:47 +03003719 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003720
Steven Cooremanea7ab132021-03-17 16:28:00 +01003721 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3722 return( PSA_ERROR_NOT_SUPPORTED );
3723
Ronald Cron9a986162021-03-26 12:40:07 +01003724 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003725 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003726 if( status != PSA_SUCCESS )
3727 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003728
Ronald Cron215633c2021-03-16 17:15:37 +01003729 psa_key_attributes_t attributes = {
3730 .core = slot->attr
3731 };
mohammad16035955c982018-04-26 00:53:03 +03003732
Ronald Cronde822812021-03-17 16:08:20 +01003733 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003734 &attributes, slot->key.data, slot->key.bytes,
3735 alg,
3736 nonce, nonce_length,
3737 additional_data, additional_data_length,
3738 plaintext, plaintext_length,
3739 ciphertext, ciphertext_size, ciphertext_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003740
Gilles Peskineedf9a652018-08-17 18:11:56 +02003741 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3742 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003743
Ronald Cron9f310172021-03-16 16:36:37 +01003744 psa_unlock_key_slot( slot );
mohammad16035955c982018-04-26 00:53:03 +03003745
mohammad16035955c982018-04-26 00:53:03 +03003746 return( status );
Gilles Peskineee652a32018-06-01 19:23:52 +02003747}
3748
Ronald Croncf56a0a2020-08-04 09:51:30 +02003749psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003750 psa_algorithm_t alg,
3751 const uint8_t *nonce,
3752 size_t nonce_length,
3753 const uint8_t *additional_data,
3754 size_t additional_data_length,
3755 const uint8_t *ciphertext,
3756 size_t ciphertext_length,
3757 uint8_t *plaintext,
3758 size_t plaintext_size,
3759 size_t *plaintext_length )
3760{
Ronald Cron215633c2021-03-16 17:15:37 +01003761 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3762 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003763
Gilles Peskineee652a32018-06-01 19:23:52 +02003764 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003765
Steven Cooremanea7ab132021-03-17 16:28:00 +01003766 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3767 return( PSA_ERROR_NOT_SUPPORTED );
3768
Ronald Cron9a986162021-03-26 12:40:07 +01003769 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003770 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003771 if( status != PSA_SUCCESS )
3772 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003773
Ronald Cron215633c2021-03-16 17:15:37 +01003774 psa_key_attributes_t attributes = {
3775 .core = slot->attr
3776 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003777
Ronald Cronde822812021-03-17 16:08:20 +01003778 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003779 &attributes, slot->key.data, slot->key.bytes,
3780 alg,
3781 nonce, nonce_length,
3782 additional_data, additional_data_length,
3783 ciphertext, ciphertext_length,
3784 plaintext, plaintext_size, plaintext_length );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003785
Gilles Peskineedf9a652018-08-17 18:11:56 +02003786 if( status != PSA_SUCCESS && plaintext_size != 0 )
3787 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003788
Ronald Cron215633c2021-03-16 17:15:37 +01003789 psa_unlock_key_slot( slot );
3790
Gilles Peskineedf9a652018-08-17 18:11:56 +02003791 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003792}
3793
Gilles Peskinee59236f2018-01-27 23:32:46 +01003794/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003795/* Generators */
3796/****************************************************************/
3797
John Durkop07cc04a2020-11-16 22:08:34 -08003798#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3799 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3800 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3801#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003802#endif /* At least one builtin KDF */
3803
3804#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3805 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3806 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3807static psa_status_t psa_key_derivation_start_hmac(
3808 psa_mac_operation_t *operation,
3809 psa_algorithm_t hash_alg,
3810 const uint8_t *hmac_key,
3811 size_t hmac_key_length )
3812{
3813 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3815 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
3816 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) );
3817 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
3818
Steven Cooreman15f0d922021-05-07 14:14:37 +02003819 operation->is_sign = 1;
3820 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
3821
Steven Cooreman9878a162021-05-06 17:58:36 +02003822 status = psa_driver_wrapper_mac_sign_setup( operation,
3823 &attributes,
3824 hmac_key, hmac_key_length,
3825 PSA_ALG_HMAC( hash_alg ) );
3826
3827 psa_reset_key_attributes( &attributes );
3828 return( status );
3829}
3830#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003831
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003832#define HKDF_STATE_INIT 0 /* no input yet */
3833#define HKDF_STATE_STARTED 1 /* got salt */
3834#define HKDF_STATE_KEYED 2 /* got key */
3835#define HKDF_STATE_OUTPUT 3 /* output started */
3836
Gilles Peskinecbe66502019-05-16 16:59:18 +02003837static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003838 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003839{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003840 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3841 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003842 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003843 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003844}
3845
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003846psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003847{
3848 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003849 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003850 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003851 {
3852 /* The object has (apparently) been initialized but it is not
3853 * in use. It's ok to call abort on such an object, and there's
3854 * nothing to do. */
3855 }
3856 else
John Durkopd0321952020-10-29 21:37:36 -07003857#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003858 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003859 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003860 mbedtls_free( operation->ctx.hkdf.info );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003861 status = psa_mac_abort( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003862 }
John Durkop07cc04a2020-11-16 22:08:34 -08003863 else
3864#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3865#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3866 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3867 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003868 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003869 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003870 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003871 if( operation->ctx.tls12_prf.secret != NULL )
3872 {
3873 mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret,
3874 operation->ctx.tls12_prf.secret_length );
3875 mbedtls_free( operation->ctx.tls12_prf.secret );
3876 }
3877
Janos Follath6a1d2622019-06-11 10:37:28 +01003878 if( operation->ctx.tls12_prf.seed != NULL )
3879 {
3880 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
3881 operation->ctx.tls12_prf.seed_length );
3882 mbedtls_free( operation->ctx.tls12_prf.seed );
3883 }
3884
3885 if( operation->ctx.tls12_prf.label != NULL )
3886 {
3887 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
3888 operation->ctx.tls12_prf.label_length );
3889 mbedtls_free( operation->ctx.tls12_prf.label );
3890 }
3891
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003892 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003893
3894 /* We leave the fields Ai and output_block to be erased safely by the
3895 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003896 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003897 else
John Durkop07cc04a2020-11-16 22:08:34 -08003898#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3899 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003900 {
3901 status = PSA_ERROR_BAD_STATE;
3902 }
Janos Follath083036a2019-06-11 10:22:26 +01003903 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003904 return( status );
3905}
3906
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003907psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003908 size_t *capacity)
3909{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003910 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003911 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003912 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02003913 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003914 }
3915
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003916 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003917 return( PSA_SUCCESS );
3918}
3919
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003920psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003921 size_t capacity )
3922{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003923 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003924 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003925 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003926 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003927 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003928 return( PSA_SUCCESS );
3929}
3930
John Durkopd0321952020-10-29 21:37:36 -07003931#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003932/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003933 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003934static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Steven Cooremanb27e3502021-04-29 19:11:25 +02003935 psa_algorithm_t hash_alg,
3936 uint8_t *output,
3937 size_t output_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003938{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003939 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003940 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003941 psa_status_t status;
3942
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003943 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3944 return( PSA_ERROR_BAD_STATE );
3945 hkdf->state = HKDF_STATE_OUTPUT;
3946
Gilles Peskinebef7f142018-07-12 17:22:21 +02003947 while( output_length != 0 )
3948 {
3949 /* Copy what remains of the current block */
3950 uint8_t n = hash_length - hkdf->offset_in_block;
3951 if( n > output_length )
3952 n = (uint8_t) output_length;
3953 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3954 output += n;
3955 output_length -= n;
3956 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003957 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003958 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003959 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003960 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003961 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003962 * object was corrupted or if this function is called directly
3963 * inside the library. */
3964 if( hkdf->block_number == 0xff )
3965 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003966
3967 /* We need a new block */
3968 ++hkdf->block_number;
3969 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003970
Steven Cooreman9878a162021-05-06 17:58:36 +02003971 status = psa_key_derivation_start_hmac( &hkdf->hmac,
3972 hash_alg,
3973 hkdf->prk,
3974 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003975 if( status != PSA_SUCCESS )
3976 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003977
Gilles Peskinebef7f142018-07-12 17:22:21 +02003978 if( hkdf->block_number != 1 )
3979 {
Steven Cooremanb27e3502021-04-29 19:11:25 +02003980 status = psa_mac_update( &hkdf->hmac,
3981 hkdf->output_block,
3982 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003983 if( status != PSA_SUCCESS )
3984 return( status );
3985 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02003986 status = psa_mac_update( &hkdf->hmac,
3987 hkdf->info,
3988 hkdf->info_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003989 if( status != PSA_SUCCESS )
3990 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003991 status = psa_mac_update( &hkdf->hmac,
3992 &hkdf->block_number, 1 );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003993 if( status != PSA_SUCCESS )
3994 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003995 status = psa_mac_sign_finish( &hkdf->hmac,
3996 hkdf->output_block,
3997 sizeof( hkdf->output_block ),
3998 &hmac_output_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003999 if( status != PSA_SUCCESS )
4000 return( status );
4001 }
4002
4003 return( PSA_SUCCESS );
4004}
John Durkop07cc04a2020-11-16 22:08:34 -08004005#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004006
John Durkop07cc04a2020-11-16 22:08:34 -08004007#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4008 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004009static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4010 psa_tls12_prf_key_derivation_t *tls12_prf,
4011 psa_algorithm_t alg )
4012{
4013 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004014 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004015 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
4016 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01004017 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004018
Janos Follath7742fee2019-06-17 12:58:10 +01004019 /* We can't be wanting more output after block 0xff, otherwise
4020 * the capacity check in psa_key_derivation_output_bytes() would have
4021 * prevented this call. It could happen only if the operation
4022 * object was corrupted or if this function is called directly
4023 * inside the library. */
4024 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004025 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004026
4027 /* We need a new block */
4028 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004029 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004030
4031 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4032 *
4033 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4034 *
4035 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4036 * HMAC_hash(secret, A(2) + seed) +
4037 * HMAC_hash(secret, A(3) + seed) + ...
4038 *
4039 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004040 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004041 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004042 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004043 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004044 * is currently extracted as `output_block` and where i is
4045 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004046 */
4047
Steven Cooreman9878a162021-05-06 17:58:36 +02004048 status = psa_key_derivation_start_hmac( &hmac,
4049 hash_alg,
4050 tls12_prf->secret,
4051 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004052 if( status != PSA_SUCCESS )
4053 goto cleanup;
4054
4055 /* Calculate A(i) where i = tls12_prf->block_number. */
4056 if( tls12_prf->block_number == 1 )
4057 {
4058 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4059 * the variable seed and in this instance means it in the context of the
4060 * P_hash function, where seed = label + seed.) */
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004061 status = psa_mac_update( &hmac,
4062 tls12_prf->label,
4063 tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004064 if( status != PSA_SUCCESS )
4065 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004066 status = psa_mac_update( &hmac,
4067 tls12_prf->seed,
4068 tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004069 if( status != PSA_SUCCESS )
4070 goto cleanup;
4071 }
4072 else
4073 {
4074 /* A(i) = HMAC_hash(secret, A(i-1)) */
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004075 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004076 if( status != PSA_SUCCESS )
4077 goto cleanup;
4078 }
4079
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004080 status = psa_mac_sign_finish( &hmac,
4081 tls12_prf->Ai, hash_length,
4082 &hmac_output_length );
4083 if( hmac_output_length != hash_length )
4084 status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follathea29bfb2019-06-19 12:21:20 +01004085 if( status != PSA_SUCCESS )
4086 goto cleanup;
4087
4088 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Steven Cooreman9878a162021-05-06 17:58:36 +02004089 status = psa_key_derivation_start_hmac( &hmac,
4090 hash_alg,
4091 tls12_prf->secret,
4092 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004093 if( status != PSA_SUCCESS )
4094 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004095 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004096 if( status != PSA_SUCCESS )
4097 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004098 status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004099 if( status != PSA_SUCCESS )
4100 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004101 status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004102 if( status != PSA_SUCCESS )
4103 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004104 status = psa_mac_sign_finish( &hmac,
4105 tls12_prf->output_block, hash_length,
4106 &hmac_output_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004107 if( status != PSA_SUCCESS )
4108 goto cleanup;
4109
Janos Follath7742fee2019-06-17 12:58:10 +01004110
4111cleanup:
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004112 cleanup_status = psa_mac_abort( &hmac );
Janos Follathea29bfb2019-06-19 12:21:20 +01004113 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4114 status = cleanup_status;
4115
4116 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004117}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004118
Janos Follath844eb0e2019-06-19 12:10:49 +01004119static psa_status_t psa_key_derivation_tls12_prf_read(
4120 psa_tls12_prf_key_derivation_t *tls12_prf,
4121 psa_algorithm_t alg,
4122 uint8_t *output,
4123 size_t output_length )
4124{
4125 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004126 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004127 psa_status_t status;
4128 uint8_t offset, length;
4129
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004130 switch( tls12_prf->state )
4131 {
4132 case PSA_TLS12_PRF_STATE_LABEL_SET:
4133 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4134 break;
4135 case PSA_TLS12_PRF_STATE_OUTPUT:
4136 break;
4137 default:
4138 return( PSA_ERROR_BAD_STATE );
4139 }
4140
Janos Follath844eb0e2019-06-19 12:10:49 +01004141 while( output_length != 0 )
4142 {
4143 /* Check if we have fully processed the current block. */
4144 if( tls12_prf->left_in_block == 0 )
4145 {
4146 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4147 alg );
4148 if( status != PSA_SUCCESS )
4149 return( status );
4150
4151 continue;
4152 }
4153
4154 if( tls12_prf->left_in_block > output_length )
4155 length = (uint8_t) output_length;
4156 else
4157 length = tls12_prf->left_in_block;
4158
4159 offset = hash_length - tls12_prf->left_in_block;
4160 memcpy( output, tls12_prf->output_block + offset, length );
4161 output += length;
4162 output_length -= length;
4163 tls12_prf->left_in_block -= length;
4164 }
4165
4166 return( PSA_SUCCESS );
4167}
John Durkop07cc04a2020-11-16 22:08:34 -08004168#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4169 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004170
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004171psa_status_t psa_key_derivation_output_bytes(
4172 psa_key_derivation_operation_t *operation,
4173 uint8_t *output,
4174 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004175{
4176 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004177 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004178
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004179 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004180 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004181 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004182 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004183 }
4184
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004185 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004186 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004187 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004188 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004189 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004190 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004191 goto exit;
4192 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004194 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004195 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004196 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004197 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4198 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004199 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004200 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004201 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004202 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004203 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004204
John Durkopd0321952020-10-29 21:37:36 -07004205#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004206 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004207 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004208 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004209 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004210 output, output_length );
4211 }
Janos Follathadbec812019-06-14 11:05:39 +01004212 else
John Durkop07cc04a2020-11-16 22:08:34 -08004213#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4214#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4215 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004216 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004217 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004218 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004219 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004220 kdf_alg, output,
4221 output_length );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004222 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004223 else
John Durkop07cc04a2020-11-16 22:08:34 -08004224#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4225 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004226 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004227 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004228 return( PSA_ERROR_BAD_STATE );
4229 }
4230
4231exit:
4232 if( status != PSA_SUCCESS )
4233 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004234 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004235 * This allows us to differentiate between exhausted operations and
4236 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4237 * operations. */
4238 psa_algorithm_t alg = operation->alg;
4239 psa_key_derivation_abort( operation );
4240 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004241 memset( output, '!', output_length );
4242 }
4243 return( status );
4244}
4245
David Brown7807bf72021-02-09 16:28:23 -07004246#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004247static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4248{
4249 if( data_size >= 8 )
4250 mbedtls_des_key_set_parity( data );
4251 if( data_size >= 16 )
4252 mbedtls_des_key_set_parity( data + 8 );
4253 if( data_size >= 24 )
4254 mbedtls_des_key_set_parity( data + 16 );
4255}
David Brown7807bf72021-02-09 16:28:23 -07004256#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004257
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004258static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004259 psa_key_slot_t *slot,
4260 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004261 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004262{
4263 uint8_t *data = NULL;
4264 size_t bytes = PSA_BITS_TO_BYTES( bits );
4265 psa_status_t status;
4266
Gilles Peskine8e338702019-07-30 20:06:31 +02004267 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004268 return( PSA_ERROR_INVALID_ARGUMENT );
4269 if( bits % 8 != 0 )
4270 return( PSA_ERROR_INVALID_ARGUMENT );
4271 data = mbedtls_calloc( 1, bytes );
4272 if( data == NULL )
4273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4274
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004275 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004276 if( status != PSA_SUCCESS )
4277 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004278#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004279 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004280 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07004281#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004282
4283 status = psa_allocate_buffer_to_slot( slot, bytes );
4284 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00004285 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01004286
Ronald Cronbf33c932020-11-28 18:06:53 +01004287 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004288 psa_key_attributes_t attributes = {
4289 .core = slot->attr
4290 };
4291
Ronald Cronbf33c932020-11-28 18:06:53 +01004292 status = psa_driver_wrapper_import_key( &attributes,
4293 data, bytes,
4294 slot->key.data,
4295 slot->key.bytes,
4296 &slot->key.bytes, &bits );
4297 if( bits != slot->attr.bits )
4298 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004299
4300exit:
4301 mbedtls_free( data );
4302 return( status );
4303}
4304
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004305psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004306 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004307 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004308{
4309 psa_status_t status;
4310 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004311 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004312
Ronald Cron81709fc2020-11-14 12:10:32 +01004313 *key = MBEDTLS_SVC_KEY_ID_INIT;
4314
Gilles Peskine0f84d622019-09-12 19:03:13 +02004315 /* Reject any attempt to create a zero-length key so that we don't
4316 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4317 if( psa_get_key_bits( attributes ) == 0 )
4318 return( PSA_ERROR_INVALID_ARGUMENT );
4319
Dave Rodgman021e7242021-11-16 10:32:48 +00004320 if( operation->alg == PSA_ALG_NONE )
4321 return( PSA_ERROR_BAD_STATE );
4322
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004323 if( ! operation->can_output_key )
4324 return( PSA_ERROR_NOT_PERMITTED );
4325
Ronald Cron81709fc2020-11-14 12:10:32 +01004326 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
4327 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004328#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4329 if( driver != NULL )
4330 {
4331 /* Deriving a key in a secure element is not implemented yet. */
4332 status = PSA_ERROR_NOT_SUPPORTED;
4333 }
4334#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004335 if( status == PSA_SUCCESS )
4336 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004337 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004338 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004339 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004340 }
4341 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01004342 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004343 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004344 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004345
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004346 return( status );
4347}
4348
Gilles Peskineeab56e42018-07-12 17:12:33 +02004349
4350
4351/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004352/* Key derivation */
4353/****************************************************************/
4354
Steven Cooreman932ffb72021-02-15 12:14:32 +01004355#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskinecdacf042021-04-29 21:10:00 +02004356static int is_kdf_alg_supported( psa_algorithm_t kdf_alg )
4357{
4358#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
4359 if( PSA_ALG_IS_HKDF( kdf_alg ) )
4360 return( 1 );
4361#endif
4362#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4363 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
4364 return( 1 );
4365#endif
4366#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4367 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
4368 return( 1 );
4369#endif
4370 return( 0 );
4371}
4372
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004373static psa_status_t psa_hash_try_support( psa_algorithm_t alg )
4374{
4375 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
4376 psa_status_t status = psa_hash_setup( &operation, alg );
4377 psa_hash_abort( &operation );
4378 return( status );
4379}
4380
Gilles Peskine969c5d62019-01-16 15:53:06 +01004381static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004382 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004383 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004384{
Janos Follath5fe19732019-06-20 15:09:30 +01004385 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4386 * initialisers for this union leave some bytes unspecified.) */
4387 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4388
Gilles Peskine969c5d62019-01-16 15:53:06 +01004389 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskinecdacf042021-04-29 21:10:00 +02004390 if( ! is_kdf_alg_supported( kdf_alg ) )
4391 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop07cc04a2020-11-16 22:08:34 -08004392
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004393 /* All currently supported key derivation algorithms are based on a
4394 * hash algorithm. */
Gilles Peskinecdacf042021-04-29 21:10:00 +02004395 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
4396 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
4397 if( hash_size == 0 )
4398 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004399
4400 /* Make sure that hash_alg is a supported hash algorithm. Otherwise
4401 * we might fail later, which is somewhat unfriendly and potentially
4402 * risk-prone. */
4403 psa_status_t status = psa_hash_try_support( hash_alg );
4404 if( status != PSA_SUCCESS )
4405 return( status );
4406
Gilles Peskinecdacf042021-04-29 21:10:00 +02004407 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4408 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
4409 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004410 {
Gilles Peskinecdacf042021-04-29 21:10:00 +02004411 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004412 }
Gilles Peskineea0d95e2021-04-29 21:18:14 +02004413
Gilles Peskinecdacf042021-04-29 21:10:00 +02004414 operation->capacity = 255 * hash_size;
4415 return( PSA_SUCCESS );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004416}
John Durkop07cc04a2020-11-16 22:08:34 -08004417#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004418
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004420 psa_algorithm_t alg )
4421{
4422 psa_status_t status;
4423
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004425 return( PSA_ERROR_BAD_STATE );
4426
Gilles Peskine6843c292019-01-18 16:44:49 +01004427 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4428 return( PSA_ERROR_INVALID_ARGUMENT );
4429 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004430 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004431#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004434#else
4435 return( PSA_ERROR_NOT_SUPPORTED );
4436#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004437 }
4438 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4439 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004440#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004442#else
4443 return( PSA_ERROR_NOT_SUPPORTED );
4444#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004445 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004446 else
4447 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004448
4449 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004450 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004451 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004452}
4453
John Durkopd0321952020-10-29 21:37:36 -07004454#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004455static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004456 psa_algorithm_t hash_alg,
4457 psa_key_derivation_step_t step,
4458 const uint8_t *data,
4459 size_t data_length )
4460{
4461 psa_status_t status;
4462 switch( step )
4463 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004464 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004465 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004466 return( PSA_ERROR_BAD_STATE );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004467 else
4468 {
Steven Cooreman9878a162021-05-06 17:58:36 +02004469 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4470 hash_alg,
4471 data, data_length );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004472 if( status != PSA_SUCCESS )
4473 return( status );
4474 hkdf->state = HKDF_STATE_STARTED;
4475 return( PSA_SUCCESS );
4476 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004477 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004478 /* If no salt was provided, use an empty salt. */
4479 if( hkdf->state == HKDF_STATE_INIT )
4480 {
Steven Cooreman9878a162021-05-06 17:58:36 +02004481 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4482 hash_alg,
4483 NULL, 0 );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004484 if( status != PSA_SUCCESS )
4485 return( status );
4486 hkdf->state = HKDF_STATE_STARTED;
4487 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004488 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004489 return( PSA_ERROR_BAD_STATE );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004490 status = psa_mac_update( &hkdf->hmac,
4491 data, data_length );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004492 if( status != PSA_SUCCESS )
4493 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004494 status = psa_mac_sign_finish( &hkdf->hmac,
4495 hkdf->prk,
4496 sizeof( hkdf->prk ),
4497 &data_length );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004498 if( status != PSA_SUCCESS )
4499 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004500 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004501 hkdf->block_number = 0;
4502 hkdf->state = HKDF_STATE_KEYED;
4503 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004504 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004505 if( hkdf->state == HKDF_STATE_OUTPUT )
4506 return( PSA_ERROR_BAD_STATE );
4507 if( hkdf->info_set )
4508 return( PSA_ERROR_BAD_STATE );
4509 hkdf->info_length = data_length;
4510 if( data_length != 0 )
4511 {
4512 hkdf->info = mbedtls_calloc( 1, data_length );
4513 if( hkdf->info == NULL )
4514 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4515 memcpy( hkdf->info, data, data_length );
4516 }
4517 hkdf->info_set = 1;
4518 return( PSA_SUCCESS );
4519 default:
4520 return( PSA_ERROR_INVALID_ARGUMENT );
4521 }
4522}
John Durkop07cc04a2020-11-16 22:08:34 -08004523#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004524
John Durkop07cc04a2020-11-16 22:08:34 -08004525#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4526 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01004527static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4528 const uint8_t *data,
4529 size_t data_length )
4530{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004531 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01004532 return( PSA_ERROR_BAD_STATE );
4533
Janos Follathd6dce9f2019-07-04 09:11:38 +01004534 if( data_length != 0 )
4535 {
4536 prf->seed = mbedtls_calloc( 1, data_length );
4537 if( prf->seed == NULL )
4538 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004539
Janos Follathd6dce9f2019-07-04 09:11:38 +01004540 memcpy( prf->seed, data, data_length );
4541 prf->seed_length = data_length;
4542 }
Janos Follathf08e2652019-06-13 09:05:41 +01004543
Gilles Peskine43f958b2020-12-13 14:55:14 +01004544 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004545
4546 return( PSA_SUCCESS );
4547}
4548
Janos Follath81550542019-06-13 14:26:34 +01004549static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
Janos Follath81550542019-06-13 14:26:34 +01004550 const uint8_t *data,
4551 size_t data_length )
4552{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004553 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01004554 return( PSA_ERROR_BAD_STATE );
4555
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004556 if( data_length != 0 )
4557 {
4558 prf->secret = mbedtls_calloc( 1, data_length );
4559 if( prf->secret == NULL )
4560 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4561
4562 memcpy( prf->secret, data, data_length );
4563 prf->secret_length = data_length;
4564 }
Janos Follath81550542019-06-13 14:26:34 +01004565
Gilles Peskine43f958b2020-12-13 14:55:14 +01004566 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004567
4568 return( PSA_SUCCESS );
4569}
4570
Janos Follath63028dd2019-06-13 09:15:47 +01004571static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
4572 const uint8_t *data,
4573 size_t data_length )
4574{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004575 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01004576 return( PSA_ERROR_BAD_STATE );
4577
Janos Follathd6dce9f2019-07-04 09:11:38 +01004578 if( data_length != 0 )
4579 {
4580 prf->label = mbedtls_calloc( 1, data_length );
4581 if( prf->label == NULL )
4582 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01004583
Janos Follathd6dce9f2019-07-04 09:11:38 +01004584 memcpy( prf->label, data, data_length );
4585 prf->label_length = data_length;
4586 }
Janos Follath63028dd2019-06-13 09:15:47 +01004587
Gilles Peskine43f958b2020-12-13 14:55:14 +01004588 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004589
4590 return( PSA_SUCCESS );
4591}
4592
Janos Follathb03233e2019-06-11 15:30:30 +01004593static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
Janos Follathb03233e2019-06-11 15:30:30 +01004594 psa_key_derivation_step_t step,
4595 const uint8_t *data,
4596 size_t data_length )
4597{
Janos Follathb03233e2019-06-11 15:30:30 +01004598 switch( step )
4599 {
Janos Follathf08e2652019-06-13 09:05:41 +01004600 case PSA_KEY_DERIVATION_INPUT_SEED:
4601 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01004602 case PSA_KEY_DERIVATION_INPUT_SECRET:
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004603 return( psa_tls12_prf_set_key( prf, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01004604 case PSA_KEY_DERIVATION_INPUT_LABEL:
4605 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01004606 default:
4607 return( PSA_ERROR_INVALID_ARGUMENT );
4608 }
4609}
John Durkop07cc04a2020-11-16 22:08:34 -08004610#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4611 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4612
4613#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4614static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4615 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004616 const uint8_t *data,
4617 size_t data_length )
4618{
4619 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004620 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08004621 uint8_t *cur = pms;
4622
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004623 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08004624 return( PSA_ERROR_INVALID_ARGUMENT );
4625
4626 /* Quoting RFC 4279, Section 2:
4627 *
4628 * The premaster secret is formed as follows: if the PSK is N octets
4629 * long, concatenate a uint16 with the value N, N zero octets, a second
4630 * uint16 with the value N, and the PSK itself.
4631 */
4632
Joe Subbiani2bbafda2021-06-24 13:00:03 +01004633 *cur++ = MBEDTLS_BYTE_1( data_length );
4634 *cur++ = MBEDTLS_BYTE_0( data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08004635 memset( cur, 0, data_length );
4636 cur += data_length;
4637 *cur++ = pms[0];
4638 *cur++ = pms[1];
4639 memcpy( cur, data, data_length );
4640 cur += data_length;
4641
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004642 status = psa_tls12_prf_set_key( prf, pms, cur - pms );
John Durkop07cc04a2020-11-16 22:08:34 -08004643
4644 mbedtls_platform_zeroize( pms, sizeof( pms ) );
4645 return( status );
4646}
Janos Follath6660f0e2019-06-17 08:44:03 +01004647
4648static psa_status_t psa_tls12_prf_psk_to_ms_input(
4649 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004650 psa_key_derivation_step_t step,
4651 const uint8_t *data,
4652 size_t data_length )
4653{
4654 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01004655 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004656 return( psa_tls12_prf_psk_to_ms_set_key( prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004657 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01004658 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004659
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004660 return( psa_tls12_prf_input( prf, step, data, data_length ) );
Janos Follath6660f0e2019-06-17 08:44:03 +01004661}
John Durkop07cc04a2020-11-16 22:08:34 -08004662#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004663
Gilles Peskineb8965192019-09-24 16:21:10 +02004664/** Check whether the given key type is acceptable for the given
4665 * input step of a key derivation.
4666 *
4667 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4668 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4669 * Both secret and non-secret inputs can alternatively have the type
4670 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4671 * that the input was passed as a buffer rather than via a key object.
4672 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004673static int psa_key_derivation_check_input_type(
4674 psa_key_derivation_step_t step,
4675 psa_key_type_t key_type )
4676{
4677 switch( step )
4678 {
4679 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02004680 if( key_type == PSA_KEY_TYPE_DERIVE )
4681 return( PSA_SUCCESS );
4682 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004683 return( PSA_SUCCESS );
4684 break;
4685 case PSA_KEY_DERIVATION_INPUT_LABEL:
4686 case PSA_KEY_DERIVATION_INPUT_SALT:
4687 case PSA_KEY_DERIVATION_INPUT_INFO:
4688 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02004689 if( key_type == PSA_KEY_TYPE_RAW_DATA )
4690 return( PSA_SUCCESS );
4691 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004692 return( PSA_SUCCESS );
4693 break;
4694 }
4695 return( PSA_ERROR_INVALID_ARGUMENT );
4696}
4697
Janos Follathb80a94e2019-06-12 15:54:46 +01004698static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004700 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004701 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004702 const uint8_t *data,
4703 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004704{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004705 psa_status_t status;
4706 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
4707
4708 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02004709 if( status != PSA_SUCCESS )
4710 goto exit;
4711
John Durkopd0321952020-10-29 21:37:36 -07004712#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004713 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004714 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004715 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004716 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004717 step, data, data_length );
4718 }
John Durkop07cc04a2020-11-16 22:08:34 -08004719 else
4720#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4721#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4722 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004723 {
Janos Follathb03233e2019-06-11 15:30:30 +01004724 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
Janos Follathb03233e2019-06-11 15:30:30 +01004725 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01004726 }
John Durkop07cc04a2020-11-16 22:08:34 -08004727 else
4728#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4729#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4730 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01004731 {
4732 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004733 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004734 }
4735 else
John Durkop07cc04a2020-11-16 22:08:34 -08004736#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004737 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004739 (void) data;
4740 (void) data_length;
4741 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004742 return( PSA_ERROR_BAD_STATE );
4743 }
4744
Gilles Peskine224b0d62019-09-23 18:13:17 +02004745exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004746 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004748 return( status );
4749}
4750
Janos Follath51f4a0f2019-06-14 11:35:55 +01004751psa_status_t psa_key_derivation_input_bytes(
4752 psa_key_derivation_operation_t *operation,
4753 psa_key_derivation_step_t step,
4754 const uint8_t *data,
4755 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004756{
Gilles Peskineb8965192019-09-24 16:21:10 +02004757 return( psa_key_derivation_input_internal( operation, step,
4758 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01004759 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004760}
4761
Janos Follath51f4a0f2019-06-14 11:35:55 +01004762psa_status_t psa_key_derivation_input_key(
4763 psa_key_derivation_operation_t *operation,
4764 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004765 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004766{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004767 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004768 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004769 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004770
Ronald Cron5c522922020-11-14 16:35:34 +01004771 status = psa_get_and_lock_transparent_key_slot_with_policy(
4772 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004773 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02004774 {
4775 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004776 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02004777 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004778
4779 /* Passing a key object as a SECRET input unlocks the permission
4780 * to output to a key object. */
4781 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4782 operation->can_output_key = 1;
4783
Ronald Cronf95a2b12020-10-22 15:24:49 +02004784 status = psa_key_derivation_input_internal( operation,
4785 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004786 slot->key.data,
4787 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004788
Ronald Cron5c522922020-11-14 16:35:34 +01004789 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004790
Ronald Cron5c522922020-11-14 16:35:34 +01004791 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004792}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004793
4794
4795
4796/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004797/* Key agreement */
4798/****************************************************************/
4799
John Durkop6ba40d12020-11-10 08:50:04 -08004800#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004801static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4802 size_t peer_key_length,
4803 const mbedtls_ecp_keypair *our_key,
4804 uint8_t *shared_secret,
4805 size_t shared_secret_size,
4806 size_t *shared_secret_length )
4807{
Steven Cooremand4867872020-08-05 16:31:39 +02004808 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004809 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004810 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004811 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01004812 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004813 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004814
Ronald Cron90857082020-11-25 14:58:33 +01004815 status = mbedtls_psa_ecp_load_representation(
4816 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004817 bits,
Ronald Cron90857082020-11-25 14:58:33 +01004818 peer_key,
4819 peer_key_length,
4820 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004821 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004822 goto exit;
4823
Jaeden Amero97271b32019-01-10 19:38:51 +00004824 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02004825 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00004826 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004827 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004828 status = mbedtls_to_psa_error(
4829 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4830 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004831 goto exit;
4832
Jaeden Amero97271b32019-01-10 19:38:51 +00004833 status = mbedtls_to_psa_error(
4834 mbedtls_ecdh_calc_secret( &ecdh,
4835 shared_secret_length,
4836 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004837 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004838 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004839 if( status != PSA_SUCCESS )
4840 goto exit;
4841 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
4842 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004843
4844exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01004845 if( status != PSA_SUCCESS )
4846 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004847 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02004848 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02004849 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02004850
Jaeden Amero97271b32019-01-10 19:38:51 +00004851 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004852}
John Durkop6ba40d12020-11-10 08:50:04 -08004853#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004854
Gilles Peskine01d718c2018-09-18 12:01:02 +02004855#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4856
Gilles Peskine0216fe12019-04-11 21:23:21 +02004857static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4858 psa_key_slot_t *private_key,
4859 const uint8_t *peer_key,
4860 size_t peer_key_length,
4861 uint8_t *shared_secret,
4862 size_t shared_secret_size,
4863 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004864{
Gilles Peskine0216fe12019-04-11 21:23:21 +02004865 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004866 {
John Durkop6ba40d12020-11-10 08:50:04 -08004867#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004868 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02004869 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004870 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02004871 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004872 psa_status_t status = mbedtls_psa_ecp_load_representation(
4873 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004874 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01004875 private_key->key.data,
4876 private_key->key.bytes,
4877 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004878 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004879 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02004880 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02004881 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004882 shared_secret, shared_secret_size,
4883 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004884 mbedtls_ecp_keypair_free( ecp );
4885 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02004886 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08004887#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004888 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004889 (void) private_key;
4890 (void) peer_key;
4891 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004892 (void) shared_secret;
4893 (void) shared_secret_size;
4894 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004895 return( PSA_ERROR_NOT_SUPPORTED );
4896 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004897}
4898
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004899/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004900 * to potentially free embedded data structures and wipe confidential data.
4901 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004902static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004903 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004904 psa_key_slot_t *private_key,
4905 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004906 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004907{
4908 psa_status_t status;
4909 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4910 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004912
4913 /* Step 1: run the secret agreement algorithm to generate the shared
4914 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004915 status = psa_key_agreement_raw_internal( ka_alg,
4916 private_key,
4917 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03004918 shared_secret,
4919 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02004920 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004921 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004922 goto exit;
4923
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004924 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02004925 * the shared secret. A shared secret is permitted wherever a key
4926 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01004927 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004928 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01004929 shared_secret,
4930 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004931exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004932 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004933 return( status );
4934}
4935
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004937 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004938 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004939 const uint8_t *peer_key,
4940 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004941{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004942 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004943 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004944 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004945
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004946 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004947 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01004948 status = psa_get_and_lock_transparent_key_slot_with_policy(
4949 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004950 if( status != PSA_SUCCESS )
4951 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004952 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004953 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004954 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004955 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004957 else
4958 {
4959 /* If a private key has been added as SECRET, we allow the derived
4960 * key material to be used as a key in PSA Crypto. */
4961 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4962 operation->can_output_key = 1;
4963 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004964
Ronald Cron5c522922020-11-14 16:35:34 +01004965 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004966
Ronald Cron5c522922020-11-14 16:35:34 +01004967 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004968}
4969
Gilles Peskinebe697d82019-05-16 18:00:41 +02004970psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004971 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02004972 const uint8_t *peer_key,
4973 size_t peer_key_length,
4974 uint8_t *output,
4975 size_t output_size,
4976 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02004977{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004978 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004979 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004980 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004981
4982 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4983 {
4984 status = PSA_ERROR_INVALID_ARGUMENT;
4985 goto exit;
4986 }
Ronald Cron5c522922020-11-14 16:35:34 +01004987 status = psa_get_and_lock_transparent_key_slot_with_policy(
4988 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004989 if( status != PSA_SUCCESS )
4990 goto exit;
4991
4992 status = psa_key_agreement_raw_internal( alg, slot,
4993 peer_key, peer_key_length,
4994 output, output_size,
4995 output_length );
4996
4997exit:
4998 if( status != PSA_SUCCESS )
4999 {
5000 /* If an error happens and is not handled properly, the output
5001 * may be used as a key to protect sensitive data. Arrange for such
5002 * a key to be random, which is likely to result in decryption or
5003 * verification errors. This is better than filling the buffer with
5004 * some constant data such as zeros, which would result in the data
5005 * being protected with a reproducible, easily knowable key.
5006 */
5007 psa_generate_random( output, output_size );
5008 *output_length = output_size;
5009 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005010
Ronald Cron5c522922020-11-14 16:35:34 +01005011 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005012
Ronald Cron5c522922020-11-14 16:35:34 +01005013 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005014}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005015
5016
Gilles Peskine30524eb2020-11-13 17:02:26 +01005017
Gilles Peskine86a440b2018-11-12 18:39:40 +01005018/****************************************************************/
5019/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005020/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005021
Gilles Peskine30524eb2020-11-13 17:02:26 +01005022/** Initialize the PSA random generator.
5023 */
5024static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5025{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005026#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5027 memset( rng, 0, sizeof( *rng ) );
5028#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5029
Gilles Peskine30524eb2020-11-13 17:02:26 +01005030 /* Set default configuration if
5031 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5032 if( rng->entropy_init == NULL )
5033 rng->entropy_init = mbedtls_entropy_init;
5034 if( rng->entropy_free == NULL )
5035 rng->entropy_free = mbedtls_entropy_free;
5036
5037 rng->entropy_init( &rng->entropy );
5038#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5039 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5040 /* The PSA entropy injection feature depends on using NV seed as an entropy
5041 * source. Add NV seed as an entropy source for PSA entropy injection. */
5042 mbedtls_entropy_add_source( &rng->entropy,
5043 mbedtls_nv_seed_poll, NULL,
5044 MBEDTLS_ENTROPY_BLOCK_SIZE,
5045 MBEDTLS_ENTROPY_SOURCE_STRONG );
5046#endif
5047
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005048 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005049#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005050}
5051
5052/** Deinitialize the PSA random generator.
5053 */
5054static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5055{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005056#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5057 memset( rng, 0, sizeof( *rng ) );
5058#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005059 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005060 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005061#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005062}
5063
5064/** Seed the PSA random generator.
5065 */
5066static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5067{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005068#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5069 /* Do nothing: the external RNG seeds itself. */
5070 (void) rng;
5071 return( PSA_SUCCESS );
5072#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005073 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005074 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5075 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005076 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005077#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005078}
5079
Gilles Peskinee59236f2018-01-27 23:32:46 +01005080psa_status_t psa_generate_random( uint8_t *output,
5081 size_t output_size )
5082{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005083 GUARD_MODULE_INITIALIZED;
5084
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005085#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5086
5087 size_t output_length = 0;
5088 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5089 output, output_size,
5090 &output_length );
5091 if( status != PSA_SUCCESS )
5092 return( status );
5093 /* Breaking up a request into smaller chunks is currently not supported
5094 * for the extrernal RNG interface. */
5095 if( output_length != output_size )
5096 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5097 return( PSA_SUCCESS );
5098
5099#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5100
Gilles Peskine71ddab92021-01-04 21:01:07 +01005101 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005102 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005103 size_t request_size =
5104 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5105 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5106 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005107 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
5108 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02005109 if( ret != 0 )
5110 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01005111 output_size -= request_size;
5112 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005113 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005114 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005115#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005116}
5117
Gilles Peskine8814fc42020-12-14 15:33:44 +01005118/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005119 *
5120 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5121 * `psa_generate_random(...)`. The state parameter is ignored since the
5122 * PSA API doesn't support passing an explicit state.
5123 *
5124 * In the non-external case, psa_generate_random() calls an
5125 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5126 * and semantics as mbedtls_psa_get_random(). As an optimization,
5127 * instead of doing this back-and-forth between the PSA API and the
5128 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5129 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005130 */
5131#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5132int mbedtls_psa_get_random( void *p_rng,
5133 unsigned char *output,
5134 size_t output_size )
5135{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005136 /* This function takes a pointer to the RNG state because that's what
5137 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5138 * its own state internally and doesn't let the caller access that state.
5139 * So we just ignore the state parameter, and in practice we'll pass
5140 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005141 (void) p_rng;
5142 psa_status_t status = psa_generate_random( output, output_size );
5143 if( status == PSA_SUCCESS )
5144 return( 0 );
5145 else
5146 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
5147}
5148#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5149
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005150#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5151#include "mbedtls/entropy_poll.h"
5152
Gilles Peskine7228da22019-07-15 11:06:15 +02005153psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005154 size_t seed_size )
5155{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005156 if( global_data.initialized )
5157 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005158
5159 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5160 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5161 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5162 return( PSA_ERROR_INVALID_ARGUMENT );
5163
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005164 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005165}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005166#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005167
Ronald Cron3772afe2021-02-08 16:10:05 +01005168/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005169 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005170 * \param type The key type
5171 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005172 *
5173 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005174 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005175 * \retval #PSA_ERROR_INVALID_ARGUMENT
5176 * The size in bits of the key is not valid.
5177 * \retval #PSA_ERROR_NOT_SUPPORTED
5178 * The type and/or the size in bits of the key or the combination of
5179 * the two is not supported.
5180 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005181static psa_status_t psa_validate_key_type_and_size_for_key_generation(
5182 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005183{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005185
Gilles Peskinee59236f2018-01-27 23:32:46 +01005186 if( key_type_is_raw_bytes( type ) )
5187 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02005188 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005189 if( status != PSA_SUCCESS )
5190 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005191 }
5192 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005193#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005194 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5195 {
5196 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5197 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005198
Ronald Cron01b2aba2020-10-05 09:42:02 +02005199 /* Accept only byte-aligned keys, for the same reasons as
5200 * in psa_import_rsa_key(). */
5201 if( bits % 8 != 0 )
5202 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005203 }
5204 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005205#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005206
Ronald Cron5c4d3862020-12-07 11:07:24 +01005207#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005208 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5209 {
Ronald Crond81ab562021-02-16 09:01:16 +01005210 /* To avoid empty block, return successfully here. */
5211 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005212 }
5213 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005214#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005215 {
5216 return( PSA_ERROR_NOT_SUPPORTED );
5217 }
5218
5219 return( PSA_SUCCESS );
5220}
5221
Ronald Cron55ed0592020-10-05 10:30:40 +02005222psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005223 const psa_key_attributes_t *attributes,
5224 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005225{
5226 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005227 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005228
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005229 if( ( attributes->domain_parameters == NULL ) &&
5230 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005231 return( PSA_ERROR_INVALID_ARGUMENT );
5232
Ronald Cron01b2aba2020-10-05 09:42:02 +02005233 if( key_type_is_raw_bytes( type ) )
5234 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005235 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005236 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005237 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005238
David Brown12ca5032021-02-11 11:02:00 -07005239#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005240 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005241 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07005242#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005243 }
5244 else
5245
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005246#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5247 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005248 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005249 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01005250 return( mbedtls_psa_rsa_generate_key( attributes,
5251 key_buffer,
5252 key_buffer_size,
5253 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005254 }
5255 else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005256#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5257 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005258
John Durkop9814fa22020-11-04 12:28:15 -08005259#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005260 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005261 {
Ronald Cron7023db52020-11-20 18:17:42 +01005262 return( mbedtls_psa_ecp_generate_key( attributes,
5263 key_buffer,
5264 key_buffer_size,
5265 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005266 }
5267 else
John Durkop9814fa22020-11-04 12:28:15 -08005268#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005269 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005270 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005271 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005272 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005273
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005274 return( PSA_SUCCESS );
5275}
Darryl Green0c6575a2018-11-07 16:05:30 +00005276
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005277psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005278 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005279{
5280 psa_status_t status;
5281 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005282 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005283 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005284
Ronald Cron81709fc2020-11-14 12:10:32 +01005285 *key = MBEDTLS_SVC_KEY_ID_INIT;
5286
Gilles Peskine0f84d622019-09-12 19:03:13 +02005287 /* Reject any attempt to create a zero-length key so that we don't
5288 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5289 if( psa_get_key_bits( attributes ) == 0 )
5290 return( PSA_ERROR_INVALID_ARGUMENT );
5291
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005292 /* Reject any attempt to create a public key. */
5293 if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) )
5294 return( PSA_ERROR_INVALID_ARGUMENT );
5295
Ronald Cron81709fc2020-11-14 12:10:32 +01005296 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
5297 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005298 if( status != PSA_SUCCESS )
5299 goto exit;
5300
Ronald Cron977c2472020-10-13 08:32:21 +02005301 /* In the case of a transparent key or an opaque key stored in local
5302 * storage (thus not in the case of generating a key in a secure element
5303 * or cryptoprocessor with storage), we have to allocate a buffer to
5304 * hold the generated key material. */
5305 if( slot->key.data == NULL )
5306 {
5307 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
5308 PSA_KEY_LOCATION_LOCAL_STORAGE )
5309 {
Ronald Cron3772afe2021-02-08 16:10:05 +01005310 status = psa_validate_key_type_and_size_for_key_generation(
5311 attributes->core.type, attributes->core.bits );
5312 if( status != PSA_SUCCESS )
5313 goto exit;
5314
5315 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
5316 attributes->core.type,
5317 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02005318 }
5319 else
5320 {
5321 status = psa_driver_wrapper_get_key_buffer_size(
5322 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01005323 if( status != PSA_SUCCESS )
5324 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02005325 }
Ronald Cron977c2472020-10-13 08:32:21 +02005326
5327 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
5328 if( status != PSA_SUCCESS )
5329 goto exit;
5330 }
5331
Steven Cooreman2a1664c2020-07-20 15:33:08 +02005332 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02005333 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02005334
Ronald Cron2b56bc82020-10-05 10:02:26 +02005335 if( status != PSA_SUCCESS )
5336 psa_remove_key_data_from_memory( slot );
5337
Gilles Peskine11792082019-08-06 18:36:36 +02005338exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005339 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005340 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005341 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005342 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005343
Darryl Green0c6575a2018-11-07 16:05:30 +00005344 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005345}
5346
Gilles Peskinee59236f2018-01-27 23:32:46 +01005347/****************************************************************/
5348/* Module setup */
5349/****************************************************************/
5350
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005351#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005352psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5353 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5354 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5355{
5356 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5357 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005358 global_data.rng.entropy_init = entropy_init;
5359 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01005360 return( PSA_SUCCESS );
5361}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005362#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005363
Gilles Peskinee59236f2018-01-27 23:32:46 +01005364void mbedtls_psa_crypto_free( void )
5365{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005366 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005367 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5368 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005369 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005370 }
5371 /* Wipe all remaining data, including configuration.
5372 * In particular, this sets all state indicator to the value
5373 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005374 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Ronald Cron088d5d02021-04-10 16:57:30 +02005375
5376 /* Terminate drivers */
5377 psa_driver_wrapper_free( );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005378}
5379
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005380#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5381/** Recover a transaction that was interrupted by a power failure.
5382 *
5383 * This function is called during initialization, before psa_crypto_init()
5384 * returns. If this function returns a failure status, the initialization
5385 * fails.
5386 */
5387static psa_status_t psa_crypto_recover_transaction(
5388 const psa_crypto_transaction_t *transaction )
5389{
5390 switch( transaction->unknown.type )
5391 {
5392 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5393 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005394 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005395 * is implemented.
5396 * https://github.com/ARMmbed/mbed-crypto/issues/218
5397 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005398 default:
5399 /* We found an unsupported transaction in the storage.
5400 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01005401 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005402 }
5403}
5404#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5405
Gilles Peskinee59236f2018-01-27 23:32:46 +01005406psa_status_t psa_crypto_init( void )
5407{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005408 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005409
Gilles Peskinec6b69072018-11-20 21:42:52 +01005410 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005411 if( global_data.initialized != 0 )
5412 return( PSA_SUCCESS );
5413
Gilles Peskine30524eb2020-11-13 17:02:26 +01005414 /* Initialize and seed the random generator. */
5415 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005416 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01005417 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005418 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005419 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005420 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005421
Gilles Peskine66fb1262018-12-10 16:29:04 +01005422 status = psa_initialize_key_slots( );
5423 if( status != PSA_SUCCESS )
5424 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005425
Ronald Cron088d5d02021-04-10 16:57:30 +02005426 /* Init drivers */
5427 status = psa_driver_wrapper_init( );
Gilles Peskined9348f22019-10-01 15:22:29 +02005428 if( status != PSA_SUCCESS )
5429 goto exit;
Gilles Peskined9348f22019-10-01 15:22:29 +02005430
Gilles Peskine4b734222019-07-24 15:56:31 +02005431#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005432 status = psa_crypto_load_transaction( );
5433 if( status == PSA_SUCCESS )
5434 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005435 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5436 if( status != PSA_SUCCESS )
5437 goto exit;
5438 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005439 }
5440 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5441 {
5442 /* There's no transaction to complete. It's all good. */
5443 status = PSA_SUCCESS;
5444 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005445#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005446
Gilles Peskinec6b69072018-11-20 21:42:52 +01005447 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005448 global_data.initialized = 1;
5449
5450exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005451 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005452 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005453 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005454}
5455
5456#endif /* MBEDTLS_PSA_CRYPTO_C */