blob: 2c562ce2a3c791a55d892b3cbefea4b8660b276d [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 Peskine30524eb2020-11-13 17:02:26 +0100111 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100113 unsigned rng_state : 2;
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
Gilles Peskine73167e12019-07-12 23:44:37 +0200388#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
389static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
390{
Gilles Peskine8e338702019-07-30 20:06:31 +0200391 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200392}
393#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
394
John Durkop07cc04a2020-11-16 22:08:34 -0800395/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
396 * current test driver in key_management.c is using this function
397 * when accelerators are used for ECC key pair and public key.
398 * Once that dependency is resolved these guards can be removed.
399 */
John Durkop9814fa22020-11-04 12:28:15 -0800400#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800401 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
402 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
403 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100404mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100405 size_t bits,
406 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200407{
408 switch( curve )
409 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100410 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100413#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100414 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100415 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100416#endif
417#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100418 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100419 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100420#endif
421#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100422 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100423 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100424#endif
425#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100426 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100427 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100428#endif
429#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100430 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100431 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100432 case 528:
433 if( bits_is_sloppy )
434 return( MBEDTLS_ECP_DP_SECP521R1 );
435 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100436#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100437 }
438 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100439
Paul Elliott8ff510a2020-06-02 17:19:28 +0100440 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100441 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100442 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100443#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100444 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100445 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100446#endif
447#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100448 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100449 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100450#endif
451#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100452 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100453 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100454#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100455 }
456 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100457
Paul Elliott8ff510a2020-06-02 17:19:28 +0100458 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100460 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100461#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100462 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100463 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100464 case 256:
465 if( bits_is_sloppy )
466 return( MBEDTLS_ECP_DP_CURVE25519 );
467 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100468#endif
469#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100470 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100471 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100472#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
Paul Elliott8ff510a2020-06-02 17:19:28 +0100476 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100477 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100478 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100479#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100480 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100481 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100482#endif
483#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100484 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100485 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100486#endif
487#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100488 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100489 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100490#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100491 }
492 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200493 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100494
Gilles Peskine71f45ba2021-03-23 14:17:55 +0100495 (void) bits_is_sloppy;
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100496 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200497}
John Durkop9814fa22020-11-04 12:28:15 -0800498#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800499 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
500 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
501 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200502
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200503static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
504 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505{
506 /* Check that the bit size is acceptable for the key type */
507 switch( type )
508 {
509 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200510 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200511 case PSA_KEY_TYPE_DERIVE:
512 break;
Ronald Cron1f0db802021-03-12 09:59:30 +0100513#if defined(PSA_WANT_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200514 case PSA_KEY_TYPE_AES:
515 if( bits != 128 && bits != 192 && bits != 256 )
516 return( PSA_ERROR_INVALID_ARGUMENT );
517 break;
518#endif
Gilles Peskine8890f642021-09-21 11:59:39 +0200519#if defined(PSA_WANT_KEY_TYPE_ARIA)
520 case PSA_KEY_TYPE_ARIA:
521 if( bits != 128 && bits != 192 && bits != 256 )
522 return( PSA_ERROR_INVALID_ARGUMENT );
523 break;
524#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100525#if defined(PSA_WANT_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526 case PSA_KEY_TYPE_CAMELLIA:
527 if( bits != 128 && bits != 192 && bits != 256 )
528 return( PSA_ERROR_INVALID_ARGUMENT );
529 break;
530#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100531#if defined(PSA_WANT_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200532 case PSA_KEY_TYPE_DES:
533 if( bits != 64 && bits != 128 && bits != 192 )
534 return( PSA_ERROR_INVALID_ARGUMENT );
535 break;
536#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100537#if defined(PSA_WANT_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200538 case PSA_KEY_TYPE_ARC4:
539 if( bits < 8 || bits > 2048 )
540 return( PSA_ERROR_INVALID_ARGUMENT );
541 break;
542#endif
Ronald Cron1f0db802021-03-12 09:59:30 +0100543#if defined(PSA_WANT_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200544 case PSA_KEY_TYPE_CHACHA20:
545 if( bits != 256 )
546 return( PSA_ERROR_INVALID_ARGUMENT );
547 break;
548#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200549 default:
550 return( PSA_ERROR_NOT_SUPPORTED );
551 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200552 if( bits % 8 != 0 )
553 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200554
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200555 return( PSA_SUCCESS );
556}
557
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100558/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100559 *
Steven Cooremancd640932021-03-08 12:00:27 +0100560 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
561 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100562 *
563 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100564 * \param[in] key_type The key type of the key to be used with the
565 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100566 *
567 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100568 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100569 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100570 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100571 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100572MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100573 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100574 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100575{
Steven Cooremancd640932021-03-08 12:00:27 +0100576 if( PSA_ALG_IS_HMAC( algorithm ) )
577 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100578 if( key_type == PSA_KEY_TYPE_HMAC )
579 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100580 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100581
582 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100583 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100584 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
585 * key. */
586 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
587 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
588 {
589 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
590 * the block length (larger than 1) for block ciphers. */
591 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
592 return( PSA_SUCCESS );
593 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100594 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100595
596 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100597}
598
Steven Cooreman7609b1f2021-04-06 16:45:06 +0200599psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
600 size_t buffer_length )
Steven Cooreman75b74362020-07-28 14:30:13 +0200601{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100602 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200603 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200604
Ronald Cronea0f8a62020-11-25 17:52:23 +0100605 slot->key.data = mbedtls_calloc( 1, buffer_length );
606 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200607 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200608
Ronald Cronea0f8a62020-11-25 17:52:23 +0100609 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200610 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200611}
612
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200613psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
614 const uint8_t* data,
615 size_t data_length )
616{
617 psa_status_t status = psa_allocate_buffer_to_slot( slot,
618 data_length );
619 if( status != PSA_SUCCESS )
620 return( status );
621
Ronald Cronea0f8a62020-11-25 17:52:23 +0100622 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200623 return( PSA_SUCCESS );
624}
625
Ronald Crona0fe59f2020-11-29 11:14:53 +0100626psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100627 const psa_key_attributes_t *attributes,
628 const uint8_t *data, size_t data_length,
629 uint8_t *key_buffer, size_t key_buffer_size,
630 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100631{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100632 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
633 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200635 /* zero-length keys are never supported. */
636 if( data_length == 0 )
637 return( PSA_ERROR_NOT_SUPPORTED );
638
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100639 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100641 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200642
Steven Cooreman75b74362020-07-28 14:30:13 +0200643 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
644 if( data_length > SIZE_MAX / 8 )
645 return( PSA_ERROR_NOT_SUPPORTED );
646
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200647 /* Enforce a size limit, and in particular ensure that the bit
648 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100649 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200650 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200651
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100652 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200653 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200654 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200655
Ronald Crondd04d422020-11-28 15:14:42 +0100656 /* Copy the key material. */
657 memcpy( key_buffer, data, data_length );
658 *key_buffer_length = data_length;
659 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200660
Steven Cooreman40120f62020-10-29 11:42:22 +0100661 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100663 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200664 {
John Durkop9814fa22020-11-04 12:28:15 -0800665#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
666 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100667 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200668 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100669 return( mbedtls_psa_ecp_import_key( attributes,
670 data, data_length,
671 key_buffer, key_buffer_size,
672 key_buffer_length,
673 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100674 }
John Durkop9814fa22020-11-04 12:28:15 -0800675#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
676 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700677#if 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 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200680 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100681 return( mbedtls_psa_rsa_import_key( attributes,
682 data, data_length,
683 key_buffer, key_buffer_size,
684 key_buffer_length,
685 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200686 }
John Durkop9814fa22020-11-04 12:28:15 -0800687#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
688 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100689 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100690
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100691 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100692}
693
Gilles Peskinef603c712019-01-19 13:40:11 +0100694/** Calculate the intersection of two algorithm usage policies.
695 *
696 * Return 0 (which allows no operation) on incompatibility.
697 */
698static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100699 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100700 psa_algorithm_t alg1,
701 psa_algorithm_t alg2 )
702{
Gilles Peskine549ea862019-05-22 11:45:59 +0200703 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100704 if( alg1 == alg2 )
705 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100706 /* If the policies are from the same hash-and-sign family, check
707 * if one is a wildcard. If so the other has the specific algorithm. */
Gilles Peskine8cb22c82021-09-22 16:15:05 +0200708 if( PSA_ALG_IS_SIGN_HASH( alg1 ) &&
709 PSA_ALG_IS_SIGN_HASH( alg2 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100710 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100711 {
Steven Cooreman03488022021-02-18 12:07:20 +0100712 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
713 return( alg2 );
714 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
715 return( alg1 );
716 }
717 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100718 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100719 * restrictive tag length. */
720 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
721 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
722 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
723 {
724 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
725 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100726 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100727
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100728 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100729 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
730 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100731 {
Steven Cooremancd640932021-03-08 12:00:27 +0100732 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
733 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100734 }
735 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100736 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100737 ( alg1_len <= alg2_len ) )
738 {
739 return( alg2 );
740 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100741 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100742 ( alg2_len <= alg1_len ) )
743 {
744 return( alg1 );
745 }
746 }
747 /* If the policies are from the same MAC family, check whether one
748 * of them is a minimum-MAC-length policy. Calculate the most
749 * restrictive tag length. */
750 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
751 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
752 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
753 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100754 /* Validate the combination of key type and algorithm. Since the base
755 * algorithm of alg1 and alg2 are the same, we only need this once. */
756 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100757 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100758
Steven Cooreman31a876d2021-03-03 20:47:40 +0100759 /* Get the (exact or at-least) output lengths for both sides of the
760 * requested intersection. None of the currently supported algorithms
761 * have an output length dependent on the actual key size, so setting it
762 * to a bogus value of 0 is currently OK.
763 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100764 * Note that for at-least-this-length wildcard algorithms, the output
765 * length is set to the shortest allowed length, which allows us to
766 * calculate the most restrictive tag length for the intersection. */
767 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
768 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100769 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100770
Steven Cooremana1d83222021-02-25 10:20:29 +0100771 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100772 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
773 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100774 {
Steven Cooremancd640932021-03-08 12:00:27 +0100775 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100776 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100777
778 /* If only one is an at-least-this-length policy, the intersection would
779 * be the other (fixed-length) policy as long as said fixed length is
780 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100781 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100782 {
Steven Cooremancd640932021-03-08 12:00:27 +0100783 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100784 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100785 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100786 {
Steven Cooremancd640932021-03-08 12:00:27 +0100787 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100788 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100789
Steven Cooremancd640932021-03-08 12:00:27 +0100790 /* If none of them are wildcards, check whether they define the same tag
791 * length. This is still possible here when one is default-length and
792 * the other specific-length. Ensure to always return the
793 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100794 if( alg1_len == alg2_len )
795 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100796 }
797 /* If the policies are incompatible, allow nothing. */
798 return( 0 );
799}
800
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100801static int psa_key_algorithm_permits( psa_key_type_t key_type,
802 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200803 psa_algorithm_t requested_alg )
804{
Gilles Peskine549ea862019-05-22 11:45:59 +0200805 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200806 if( requested_alg == policy_alg )
807 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100808 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
809 * and requested_alg is the same hash-and-sign family with any hash,
810 * then requested_alg is compliant with policy_alg. */
Gilles Peskine8cb22c82021-09-22 16:15:05 +0200811 if( PSA_ALG_IS_SIGN_HASH( requested_alg ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100812 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200813 {
Steven Cooreman03488022021-02-18 12:07:20 +0100814 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
815 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
816 }
817 /* If policy_alg is a wildcard AEAD algorithm of the same base as
818 * the requested algorithm, check the requested tag length to be
819 * equal-length or longer than the wildcard-specified length. */
820 if( PSA_ALG_IS_AEAD( policy_alg ) &&
821 PSA_ALG_IS_AEAD( requested_alg ) &&
822 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
823 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100824 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100825 {
826 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
827 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
828 }
Steven Cooremancd640932021-03-08 12:00:27 +0100829 /* If policy_alg is a MAC algorithm of the same base as the requested
830 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100831 if( PSA_ALG_IS_MAC( policy_alg ) &&
832 PSA_ALG_IS_MAC( requested_alg ) &&
833 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100834 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100835 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100836 /* Validate the combination of key type and algorithm. Since the policy
837 * and requested algorithms are the same, we only need this once. */
838 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100839 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100840
Steven Cooreman31a876d2021-03-03 20:47:40 +0100841 /* Get both the requested output length for the algorithm which is to be
842 * verified, and the default output length for the base algorithm.
843 * Note that none of the currently supported algorithms have an output
844 * length dependent on actual key size, so setting it to a bogus value
845 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100846 size_t requested_output_length = PSA_MAC_LENGTH(
847 key_type, 0, requested_alg );
848 size_t default_output_length = PSA_MAC_LENGTH(
849 key_type, 0,
850 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100851
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100852 /* If the policy is default-length, only allow an algorithm with
853 * a declared exact-length matching the default. */
854 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100855 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100856
857 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100858 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100859 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
860 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
861 {
862 return( 1 );
863 }
864
Steven Cooremancd640932021-03-08 12:00:27 +0100865 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
866 * check for the requested MAC length to be equal to or longer than the
867 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100868 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
869 {
870 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100871 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100872 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200873 }
Steven Cooremance48e852020-10-05 16:02:45 +0200874 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200875 * a key derivation with that key agreement should also be allowed. This
876 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200877 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
878 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
879 {
880 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
881 policy_alg );
882 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100883 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200884 return( 0 );
885}
886
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100887/** Test whether a policy permits an algorithm.
888 *
889 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100890 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100891 * \note This function requires providing the key type for which the policy is
892 * being validated, since some algorithm policy definitions (e.g. MAC)
893 * have different properties depending on what kind of cipher it is
894 * combined with.
895 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100896 * \retval PSA_SUCCESS When \p alg is a specific algorithm
897 * allowed by the \p policy.
898 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
899 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
900 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100901 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100902static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100903 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100904 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100905{
Steven Cooremand788fab2021-02-25 11:29:17 +0100906 /* '0' is not a valid algorithm */
907 if( alg == 0 )
908 return( PSA_ERROR_INVALID_ARGUMENT );
909
Steven Cooreman7e39f052021-02-23 14:18:32 +0100910 /* A requested algorithm cannot be a wildcard. */
911 if( PSA_ALG_IS_WILDCARD( alg ) )
912 return( PSA_ERROR_INVALID_ARGUMENT );
913
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100914 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
915 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100916 return( PSA_SUCCESS );
917 else
918 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100919}
920
Gilles Peskinef603c712019-01-19 13:40:11 +0100921/** Restrict a key policy based on a constraint.
922 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100923 * \note This function requires providing the key type for which the policy is
924 * being restricted, since some algorithm policy definitions (e.g. MAC)
925 * have different properties depending on what kind of cipher it is
926 * combined with.
927 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100928 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100929 * \param[in,out] policy The policy to restrict.
930 * \param[in] constraint The policy constraint to apply.
931 *
932 * \retval #PSA_SUCCESS
933 * \c *policy contains the intersection of the original value of
934 * \c *policy and \c *constraint.
935 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100936 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100937 * \c *policy is unchanged.
938 */
939static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100940 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100941 psa_key_policy_t *policy,
942 const psa_key_policy_t *constraint )
943{
944 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100945 psa_key_policy_algorithm_intersection( key_type, policy->alg,
946 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200947 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100948 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
949 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100950 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
951 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200952 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
953 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100954 policy->usage &= constraint->usage;
955 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200956 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100957 return( PSA_SUCCESS );
958}
959
Ronald Cron5c522922020-11-14 16:35:34 +0100960/** Get the description of a key given its identifier and policy constraints
961 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200962 *
Ronald Cron5c522922020-11-14 16:35:34 +0100963 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100964 * nonzero, the key must allow operations with this algorithm. If \p alg is
965 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100966 *
Ronald Cron5c522922020-11-14 16:35:34 +0100967 * In case of a persistent key, the function loads the description of the key
968 * into a key slot if not already done.
969 *
970 * On success, the returned key slot is locked. It is the responsibility of
971 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200972 */
Ronald Cron5c522922020-11-14 16:35:34 +0100973static psa_status_t psa_get_and_lock_key_slot_with_policy(
974 mbedtls_svc_key_id_t key,
975 psa_key_slot_t **p_slot,
976 psa_key_usage_t usage,
977 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100978{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200979 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
980 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100981
Ronald Cron5c522922020-11-14 16:35:34 +0100982 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100983 if( status != PSA_SUCCESS )
984 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200985 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100986
987 /* Enforce that usage policy for the key slot contains all the flags
988 * required by the usage parameter. There is one exception: public
989 * keys can always be exported, so we treat public key objects as
990 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200991 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100992 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200993
Gilles Peskine8e338702019-07-30 20:06:31 +0200994 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100995 {
996 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200997 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100998 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100999
1000 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +01001001 if( alg != 0 )
1002 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001003 status = psa_key_policy_permits( &slot->attr.policy,
1004 slot->attr.type,
1005 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +01001006 if( status != PSA_SUCCESS )
1007 goto error;
1008 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001009
Darryl Green06fd18d2018-07-16 11:21:11 +01001010 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001011
1012error:
1013 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001014 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001015
1016 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001017}
Darryl Green940d72c2018-07-13 13:18:51 +01001018
Ronald Cron5c522922020-11-14 16:35:34 +01001019/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001020 *
1021 * A transparent key is a key for which the key material is directly
Ronald Cron8a0466a2021-08-24 15:39:44 +02001022 * available, as opposed to a key in a secure element and/or to be used
1023 * by a secure element.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001024 *
Ronald Cron8a0466a2021-08-24 15:39:44 +02001025 * This is a temporary function that may be used instead of
1026 * psa_get_and_lock_key_slot_with_policy() when there is no opaque key support
1027 * for a cryptographic operation.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001028 *
Ronald Cron5c522922020-11-14 16:35:34 +01001029 * On success, the returned key slot is locked. It is the responsibility of the
1030 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001031 */
Ronald Cron5c522922020-11-14 16:35:34 +01001032static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1033 mbedtls_svc_key_id_t key,
1034 psa_key_slot_t **p_slot,
1035 psa_key_usage_t usage,
1036 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001037{
Ronald Cron5c522922020-11-14 16:35:34 +01001038 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1039 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001040 if( status != PSA_SUCCESS )
1041 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001042
Ronald Cron8a0466a2021-08-24 15:39:44 +02001043 if( psa_key_lifetime_is_external( (*p_slot)->attr.lifetime ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001044 {
Ronald Cron5c522922020-11-14 16:35:34 +01001045 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001046 *p_slot = NULL;
1047 return( PSA_ERROR_NOT_SUPPORTED );
1048 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001049
Gilles Peskine28f8f302019-07-24 13:30:31 +02001050 return( PSA_SUCCESS );
1051}
Gilles Peskine28f8f302019-07-24 13:30:31 +02001052
Steven Cooreman7ddee7f2021-04-07 18:08:30 +02001053psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001054{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001055 /* Data pointer will always be either a valid pointer or NULL in an
1056 * initialized slot, so we can just free it. */
1057 if( slot->key.data != NULL )
1058 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1059
1060 mbedtls_free( slot->key.data );
1061 slot->key.data = NULL;
1062 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001063
1064 return( PSA_SUCCESS );
1065}
1066
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001067/** Completely wipe a slot in memory, including its policy.
1068 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001069psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001070{
1071 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001072
1073 /*
1074 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001075 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001076 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1077 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001078 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001079 */
Ronald Cron5c522922020-11-14 16:35:34 +01001080 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001081 {
1082#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001083 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001084#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001085 status = PSA_ERROR_CORRUPTION_DETECTED;
1086 }
1087
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001088 /* Multipart operations may still be using the key. This is safe
1089 * because all multipart operation objects are independent from
1090 * the key slot: if they need to access the key after the setup
1091 * phase, they have a copy of the key. Note that this means that
1092 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001093 /* At this point, key material and other type-specific content has
1094 * been wiped. Clear remaining metadata. We can call memset and not
1095 * zeroize because the metadata is not particularly sensitive. */
1096 memset( slot, 0, sizeof( *slot ) );
1097 return( status );
1098}
1099
Ronald Croncf56a0a2020-08-04 09:51:30 +02001100psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001101{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001102 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001103 psa_status_t status; /* status of the last operation */
1104 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001105#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1106 psa_se_drv_table_entry_t *driver;
1107#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001108
Ronald Croncf56a0a2020-08-04 09:51:30 +02001109 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001110 return( PSA_SUCCESS );
1111
Ronald Cronf2911112020-10-29 17:51:10 +01001112 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001113 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001114 * key, this will load the key description from persistent memory if not
1115 * done yet. We cannot avoid this loading as without it we don't know if
1116 * the key is operated by an SE or not and this information is needed by
1117 * the current implementation.
1118 */
Ronald Cron5c522922020-11-14 16:35:34 +01001119 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001120 if( status != PSA_SUCCESS )
1121 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001122
Ronald Cronf2911112020-10-29 17:51:10 +01001123 /*
1124 * If the key slot containing the key description is under access by the
1125 * library (apart from the present access), the key cannot be destroyed
1126 * yet. For the time being, just return in error. Eventually (to be
1127 * implemented), the key should be destroyed when all accesses have
1128 * stopped.
1129 */
Ronald Cron5c522922020-11-14 16:35:34 +01001130 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001131 {
Ronald Cron5c522922020-11-14 16:35:34 +01001132 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001133 return( PSA_ERROR_GENERIC_ERROR );
1134 }
1135
Gilles Peskine251c7742021-04-21 22:32:05 +02001136 if( PSA_KEY_LIFETIME_IS_READ_ONLY( slot->attr.lifetime ) )
1137 {
1138 /* Refuse the destruction of a read-only key (which may or may not work
1139 * if we attempt it, depending on whether the key is merely read-only
1140 * by policy or actually physically read-only).
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001141 * Just do the best we can, which is to wipe the copy in memory
1142 * (done in this function's cleanup code). */
1143 overall_status = PSA_ERROR_NOT_PERMITTED;
1144 goto exit;
Gilles Peskine251c7742021-04-21 22:32:05 +02001145 }
1146
Gilles Peskine354f7672019-07-12 23:46:38 +02001147#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001148 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001149 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001150 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001151 /* For a key in a secure element, we need to do three things:
1152 * remove the key file in internal storage, destroy the
1153 * key inside the secure element, and update the driver's
1154 * persistent data. Start a transaction that will encompass these
1155 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001156 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001157 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001158 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001159 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001160 status = psa_crypto_save_transaction( );
1161 if( status != PSA_SUCCESS )
1162 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001163 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001164 /* We should still try to destroy the key in the secure
1165 * element and the key metadata in storage. This is especially
1166 * important if the error is that the storage is full.
1167 * But how to do it exactly without risking an inconsistent
1168 * state after a reset?
1169 * https://github.com/ARMmbed/mbed-crypto/issues/215
1170 */
1171 overall_status = status;
1172 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001173 }
1174
Ronald Cronea0f8a62020-11-25 17:52:23 +01001175 status = psa_destroy_se_key( driver,
1176 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001177 if( overall_status == PSA_SUCCESS )
1178 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001179 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001180#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1181
Darryl Greend49a4992018-06-18 17:27:26 +01001182#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001183 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001184 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001185 status = psa_destroy_persistent_key( slot->attr.id );
1186 if( overall_status == PSA_SUCCESS )
1187 overall_status = status;
1188
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001189 /* TODO: other slots may have a copy of the same key. We should
1190 * invalidate them.
1191 * https://github.com/ARMmbed/mbed-crypto/issues/214
1192 */
Darryl Greend49a4992018-06-18 17:27:26 +01001193 }
1194#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001195
Gilles Peskinefc762652019-07-22 19:30:34 +02001196#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1197 if( driver != NULL )
1198 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001199 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001200 if( overall_status == PSA_SUCCESS )
1201 overall_status = status;
1202 status = psa_crypto_stop_transaction( );
1203 if( overall_status == PSA_SUCCESS )
1204 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001205 }
1206#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1207
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001208exit:
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001209 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001210 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
Gilles Peskine2bfbb172021-06-07 23:27:54 +02001211 if( status != PSA_SUCCESS )
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001212 overall_status = status;
1213 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001214}
1215
John Durkop07cc04a2020-11-16 22:08:34 -08001216#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001217 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001218static psa_status_t psa_get_rsa_public_exponent(
1219 const mbedtls_rsa_context *rsa,
1220 psa_key_attributes_t *attributes )
1221{
1222 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001224 uint8_t *buffer = NULL;
1225 size_t buflen;
1226 mbedtls_mpi_init( &mpi );
1227
1228 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1229 if( ret != 0 )
1230 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001231 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1232 {
1233 /* It's the default value, which is reported as an empty string,
1234 * so there's nothing to do. */
1235 goto exit;
1236 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001237
1238 buflen = mbedtls_mpi_size( &mpi );
1239 buffer = mbedtls_calloc( 1, buflen );
1240 if( buffer == NULL )
1241 {
1242 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1243 goto exit;
1244 }
1245 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1246 if( ret != 0 )
1247 goto exit;
1248 attributes->domain_parameters = buffer;
1249 attributes->domain_parameters_size = buflen;
1250
1251exit:
1252 mbedtls_mpi_free( &mpi );
1253 if( ret != 0 )
1254 mbedtls_free( buffer );
1255 return( mbedtls_to_psa_error( ret ) );
1256}
John Durkop07cc04a2020-11-16 22:08:34 -08001257#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001258 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001259
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001260/** Retrieve all the publicly-accessible attributes of a key.
1261 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001262psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001263 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001264{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001265 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001266 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001267 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001268
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001269 psa_reset_key_attributes( attributes );
1270
Ronald Cron5c522922020-11-14 16:35:34 +01001271 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001272 if( status != PSA_SUCCESS )
1273 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001274
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001275 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001276 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1277 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1278
1279#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron0518f612021-08-24 15:50:05 +02001280 if( psa_get_se_driver_entry( slot->attr.lifetime ) != NULL )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001281 psa_set_key_slot_number( attributes,
1282 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001283#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001284
Gilles Peskine8e338702019-07-30 20:06:31 +02001285 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001286 {
John Durkop07cc04a2020-11-16 22:08:34 -08001287#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001288 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001289 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001290 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01001291 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001292 * is not yet implemented.
1293 * https://github.com/ARMmbed/mbed-crypto/issues/216
1294 */
Ronald Cron41e80062021-08-24 16:00:51 +02001295 if( ! psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02001296 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001297 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001298
Ronald Cron90857082020-11-25 14:58:33 +01001299 status = mbedtls_psa_rsa_load_representation(
1300 slot->attr.type,
1301 slot->key.data,
1302 slot->key.bytes,
1303 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001304 if( status != PSA_SUCCESS )
1305 break;
1306
Steven Cooremana2371e52020-07-28 14:30:39 +02001307 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001308 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001309 mbedtls_rsa_free( rsa );
1310 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001311 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001312 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001313#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001314 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001315 default:
1316 /* Nothing else to do. */
1317 break;
1318 }
1319
1320 if( status != PSA_SUCCESS )
1321 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001322
Ronald Cron5c522922020-11-14 16:35:34 +01001323 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001324
Ronald Cron5c522922020-11-14 16:35:34 +01001325 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001326}
1327
Gilles Peskinec8000c02019-08-02 20:15:51 +02001328#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1329psa_status_t psa_get_key_slot_number(
1330 const psa_key_attributes_t *attributes,
1331 psa_key_slot_number_t *slot_number )
1332{
1333 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1334 {
1335 *slot_number = attributes->slot_number;
1336 return( PSA_SUCCESS );
1337 }
1338 else
1339 return( PSA_ERROR_INVALID_ARGUMENT );
1340}
1341#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1342
Ronald Crond18b5f82020-11-25 16:46:05 +01001343static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1344 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001345 uint8_t *data,
1346 size_t data_size,
1347 size_t *data_length )
1348{
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001350 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001351 memcpy( data, key_buffer, key_buffer_size );
1352 memset( data + key_buffer_size, 0,
1353 data_size - key_buffer_size );
1354 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001355 return( PSA_SUCCESS );
1356}
1357
Ronald Cron7285cda2020-11-26 14:46:37 +01001358psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001359 const psa_key_attributes_t *attributes,
1360 const uint8_t *key_buffer, size_t key_buffer_size,
1361 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001362{
Ronald Crond18b5f82020-11-25 16:46:05 +01001363 psa_key_type_t type = attributes->core.type;
1364
Ronald Crond18b5f82020-11-25 16:46:05 +01001365 if( key_type_is_raw_bytes( type ) ||
1366 PSA_KEY_TYPE_IS_RSA( type ) ||
1367 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001368 {
1369 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001370 key_buffer, key_buffer_size,
1371 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001372 }
1373 else
1374 {
1375 /* This shouldn't happen in the reference implementation, but
1376 it is valid for a special-purpose implementation to omit
1377 support for exporting certain key types. */
1378 return( PSA_ERROR_NOT_SUPPORTED );
1379 }
1380}
1381
1382psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1383 uint8_t *data,
1384 size_t data_size,
1385 size_t *data_length )
1386{
1387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1388 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1389 psa_key_slot_t *slot;
1390
Ronald Crone907e552021-01-18 13:22:38 +01001391 /* Reject a zero-length output buffer now, since this can never be a
1392 * valid key representation. This way we know that data must be a valid
1393 * pointer and we can do things like memset(data, ..., data_size). */
1394 if( data_size == 0 )
1395 return( PSA_ERROR_BUFFER_TOO_SMALL );
1396
Ronald Cron9486f9d2020-11-26 13:36:23 +01001397 /* Set the key to empty now, so that even when there are errors, we always
1398 * set data_length to a value between 0 and data_size. On error, setting
1399 * the key to empty is a good choice because an empty key representation is
1400 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001401 *data_length = 0;
1402
Ronald Cron9486f9d2020-11-26 13:36:23 +01001403 /* Export requires the EXPORT flag. There is an exception for public keys,
1404 * which don't require any flag, but
1405 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1406 */
1407 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1408 PSA_KEY_USAGE_EXPORT, 0 );
1409 if( status != PSA_SUCCESS )
1410 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001411
Ronald Crond18b5f82020-11-25 16:46:05 +01001412 psa_key_attributes_t attributes = {
1413 .core = slot->attr
1414 };
Ronald Cron67227982020-11-26 15:16:05 +01001415 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001416 slot->key.data, slot->key.bytes,
1417 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001418
Ronald Cron9486f9d2020-11-26 13:36:23 +01001419 unlock_status = psa_unlock_key_slot( slot );
1420
1421 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1422}
1423
Ronald Cron7285cda2020-11-26 14:46:37 +01001424psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001425 const psa_key_attributes_t *attributes,
1426 const uint8_t *key_buffer,
1427 size_t key_buffer_size,
1428 uint8_t *data,
1429 size_t data_size,
1430 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001431{
Ronald Crond18b5f82020-11-25 16:46:05 +01001432 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001433
Ronald Crond18b5f82020-11-25 16:46:05 +01001434 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001435 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001436 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001437 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001438 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001439 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001440 key_buffer, key_buffer_size,
1441 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001442 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001443
Ronald Crond18b5f82020-11-25 16:46:05 +01001444 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001445 {
John Durkop0e005192020-10-31 22:06:54 -07001446#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1447 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001448 return( mbedtls_psa_rsa_export_public_key( attributes,
1449 key_buffer,
1450 key_buffer_size,
1451 data,
1452 data_size,
1453 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001454#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001455 /* We don't know how to convert a private RSA key to public. */
1456 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001457#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1458 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001459 }
1460 else
Moran Pekera998bc62018-04-16 18:16:20 +03001461 {
John Durkop6ba40d12020-11-10 08:50:04 -08001462#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1463 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001464 return( mbedtls_psa_ecp_export_public_key( attributes,
1465 key_buffer,
1466 key_buffer_size,
1467 data,
1468 data_size,
1469 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001470#else
1471 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001472 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001473#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1474 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001475 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001476 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001477 else
1478 {
1479 /* This shouldn't happen in the reference implementation, but
1480 it is valid for a special-purpose implementation to omit
1481 support for exporting certain key types. */
1482 return( PSA_ERROR_NOT_SUPPORTED );
1483 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001484}
Ronald Crond18b5f82020-11-25 16:46:05 +01001485
Ronald Croncf56a0a2020-08-04 09:51:30 +02001486psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001487 uint8_t *data,
1488 size_t data_size,
1489 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001490{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001491 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001492 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001493 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001494
Ronald Crone907e552021-01-18 13:22:38 +01001495 /* Reject a zero-length output buffer now, since this can never be a
1496 * valid key representation. This way we know that data must be a valid
1497 * pointer and we can do things like memset(data, ..., data_size). */
1498 if( data_size == 0 )
1499 return( PSA_ERROR_BUFFER_TOO_SMALL );
1500
Darryl Greendd8fb772018-11-07 16:00:44 +00001501 /* Set the key to empty now, so that even when there are errors, we always
1502 * set data_length to a value between 0 and data_size. On error, setting
1503 * the key to empty is a good choice because an empty key representation is
1504 * unlikely to be accepted anywhere. */
1505 *data_length = 0;
1506
1507 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001508 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001509 if( status != PSA_SUCCESS )
1510 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001511
Ronald Cron9486f9d2020-11-26 13:36:23 +01001512 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1513 {
1514 status = PSA_ERROR_INVALID_ARGUMENT;
1515 goto exit;
1516 }
1517
Ronald Crond18b5f82020-11-25 16:46:05 +01001518 psa_key_attributes_t attributes = {
1519 .core = slot->attr
1520 };
Ronald Cron67227982020-11-26 15:16:05 +01001521 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001522 &attributes, slot->key.data, slot->key.bytes,
1523 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001524
1525exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001526 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001527
Ronald Cron5c522922020-11-14 16:35:34 +01001528 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001529}
1530
Gilles Peskine91e8c332019-08-02 19:19:39 +02001531#if defined(static_assert)
1532static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1533 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001534static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001535 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001536static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001537 "One or more key attribute flag is listed as both internal-only and external-only" );
1538#endif
1539
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001540/** Validate that a key policy is internally well-formed.
1541 *
1542 * This function only rejects invalid policies. It does not validate the
1543 * consistency of the policy with respect to other attributes of the key
1544 * such as the key type.
1545 */
1546static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001547{
1548 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001549 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 PSA_KEY_USAGE_ENCRYPT |
1551 PSA_KEY_USAGE_DECRYPT |
gabor-mezei-arme8efa392021-04-14 21:14:28 +02001552 PSA_KEY_USAGE_SIGN_MESSAGE |
1553 PSA_KEY_USAGE_VERIFY_MESSAGE |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001554 PSA_KEY_USAGE_SIGN_HASH |
1555 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001556 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1557 return( PSA_ERROR_INVALID_ARGUMENT );
1558
Gilles Peskine4747d192019-04-17 15:05:45 +02001559 return( PSA_SUCCESS );
1560}
1561
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001562/** Validate the internal consistency of key attributes.
1563 *
1564 * This function only rejects invalid attribute values. If does not
1565 * validate the consistency of the attributes with any key data that may
1566 * be involved in the creation of the key.
1567 *
1568 * Call this function early in the key creation process.
1569 *
1570 * \param[in] attributes Key attributes for the new key.
1571 * \param[out] p_drv On any return, the driver for the key, if any.
1572 * NULL for a transparent key.
1573 *
1574 */
1575static psa_status_t psa_validate_key_attributes(
1576 const psa_key_attributes_t *attributes,
1577 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001578{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001579 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001580 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001581 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001582
Ronald Cron54b90082020-10-29 15:26:43 +01001583 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001584 if( status != PSA_SUCCESS )
1585 return( status );
1586
Ronald Crond2ed4812020-07-17 16:11:30 +02001587 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001588 if( status != PSA_SUCCESS )
1589 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001590
Ronald Cron65f38a32020-10-23 17:11:13 +02001591 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1592 {
1593 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1594 return( PSA_ERROR_INVALID_ARGUMENT );
1595 }
1596 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001597 {
Ronald Cron77e412c2021-03-31 17:36:31 +02001598 if( !psa_is_valid_key_id( psa_get_key_id( attributes ), 0 ) )
1599 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Crond2ed4812020-07-17 16:11:30 +02001600 }
1601
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001602 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001603 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001604 return( status );
1605
1606 /* Refuse to create overly large keys.
1607 * Note that this doesn't trigger on import if the attributes don't
1608 * explicitly specify a size (so psa_get_key_bits returns 0), so
1609 * psa_import_key() needs its own checks. */
1610 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1611 return( PSA_ERROR_NOT_SUPPORTED );
1612
Gilles Peskine91e8c332019-08-02 19:19:39 +02001613 /* Reject invalid flags. These should not be reachable through the API. */
1614 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1615 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1616 return( PSA_ERROR_INVALID_ARGUMENT );
1617
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001618 return( PSA_SUCCESS );
1619}
1620
Gilles Peskine4747d192019-04-17 15:05:45 +02001621/** Prepare a key slot to receive key material.
1622 *
1623 * This function allocates a key slot and sets its metadata.
1624 *
1625 * If this function fails, call psa_fail_key_creation().
1626 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001627 * This function is intended to be used as follows:
1628 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001629 * it with the specified attributes, and in case of a volatile key assign it
1630 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001631 * -# Populate the slot with the key material.
1632 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1633 * In case of failure at any step, stop the sequence and call
1634 * psa_fail_key_creation().
1635 *
Ronald Cron5c522922020-11-14 16:35:34 +01001636 * On success, the key slot is locked. It is the responsibility of the caller
1637 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001638 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001639 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001640 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001641 * \param[out] p_slot On success, a pointer to the prepared slot.
1642 * \param[out] p_drv On any return, the driver for the key, if any.
1643 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001644 *
1645 * \retval #PSA_SUCCESS
1646 * The key slot is ready to receive key material.
1647 * \return If this function fails, the key slot is an invalid state.
1648 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001649 */
1650static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001651 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001653 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001654 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001655{
1656 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001657 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 psa_key_slot_t *slot;
1659
Gilles Peskinedf179142019-07-15 22:02:14 +02001660 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001661 *p_drv = NULL;
1662
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001663 status = psa_validate_key_attributes( attributes, p_drv );
1664 if( status != PSA_SUCCESS )
1665 return( status );
1666
Ronald Cronc4d1b512020-07-31 11:26:37 +02001667 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001668 if( status != PSA_SUCCESS )
1669 return( status );
1670 slot = *p_slot;
1671
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001672 /* We're storing the declared bit-size of the key. It's up to each
1673 * creation mechanism to verify that this information is correct.
1674 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001675 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001676 * is optional (import, copy). In case of a volatile key, assign it the
1677 * volatile key identifier associated to the slot returned to contain its
1678 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001679
1680 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001681 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1682 {
1683#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1684 slot->attr.id = volatile_key_id;
1685#else
1686 slot->attr.id.key_id = volatile_key_id;
1687#endif
1688 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001689
Gilles Peskine91e8c332019-08-02 19:19:39 +02001690 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001691 * external-only flags, query `attributes`. Thanks to the check
1692 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001693 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001694 * may have set. */
1695 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001696
Gilles Peskinecbaff462019-07-12 23:46:04 +02001697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001698 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001699 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001700 * create the key file in internal storage, create the
1701 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001702 * persistent data. This is done by starting a transaction that will
1703 * encompass these three actions.
1704 * For registering a volatile key, we just need to find an appropriate
1705 * slot number inside the SE. Since the key is designated volatile, creating
1706 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001707 /* The first thing to do is to find a slot number for the new key.
1708 * We save the slot number in persistent storage as part of the
1709 * transaction data. It will be needed to recover if the power
1710 * fails during the key creation process, to clean up on the secure
1711 * element side after restarting. Obtaining a slot number from the
1712 * secure element driver updates its persistent state, but we do not yet
1713 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001714 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001715 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001716 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001717 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001718 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001719 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001720 if( status != PSA_SUCCESS )
1721 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001722
1723 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001724 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001725 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1726 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001727 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001728 psa_crypto_transaction.key.id = slot->attr.id;
1729 status = psa_crypto_save_transaction( );
1730 if( status != PSA_SUCCESS )
1731 {
1732 (void) psa_crypto_stop_transaction( );
1733 return( status );
1734 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001735 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001736
1737 status = psa_copy_key_material_into_slot(
1738 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001739 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001740
1741 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1742 {
1743 /* Key registration only makes sense with a secure element. */
1744 return( PSA_ERROR_INVALID_ARGUMENT );
1745 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001746#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1747
Ronald Cronc4d1b512020-07-31 11:26:37 +02001748 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001749}
Gilles Peskine4747d192019-04-17 15:05:45 +02001750
1751/** Finalize the creation of a key once its key material has been set.
1752 *
1753 * This entails writing the key to persistent storage.
1754 *
1755 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001756 * See the documentation of psa_start_key_creation() for the intended use
1757 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 *
Ronald Cron5c522922020-11-14 16:35:34 +01001759 * If the finalization succeeds, the function unlocks the key slot (it was
1760 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1761 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001762 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001763 * \param[in,out] slot Pointer to the slot with key material.
1764 * \param[in] driver The secure element driver for the key,
1765 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001766 * \param[out] key On success, identifier of the key. Note that the
1767 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001768 *
1769 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001770 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001771 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1772 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1773 * \retval #PSA_ERROR_ALREADY_EXISTS
1774 * \retval #PSA_ERROR_DATA_INVALID
1775 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001776 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001777 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001778 * \return If this function fails, the key slot is an invalid state.
1779 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001780 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001781static psa_status_t psa_finish_key_creation(
1782 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001783 psa_se_drv_table_entry_t *driver,
1784 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001785{
1786 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001787 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001788 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001789
1790#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001791 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001792 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001793#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1794 if( driver != NULL )
1795 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001796 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001797 psa_key_slot_number_t slot_number =
1798 psa_key_slot_get_slot_number( slot ) ;
1799
Gilles Peskineb46bef22019-07-30 21:32:04 +02001800#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001801 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001802 sizeof( data.slot_number ),
1803 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001804#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001805 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001806 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001807 (uint8_t*) &data,
1808 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001809 }
1810 else
1811#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1812 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001813 /* Key material is saved in export representation in the slot, so
1814 * just pass the slot buffer for storage. */
1815 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001816 slot->key.data,
1817 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001818 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001819 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001820#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1821
Gilles Peskinecbaff462019-07-12 23:46:04 +02001822#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001823 /* Finish the transaction for a key creation. This does not
1824 * happen when registering an existing key. Detect this case
1825 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001826 * creation of a persistent key in a secure element requires a transaction,
1827 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001828 if( driver != NULL &&
1829 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001830 {
1831 status = psa_save_se_persistent_data( driver );
1832 if( status != PSA_SUCCESS )
1833 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001834 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001835 return( status );
1836 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001837 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001838 }
1839#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1840
Ronald Cron50972942020-11-14 11:28:25 +01001841 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001842 {
1843 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001844 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001845 if( status != PSA_SUCCESS )
1846 *key = MBEDTLS_SVC_KEY_ID_INIT;
1847 }
Ronald Cron50972942020-11-14 11:28:25 +01001848
Gilles Peskine4747d192019-04-17 15:05:45 +02001849 return( status );
1850}
1851
1852/** Abort the creation of a key.
1853 *
1854 * You may call this function after calling psa_start_key_creation(),
1855 * or after psa_finish_key_creation() fails. In other circumstances, this
1856 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001857 * See the documentation of psa_start_key_creation() for the intended use
1858 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001860 * \param[in,out] slot Pointer to the slot with key material.
1861 * \param[in] driver The secure element driver for the key,
1862 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001863 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001864static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001865 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001866{
Gilles Peskine011e4282019-06-26 18:34:38 +02001867 (void) driver;
1868
Gilles Peskine4747d192019-04-17 15:05:45 +02001869 if( slot == NULL )
1870 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001871
1872#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001873 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001874 * element, and the failure happened later (when saving metadata
1875 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001876 * element.
1877 * https://github.com/ARMmbed/mbed-crypto/issues/217
1878 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001879
Gilles Peskined7729582019-08-05 15:55:54 +02001880 /* Abort the ongoing transaction if any (there may not be one if
1881 * the creation process failed before starting one, or if the
1882 * key creation is a registration of a key in a secure element).
1883 * Earlier functions must already have done what it takes to undo any
1884 * partial creation. All that's left is to update the transaction data
1885 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001886 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001887#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1888
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 psa_wipe_key_slot( slot );
1890}
1891
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001892/** Validate optional attributes during key creation.
1893 *
1894 * Some key attributes are optional during key creation. If they are
1895 * specified in the attributes structure, check that they are consistent
1896 * with the data in the slot.
1897 *
1898 * This function should be called near the end of key creation, after
1899 * the slot in memory is fully populated but before saving persistent data.
1900 */
1901static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001902 const psa_key_slot_t *slot,
1903 const psa_key_attributes_t *attributes )
1904{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001905 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001906 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001907 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001908 return( PSA_ERROR_INVALID_ARGUMENT );
1909 }
1910
1911 if( attributes->domain_parameters_size != 0 )
1912 {
John Durkop0e005192020-10-31 22:06:54 -07001913#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1914 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001915 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001916 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001917 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001918 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001920
Ronald Cron90857082020-11-25 14:58:33 +01001921 psa_status_t status = mbedtls_psa_rsa_load_representation(
1922 slot->attr.type,
1923 slot->key.data,
1924 slot->key.bytes,
1925 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001926 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001927 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001928
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001929 mbedtls_mpi_init( &actual );
1930 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001931 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001932 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001933 mbedtls_rsa_free( rsa );
1934 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001935 if( ret != 0 )
1936 goto rsa_exit;
1937 ret = mbedtls_mpi_read_binary( &required,
1938 attributes->domain_parameters,
1939 attributes->domain_parameters_size );
1940 if( ret != 0 )
1941 goto rsa_exit;
1942 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1943 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1944 rsa_exit:
1945 mbedtls_mpi_free( &actual );
1946 mbedtls_mpi_free( &required );
1947 if( ret != 0)
1948 return( mbedtls_to_psa_error( ret ) );
1949 }
1950 else
John Durkop9814fa22020-11-04 12:28:15 -08001951#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1952 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953 {
1954 return( PSA_ERROR_INVALID_ARGUMENT );
1955 }
1956 }
1957
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001958 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001960 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001961 return( PSA_ERROR_INVALID_ARGUMENT );
1962 }
1963
1964 return( PSA_SUCCESS );
1965}
1966
Gilles Peskine4747d192019-04-17 15:05:45 +02001967psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001968 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001969 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001970 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001971{
1972 psa_status_t status;
1973 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001974 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001975 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001976
Ronald Cron81709fc2020-11-14 12:10:32 +01001977 *key = MBEDTLS_SVC_KEY_ID_INIT;
1978
Gilles Peskine0f84d622019-09-12 19:03:13 +02001979 /* Reject zero-length symmetric keys (including raw data key objects).
1980 * This also rejects any key which might be encoded as an empty string,
1981 * which is never valid. */
1982 if( data_length == 0 )
1983 return( PSA_ERROR_INVALID_ARGUMENT );
1984
Gilles Peskinedf179142019-07-15 22:02:14 +02001985 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001986 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001990 /* In the case of a transparent key or an opaque key stored in local
1991 * storage (thus not in the case of generating a key in a secure element
1992 * or cryptoprocessor with storage), we have to allocate a buffer to
1993 * hold the generated key material. */
1994 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001995 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001996 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001997 if( status != PSA_SUCCESS )
1998 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001999 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002000
2001 bits = slot->attr.bits;
2002 status = psa_driver_wrapper_import_key( attributes,
2003 data, data_length,
2004 slot->key.data,
2005 slot->key.bytes,
2006 &slot->key.bytes, &bits );
2007 if( status != PSA_SUCCESS )
2008 goto exit;
2009
2010 if( slot->attr.bits == 0 )
2011 slot->attr.bits = (psa_key_bits_t) bits;
2012 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01002013 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002014 status = PSA_ERROR_INVALID_ARGUMENT;
2015 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002016 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002017
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002018 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002019 if( status != PSA_SUCCESS )
2020 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002021
Ronald Cron81709fc2020-11-14 12:10:32 +01002022 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002023exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002024 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002025 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002026
Gilles Peskine4747d192019-04-17 15:05:45 +02002027 return( status );
2028}
2029
Gilles Peskined7729582019-08-05 15:55:54 +02002030#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2031psa_status_t mbedtls_psa_register_se_key(
2032 const psa_key_attributes_t *attributes )
2033{
2034 psa_status_t status;
2035 psa_key_slot_t *slot = NULL;
2036 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002037 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002038
2039 /* Leaving attributes unspecified is not currently supported.
2040 * It could make sense to query the key type and size from the
2041 * secure element, but not all secure elements support this
2042 * and the driver HAL doesn't currently support it. */
2043 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2044 return( PSA_ERROR_NOT_SUPPORTED );
2045 if( psa_get_key_bits( attributes ) == 0 )
2046 return( PSA_ERROR_NOT_SUPPORTED );
2047
2048 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002049 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002050 if( status != PSA_SUCCESS )
2051 goto exit;
2052
Ronald Cron81709fc2020-11-14 12:10:32 +01002053 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002054
2055exit:
2056 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002057 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002058
Gilles Peskined7729582019-08-05 15:55:54 +02002059 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002060 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002061 return( status );
2062}
2063#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2064
Gilles Peskinef603c712019-01-19 13:40:11 +01002065static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002066 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002067{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002068 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002069 source->key.data,
2070 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002071 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002072 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002073
Steven Cooreman398aee52020-10-13 14:35:45 +02002074 target->attr.type = source->attr.type;
2075 target->attr.bits = source->attr.bits;
2076
2077 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002078}
2079
Ronald Croncf56a0a2020-08-04 09:51:30 +02002080psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002081 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002082 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002083{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002084 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002085 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002086 psa_key_slot_t *source_slot = NULL;
2087 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002088 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002089 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002090
Ronald Cron81709fc2020-11-14 12:10:32 +01002091 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2092
Ronald Cron5c522922020-11-14 16:35:34 +01002093 status = psa_get_and_lock_transparent_key_slot_with_policy(
2094 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002095 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002096 goto exit;
2097
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002098 status = psa_validate_optional_attributes( source_slot,
2099 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002100 if( status != PSA_SUCCESS )
2101 goto exit;
2102
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002103 status = psa_restrict_key_policy( source_slot->attr.type,
2104 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002105 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002106 if( status != PSA_SUCCESS )
2107 goto exit;
2108
Ronald Cron81709fc2020-11-14 12:10:32 +01002109 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2110 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002111 if( status != PSA_SUCCESS )
2112 goto exit;
2113
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002114#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2115 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002116 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002117 /* Copying to a secure element is not implemented yet. */
2118 status = PSA_ERROR_NOT_SUPPORTED;
2119 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002120 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002121#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002122
Ronald Cron6cc66312021-04-02 12:27:47 +02002123 if( psa_key_lifetime_is_external( actual_attributes.core.lifetime ) )
2124 {
2125 /*
2126 * Copying through an opaque driver is not implemented yet, consider
2127 * a lifetime with an external location as an invalid parameter for
2128 * now.
2129 */
2130 status = PSA_ERROR_INVALID_ARGUMENT;
2131 goto exit;
2132 }
2133
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002134 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002135 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002136 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002137
Ronald Cron81709fc2020-11-14 12:10:32 +01002138 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002139exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002140 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002141 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002142
Ronald Cron5c522922020-11-14 16:35:34 +01002143 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002144
Ronald Cron5c522922020-11-14 16:35:34 +01002145 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002146}
2147
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002148
2149
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002150/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002151/* Message digests */
2152/****************************************************************/
2153
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002154psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2155{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002156 /* Aborting a non-active operation is allowed */
2157 if( operation->id == 0 )
2158 return( PSA_SUCCESS );
2159
2160 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2161 operation->id = 0;
2162
2163 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002164}
2165
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002166psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002167 psa_algorithm_t alg )
2168{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002170
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002171 /* A context must be freshly initialized before it can be set up. */
2172 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002173 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002174 status = PSA_ERROR_BAD_STATE;
2175 goto exit;
2176 }
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002177
2178 if( !PSA_ALG_IS_HASH( alg ) )
Dave Rodgmandb861792021-06-24 16:17:43 +01002179 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002180 status = PSA_ERROR_INVALID_ARGUMENT;
2181 goto exit;
2182 }
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002183
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002184 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2185 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002186 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2187
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002188 status = psa_driver_wrapper_hash_setup( operation, alg );
2189
2190exit:
2191 if( status != PSA_SUCCESS )
Dave Rodgmandb861792021-06-24 16:17:43 +01002192 psa_hash_abort( operation );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002193
2194 return status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195}
2196
2197psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2198 const uint8_t *input,
2199 size_t input_length )
2200{
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2202
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002203 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002204 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002205 status = PSA_ERROR_BAD_STATE;
2206 goto exit;
2207 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002208
Steven Cooremanc8288352021-03-04 14:02:19 +01002209 /* Don't require hash implementations to behave correctly on a
2210 * zero-length input, which may have an invalid pointer. */
2211 if( input_length == 0 )
2212 return( PSA_SUCCESS );
2213
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002214 status = psa_driver_wrapper_hash_update( operation, input, input_length );
2215
2216exit:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002217 if( status != PSA_SUCCESS )
2218 psa_hash_abort( operation );
2219
2220 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221}
2222
2223psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2224 uint8_t *hash,
2225 size_t hash_size,
2226 size_t *hash_length )
2227{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002228 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002229 if( operation->id == 0 )
2230 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002231
Steven Cooreman1e582352021-02-18 17:24:37 +01002232 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002233 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002234 psa_hash_abort( operation );
2235 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236}
2237
Gilles Peskine2d277862018-06-18 15:41:12 +02002238psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2239 const uint8_t *hash,
2240 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241{
2242 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2243 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002244 psa_status_t status = psa_hash_finish(
2245 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002246 actual_hash, sizeof( actual_hash ),
2247 &actual_hash_length );
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002248
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249 if( status != PSA_SUCCESS )
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002250 goto exit;
2251
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002252 if( actual_hash_length != hash_length )
Dave Rodgmandb861792021-06-24 16:17:43 +01002253 {
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002254 status = PSA_ERROR_INVALID_SIGNATURE;
2255 goto exit;
2256 }
2257
Steven Cooremana2a1b802021-04-29 16:37:59 +02002258 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Dave Rodgman4e0a82e2021-06-24 11:52:23 +01002259 status = PSA_ERROR_INVALID_SIGNATURE;
2260
2261exit:
2262 if( status != PSA_SUCCESS )
2263 psa_hash_abort(operation);
2264
2265 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266}
2267
Gilles Peskine0a749c82019-11-28 19:33:58 +01002268psa_status_t psa_hash_compute( psa_algorithm_t alg,
2269 const uint8_t *input, size_t input_length,
2270 uint8_t *hash, size_t hash_size,
2271 size_t *hash_length )
2272{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002273 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002274 if( !PSA_ALG_IS_HASH( alg ) )
2275 return( PSA_ERROR_INVALID_ARGUMENT );
2276
Steven Cooreman1e582352021-02-18 17:24:37 +01002277 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2278 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002279}
2280
2281psa_status_t psa_hash_compare( psa_algorithm_t alg,
2282 const uint8_t *input, size_t input_length,
2283 const uint8_t *hash, size_t hash_length )
2284{
Steven Cooreman84d670d2021-02-18 16:22:53 +01002285 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2286 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002287
2288 if( !PSA_ALG_IS_HASH( alg ) )
2289 return( PSA_ERROR_INVALID_ARGUMENT );
2290
Steven Cooreman1e582352021-02-18 17:24:37 +01002291 psa_status_t status = psa_driver_wrapper_hash_compute(
2292 alg, input, input_length,
2293 actual_hash, sizeof(actual_hash),
2294 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002295 if( status != PSA_SUCCESS )
Steven Cooreman84d670d2021-02-18 16:22:53 +01002296 return( status );
2297 if( actual_hash_length != hash_length )
2298 return( PSA_ERROR_INVALID_SIGNATURE );
Steven Cooremana2a1b802021-04-29 16:37:59 +02002299 if( mbedtls_psa_safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
Steven Cooreman84d670d2021-02-18 16:22:53 +01002300 return( PSA_ERROR_INVALID_SIGNATURE );
2301 return( PSA_SUCCESS );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002302}
2303
Gilles Peskineeb35d782019-01-22 17:56:16 +01002304psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2305 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002306{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002307 if( source_operation->id == 0 ||
2308 target_operation->id != 0 )
2309 {
2310 return( PSA_ERROR_BAD_STATE );
2311 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002312
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002313 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2314 target_operation );
2315 if( status != PSA_SUCCESS )
2316 psa_hash_abort( target_operation );
2317
2318 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002319}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320
2321
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002323/* MAC */
2324/****************************************************************/
2325
Steven Cooreman07897832021-03-19 18:28:56 +01002326psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002327{
Steven Cooreman07897832021-03-19 18:28:56 +01002328 /* Aborting a non-active operation is allowed */
2329 if( operation->id == 0 )
2330 return( PSA_SUCCESS );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002331
Steven Cooreman07897832021-03-19 18:28:56 +01002332 psa_status_t status = psa_driver_wrapper_mac_abort( operation );
Steven Cooreman15f0d922021-05-07 14:14:37 +02002333 operation->mac_size = 0;
2334 operation->is_sign = 0;
Steven Cooreman07897832021-03-19 18:28:56 +01002335 operation->id = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002336
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002337 return( status );
2338}
2339
Ronald Cron1c650a12021-06-17 16:33:22 +02002340static psa_status_t psa_mac_finalize_alg_and_key_validation(
2341 psa_algorithm_t alg,
Ronald Crondef68e72021-06-17 16:46:44 +02002342 const psa_key_attributes_t *attributes,
2343 uint8_t *mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002344{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002345 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondef68e72021-06-17 16:46:44 +02002346 psa_key_type_t key_type = psa_get_key_type( attributes );
2347 size_t key_bits = psa_get_key_bits( attributes );
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002348
Ronald Cron48f875e2021-06-17 16:10:24 +02002349 if( ! PSA_ALG_IS_MAC( alg ) )
Ronald Crondef68e72021-06-17 16:46:44 +02002350 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman07897832021-03-19 18:28:56 +01002351
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002352 /* Validate the combination of key type and algorithm */
Ronald Crondef68e72021-06-17 16:46:44 +02002353 status = psa_mac_key_can_do( alg, key_type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002354 if( status != PSA_SUCCESS )
Ronald Crondef68e72021-06-17 16:46:44 +02002355 return( status );
Steven Cooreman15472f82021-03-02 16:16:22 +01002356
Steven Cooremana6474de2021-05-10 11:13:41 +02002357 /* Get the output length for the algorithm and key combination */
Ronald Crondef68e72021-06-17 16:46:44 +02002358 *mac_size = PSA_MAC_LENGTH( key_type, key_bits, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002359
Ronald Crondef68e72021-06-17 16:46:44 +02002360 if( *mac_size < 4 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002361 {
2362 /* A very short MAC is too short for security since it can be
2363 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2364 * so we make this our minimum, even though 32 bits is still
2365 * too small for security. */
Ronald Crondef68e72021-06-17 16:46:44 +02002366 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman15472f82021-03-02 16:16:22 +01002367 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002368
Ronald Crondef68e72021-06-17 16:46:44 +02002369 if( *mac_size > PSA_MAC_LENGTH( key_type, key_bits,
2370 PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002371 {
2372 /* It's impossible to "truncate" to a larger length than the full length
2373 * of the algorithm. */
Ronald Crondef68e72021-06-17 16:46:44 +02002374 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002375 }
2376
Ronald Crondef68e72021-06-17 16:46:44 +02002377 return( PSA_SUCCESS );
Ronald Cron1c650a12021-06-17 16:33:22 +02002378}
2379
2380static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
2381 mbedtls_svc_key_id_t key,
2382 psa_algorithm_t alg,
2383 int is_sign )
2384{
2385 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2386 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002387 psa_key_slot_t *slot = NULL;
Ronald Cron1c650a12021-06-17 16:33:22 +02002388
2389 /* A context must be freshly initialized before it can be set up. */
2390 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002391 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002392 status = PSA_ERROR_BAD_STATE;
2393 goto exit;
2394 }
Ronald Cron1c650a12021-06-17 16:33:22 +02002395
2396 status = psa_get_and_lock_key_slot_with_policy(
2397 key,
2398 &slot,
Mateusz Starzykdd55b252021-08-17 15:24:32 +02002399 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Cron1c650a12021-06-17 16:33:22 +02002400 alg );
2401 if( status != PSA_SUCCESS )
Dave Rodgmancccb05d2021-06-24 11:52:47 +01002402 goto exit;
Ronald Cron1c650a12021-06-17 16:33:22 +02002403
2404 psa_key_attributes_t attributes = {
2405 .core = slot->attr
2406 };
2407
Ronald Crondef68e72021-06-17 16:46:44 +02002408 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2409 &operation->mac_size );
Ronald Cron1c650a12021-06-17 16:33:22 +02002410 if( status != PSA_SUCCESS )
2411 goto exit;
2412
2413 operation->is_sign = is_sign;
Steven Cooreman07897832021-03-19 18:28:56 +01002414 /* Dispatch the MAC setup call with validated input */
2415 if( is_sign )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002416 {
Steven Cooreman07897832021-03-19 18:28:56 +01002417 status = psa_driver_wrapper_mac_sign_setup( operation,
Ronald Cron1c650a12021-06-17 16:33:22 +02002418 &attributes,
Steven Cooreman07897832021-03-19 18:28:56 +01002419 slot->key.data,
2420 slot->key.bytes,
2421 alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002422 }
2423 else
Gilles Peskinefbfac682018-07-08 20:51:54 +02002424 {
Steven Cooreman07897832021-03-19 18:28:56 +01002425 status = psa_driver_wrapper_mac_verify_setup( operation,
Ronald Cron1c650a12021-06-17 16:33:22 +02002426 &attributes,
Steven Cooreman07897832021-03-19 18:28:56 +01002427 slot->key.data,
2428 slot->key.bytes,
2429 alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002430 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002431
Gilles Peskinefbfac682018-07-08 20:51:54 +02002432exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002433 if( status != PSA_SUCCESS )
Steven Cooreman07897832021-03-19 18:28:56 +01002434 psa_mac_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002435
Ronald Cron5c522922020-11-14 16:35:34 +01002436 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002437
Ronald Cron5c522922020-11-14 16:35:34 +01002438 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002439}
2440
Gilles Peskine89167cb2018-07-08 20:12:23 +02002441psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002442 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002443 psa_algorithm_t alg )
2444{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002445 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002446}
2447
2448psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002449 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002450 psa_algorithm_t alg )
2451{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002452 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002453}
2454
Steven Cooreman11743f92021-03-19 18:38:46 +01002455psa_status_t psa_mac_update( psa_mac_operation_t *operation,
Gilles Peskine8c9def32018-02-08 10:02:12 +01002456 const uint8_t *input,
2457 size_t input_length )
2458{
Steven Cooreman11743f92021-03-19 18:38:46 +01002459 if( operation->id == 0 )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002460 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002461
Steven Cooreman11743f92021-03-19 18:38:46 +01002462 /* Don't require hash implementations to behave correctly on a
2463 * zero-length input, which may have an invalid pointer. */
2464 if( input_length == 0 )
2465 return( PSA_SUCCESS );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002466
Steven Cooreman11743f92021-03-19 18:38:46 +01002467 psa_status_t status = psa_driver_wrapper_mac_update( operation,
2468 input, input_length );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002469 if( status != PSA_SUCCESS )
Steven Cooreman11743f92021-03-19 18:38:46 +01002470 psa_mac_abort( operation );
2471
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002472 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002473}
2474
Steven Cooreman87885df2021-03-19 19:04:39 +01002475psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002476 uint8_t *mac,
2477 size_t mac_size,
2478 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002479{
Steven Cooreman87885df2021-03-19 19:04:39 +01002480 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2481 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002482
Steven Cooreman87885df2021-03-19 19:04:39 +01002483 if( operation->id == 0 )
Dave Rodgmand73e1b02021-06-24 16:19:08 +01002484 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002485 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002486 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002487 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002488
Steven Cooreman15f0d922021-05-07 14:14:37 +02002489 if( ! operation->is_sign )
Dave Rodgmandb861792021-06-24 16:17:43 +01002490 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002491 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002492 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002493 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002494
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002495 /* Sanity check. This will guarantee that mac_size != 0 (and so mac != NULL)
2496 * once all the error checks are done. */
2497 if( operation->mac_size == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002498 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002499 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002500 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002501 }
Steven Cooremanf8ad2122021-05-11 11:09:13 +02002502
2503 if( mac_size < operation->mac_size )
Dave Rodgmandb861792021-06-24 16:17:43 +01002504 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002505 status = PSA_ERROR_BUFFER_TOO_SMALL;
Dave Rodgman478ab542021-06-25 09:09:02 +01002506 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002507 }
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002508
Steven Cooreman87885df2021-03-19 19:04:39 +01002509 status = psa_driver_wrapper_mac_sign_finish( operation,
Steven Cooreman15f0d922021-05-07 14:14:37 +02002510 mac, operation->mac_size,
2511 mac_length );
2512
Dave Rodgman478ab542021-06-25 09:09:02 +01002513exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002514 /* In case of success, set the potential excess room in the output buffer
2515 * to an invalid value, to avoid potentially leaking a longer MAC.
2516 * In case of error, set the output length and content to a safe default,
2517 * such that in case the caller misses an error check, the output would be
2518 * an unachievable MAC.
2519 */
2520 if( status != PSA_SUCCESS )
Steven Cooreman15f0d922021-05-07 14:14:37 +02002521 {
Steven Cooreman15f0d922021-05-07 14:14:37 +02002522 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002523 operation->mac_size = 0;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002524 }
mohammad16036df908f2018-04-02 08:34:15 -07002525
Ronald Cron882eb782021-06-18 13:05:48 +02002526 if( mac_size > operation->mac_size )
2527 memset( &mac[operation->mac_size], '!',
2528 mac_size - operation->mac_size );
2529
Steven Cooreman87885df2021-03-19 19:04:39 +01002530 abort_status = psa_mac_abort( operation );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002531
Steven Cooreman87885df2021-03-19 19:04:39 +01002532 return( status == PSA_SUCCESS ? abort_status : status );
mohammad16036df908f2018-04-02 08:34:15 -07002533}
2534
Steven Cooreman87885df2021-03-19 19:04:39 +01002535psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
Gilles Peskineacd4be32018-07-08 19:56:25 +02002536 const uint8_t *mac,
2537 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538{
Steven Cooreman87885df2021-03-19 19:04:39 +01002539 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2540 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01002541
Steven Cooreman87885df2021-03-19 19:04:39 +01002542 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01002543 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002544 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002545 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002546 }
Jaeden Amero252ef282019-02-15 14:05:35 +00002547
Steven Cooreman15f0d922021-05-07 14:14:37 +02002548 if( operation->is_sign )
Dave Rodgmandb861792021-06-24 16:17:43 +01002549 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002550 status = PSA_ERROR_BAD_STATE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002551 goto exit;
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01002552 }
Steven Cooreman15f0d922021-05-07 14:14:37 +02002553
2554 if( operation->mac_size != mac_length )
2555 {
2556 status = PSA_ERROR_INVALID_SIGNATURE;
Dave Rodgman478ab542021-06-25 09:09:02 +01002557 goto exit;
Steven Cooreman15f0d922021-05-07 14:14:37 +02002558 }
2559
Steven Cooreman87885df2021-03-19 19:04:39 +01002560 status = psa_driver_wrapper_mac_verify_finish( operation,
2561 mac, mac_length );
Steven Cooreman15f0d922021-05-07 14:14:37 +02002562
Dave Rodgman478ab542021-06-25 09:09:02 +01002563exit:
Steven Cooreman87885df2021-03-19 19:04:39 +01002564 abort_status = psa_mac_abort( operation );
mohammad16036df908f2018-04-02 08:34:15 -07002565
Steven Cooreman87885df2021-03-19 19:04:39 +01002566 return( status == PSA_SUCCESS ? abort_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002567}
2568
Ronald Cronef0d8f12021-06-18 14:23:33 +02002569static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key,
2570 psa_algorithm_t alg,
2571 const uint8_t *input,
2572 size_t input_length,
2573 uint8_t *mac,
2574 size_t mac_size,
2575 size_t *mac_length,
2576 int is_sign )
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002577{
2578 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crondbb86462021-06-17 17:17:20 +02002579 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2580 psa_key_slot_t *slot;
2581 uint8_t operation_mac_size = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582
Ronald Crondbb86462021-06-17 17:17:20 +02002583 status = psa_get_and_lock_key_slot_with_policy(
Mateusz Starzykdd55b252021-08-17 15:24:32 +02002584 key,
2585 &slot,
2586 is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE,
Ronald Cronef0d8f12021-06-18 14:23:33 +02002587 alg );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002588 if( status != PSA_SUCCESS )
2589 goto exit;
2590
Ronald Crondbb86462021-06-17 17:17:20 +02002591 psa_key_attributes_t attributes = {
2592 .core = slot->attr
2593 };
2594
2595 status = psa_mac_finalize_alg_and_key_validation( alg, &attributes,
2596 &operation_mac_size );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002597 if( status != PSA_SUCCESS )
2598 goto exit;
2599
Ronald Crondbb86462021-06-17 17:17:20 +02002600 if( mac_size < operation_mac_size )
2601 {
2602 status = PSA_ERROR_BUFFER_TOO_SMALL;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002603 goto exit;
Ronald Crondbb86462021-06-17 17:17:20 +02002604 }
2605
2606 status = psa_driver_wrapper_mac_compute(
2607 &attributes,
2608 slot->key.data, slot->key.bytes,
2609 alg,
2610 input, input_length,
2611 mac, operation_mac_size, mac_length );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002612
2613exit:
Ronald Cron882eb782021-06-18 13:05:48 +02002614 /* In case of success, set the potential excess room in the output buffer
2615 * to an invalid value, to avoid potentially leaking a longer MAC.
2616 * In case of error, set the output length and content to a safe default,
2617 * such that in case the caller misses an error check, the output would be
2618 * an unachievable MAC.
2619 */
2620 if( status != PSA_SUCCESS )
Ronald Crondbb86462021-06-17 17:17:20 +02002621 {
Ronald Crondbb86462021-06-17 17:17:20 +02002622 *mac_length = mac_size;
Ronald Cron882eb782021-06-18 13:05:48 +02002623 operation_mac_size = 0;
Ronald Crondbb86462021-06-17 17:17:20 +02002624 }
Ronald Cron882eb782021-06-18 13:05:48 +02002625 if( mac_size > operation_mac_size )
2626 memset( &mac[operation_mac_size], '!', mac_size - operation_mac_size );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002627
Ronald Crondbb86462021-06-17 17:17:20 +02002628 unlock_status = psa_unlock_key_slot( slot );
2629
2630 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002631}
2632
Ronald Cronef0d8f12021-06-18 14:23:33 +02002633psa_status_t psa_mac_compute( mbedtls_svc_key_id_t key,
2634 psa_algorithm_t alg,
2635 const uint8_t *input,
2636 size_t input_length,
2637 uint8_t *mac,
2638 size_t mac_size,
2639 size_t *mac_length)
2640{
2641 return( psa_mac_compute_internal( key, alg,
2642 input, input_length,
2643 mac, mac_size, mac_length, 1 ) );
2644}
2645
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002646psa_status_t psa_mac_verify( mbedtls_svc_key_id_t key,
2647 psa_algorithm_t alg,
2648 const uint8_t *input,
2649 size_t input_length,
2650 const uint8_t *mac,
2651 size_t mac_length)
2652{
2653 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron4ed83682021-06-18 14:51:29 +02002654 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
2655 size_t actual_mac_length;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002656
Ronald Cron4ed83682021-06-18 14:51:29 +02002657 status = psa_mac_compute_internal( key, alg,
2658 input, input_length,
2659 actual_mac, sizeof( actual_mac ),
2660 &actual_mac_length, 0 );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002661 if( status != PSA_SUCCESS )
2662 goto exit;
2663
Ronald Cron4ed83682021-06-18 14:51:29 +02002664 if( mac_length != actual_mac_length )
2665 {
2666 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002667 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002668 }
2669 if( mbedtls_psa_safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
2670 {
2671 status = PSA_ERROR_INVALID_SIGNATURE;
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002672 goto exit;
Ronald Cron4ed83682021-06-18 14:51:29 +02002673 }
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002674
2675exit:
Ronald Cron4ed83682021-06-18 14:51:29 +02002676 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
gabor-mezei-arma00616f2021-03-01 15:34:18 +01002677
2678 return ( status );
2679}
Gilles Peskine20035e32018-02-03 22:44:14 +01002680
Gilles Peskine20035e32018-02-03 22:44:14 +01002681/****************************************************************/
2682/* Asymmetric cryptography */
2683/****************************************************************/
2684
gabor-mezei-armc9795782021-05-12 11:03:09 +02002685static psa_status_t psa_sign_verify_check_alg( int input_is_message,
gabor-mezei-arm56980482021-05-05 13:56:27 +02002686 psa_algorithm_t alg )
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002687{
gabor-mezei-armc9795782021-05-12 11:03:09 +02002688 if( input_is_message )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002689 {
2690 if( ! PSA_ALG_IS_SIGN_MESSAGE( alg ) )
2691 return( PSA_ERROR_INVALID_ARGUMENT );
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002692
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002693 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002694 {
2695 if( ! PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( alg ) ) )
2696 return( PSA_ERROR_INVALID_ARGUMENT );
2697 }
2698 }
2699 else
2700 {
Mateusz Starzykd22362c2021-08-26 11:46:14 +02002701 if( ! PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm56980482021-05-05 13:56:27 +02002702 return( PSA_ERROR_INVALID_ARGUMENT );
2703 }
2704
2705 return( PSA_SUCCESS );
2706}
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002707
2708static psa_status_t psa_sign_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002709 int input_is_message,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002710 psa_algorithm_t alg,
2711 const uint8_t * input,
2712 size_t input_length,
2713 uint8_t * signature,
2714 size_t signature_size,
2715 size_t * signature_length )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002716{
2717 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2718 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2719 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002720
2721 *signature_length = 0;
2722
gabor-mezei-armc9795782021-05-12 11:03:09 +02002723 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm56980482021-05-05 13:56:27 +02002724 if( status != PSA_SUCCESS )
2725 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002726
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002727 /* Immediately reject a zero-length signature buffer. This guarantees
gabor-mezei-arm12ff4d52021-05-05 13:54:55 +02002728 * that signature must be a valid pointer. (On the other hand, the input
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002729 * buffer can in principle be empty since it doesn't actually have
2730 * to be a hash.) */
2731 if( signature_size == 0 )
2732 return( PSA_ERROR_BUFFER_TOO_SMALL );
2733
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002734 status = psa_get_and_lock_key_slot_with_policy(
2735 key, &slot,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002736 input_is_message ? PSA_KEY_USAGE_SIGN_MESSAGE :
2737 PSA_KEY_USAGE_SIGN_HASH,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002738 alg );
2739
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002740 if( status != PSA_SUCCESS )
2741 goto exit;
2742
2743 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
2744 {
2745 status = PSA_ERROR_INVALID_ARGUMENT;
2746 goto exit;
2747 }
2748
2749 psa_key_attributes_t attributes = {
2750 .core = slot->attr
2751 };
2752
gabor-mezei-armc9795782021-05-12 11:03:09 +02002753 if( input_is_message )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002754 {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002755 status = psa_driver_wrapper_sign_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002756 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002757 alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002758 signature, signature_size, signature_length );
2759 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002760 else
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002761 {
2762
2763 status = psa_driver_wrapper_sign_hash(
2764 &attributes, slot->key.data, slot->key.bytes,
2765 alg, input, input_length,
2766 signature, signature_size, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002767 }
2768
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002769
2770exit:
2771 /* Fill the unused part of the output buffer (the whole buffer on error,
2772 * the trailing part on success) with something that isn't a valid signature
2773 * (barring an attack on the signature and deliberately-crafted input),
2774 * in case the caller doesn't check the return status properly. */
2775 if( status == PSA_SUCCESS )
2776 memset( signature + *signature_length, '!',
2777 signature_size - *signature_length );
2778 else
2779 memset( signature, '!', signature_size );
2780 /* If signature_size is 0 then we have nothing to do. We must not call
2781 * memset because signature may be NULL in this case. */
2782
2783 unlock_status = psa_unlock_key_slot( slot );
2784
2785 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
2786}
2787
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002788static psa_status_t psa_verify_internal( mbedtls_svc_key_id_t key,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002789 int input_is_message,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002790 psa_algorithm_t alg,
2791 const uint8_t * input,
2792 size_t input_length,
2793 const uint8_t * signature,
2794 size_t signature_length )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002795{
2796 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2797 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
2798 psa_key_slot_t *slot;
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002799
gabor-mezei-armc9795782021-05-12 11:03:09 +02002800 status = psa_sign_verify_check_alg( input_is_message, alg );
gabor-mezei-arm56980482021-05-05 13:56:27 +02002801 if( status != PSA_SUCCESS )
2802 return status;
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002803
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002804 status = psa_get_and_lock_key_slot_with_policy(
2805 key, &slot,
gabor-mezei-armc9795782021-05-12 11:03:09 +02002806 input_is_message ? PSA_KEY_USAGE_VERIFY_MESSAGE :
2807 PSA_KEY_USAGE_VERIFY_HASH,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002808 alg );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002809
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002810 if( status != PSA_SUCCESS )
2811 return( status );
2812
2813 psa_key_attributes_t attributes = {
2814 .core = slot->attr
2815 };
2816
gabor-mezei-armc9795782021-05-12 11:03:09 +02002817 if( input_is_message )
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002818 {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002819 status = psa_driver_wrapper_verify_message(
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002820 &attributes, slot->key.data, slot->key.bytes,
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002821 alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002822 signature, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002823 }
gabor-mezei-arm56980482021-05-05 13:56:27 +02002824 else
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002825 {
2826 status = psa_driver_wrapper_verify_hash(
2827 &attributes, slot->key.data, slot->key.bytes,
2828 alg, input, input_length,
2829 signature, signature_length );
2830 }
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002831
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002832 unlock_status = psa_unlock_key_slot( slot );
2833
2834 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002835
2836}
2837
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002838psa_status_t psa_sign_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002839 const psa_key_attributes_t *attributes,
2840 const uint8_t *key_buffer,
2841 size_t key_buffer_size,
2842 psa_algorithm_t alg,
2843 const uint8_t *input,
2844 size_t input_length,
2845 uint8_t *signature,
2846 size_t signature_size,
2847 size_t *signature_length )
2848{
2849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002850
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002851 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002852 {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002853 size_t hash_length;
2854 uint8_t hash[PSA_HASH_MAX_SIZE];
2855
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002856 status = psa_driver_wrapper_hash_compute(
2857 PSA_ALG_SIGN_GET_HASH( alg ),
2858 input, input_length,
2859 hash, sizeof( hash ), &hash_length );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002860
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002861 if( status != PSA_SUCCESS )
2862 return status;
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002863
2864 return psa_driver_wrapper_sign_hash(
2865 attributes, key_buffer, key_buffer_size,
2866 alg, hash, hash_length,
2867 signature, signature_size, signature_length );
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002868 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002869
gabor-mezei-arm56980482021-05-05 13:56:27 +02002870 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002871}
2872
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002873psa_status_t psa_sign_message( mbedtls_svc_key_id_t key,
2874 psa_algorithm_t alg,
2875 const uint8_t * input,
2876 size_t input_length,
2877 uint8_t * signature,
2878 size_t signature_size,
2879 size_t * signature_length )
2880{
2881 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002882 key, 1, alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002883 signature, signature_size, signature_length );
2884}
2885
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002886psa_status_t psa_verify_message_builtin(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002887 const psa_key_attributes_t *attributes,
2888 const uint8_t *key_buffer,
2889 size_t key_buffer_size,
2890 psa_algorithm_t alg,
2891 const uint8_t *input,
2892 size_t input_length,
2893 const uint8_t *signature,
2894 size_t signature_length )
2895{
2896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002897
Gilles Peskine8cb22c82021-09-22 16:15:05 +02002898 if ( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002899 {
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002900 size_t hash_length;
2901 uint8_t hash[PSA_HASH_MAX_SIZE];
2902
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002903 status = psa_driver_wrapper_hash_compute(
2904 PSA_ALG_SIGN_GET_HASH( alg ),
2905 input, input_length,
2906 hash, sizeof( hash ), &hash_length );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002907
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002908 if( status != PSA_SUCCESS )
2909 return status;
gabor-mezei-arm9719a842021-05-03 16:26:20 +02002910
2911 return psa_driver_wrapper_verify_hash(
2912 attributes, key_buffer, key_buffer_size,
2913 alg, hash, hash_length,
2914 signature, signature_length );
gabor-mezei-armbfbe4652021-04-29 16:48:44 +02002915 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002916
gabor-mezei-armef6f2aa2021-05-05 13:59:01 +02002917 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +02002918}
2919
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002920psa_status_t psa_verify_message( mbedtls_svc_key_id_t key,
2921 psa_algorithm_t alg,
2922 const uint8_t * input,
2923 size_t input_length,
2924 const uint8_t * signature,
2925 size_t signature_length )
2926{
2927 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002928 key, 1, alg, input, input_length,
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002929 signature, signature_length );
gabor-mezei-arme8efa392021-04-14 21:14:28 +02002930}
2931
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02002932psa_status_t psa_sign_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01002933 const psa_key_attributes_t *attributes,
2934 const uint8_t *key_buffer, size_t key_buffer_size,
2935 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2936 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002937{
Ronald Cron99b8ed72021-02-17 10:33:32 +01002938 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002939 {
Ronald Cron4501c982021-03-19 20:09:32 +01002940 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
2941 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002942 {
Ronald Cron4501c982021-03-19 20:09:32 +01002943#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2944 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
2945 return( mbedtls_psa_rsa_sign_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01002946 attributes,
2947 key_buffer, key_buffer_size,
2948 alg, hash, hash_length,
2949 signature, signature_size, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01002950#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2951 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02002952 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002953 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002954 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002955 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002956 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002957 }
2958 else
Ronald Cron4501c982021-03-19 20:09:32 +01002959 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
2960 {
2961 if( PSA_ALG_IS_ECDSA( alg ) )
2962 {
2963#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2964 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
2965 return( mbedtls_psa_ecdsa_sign_hash(
2966 attributes,
2967 key_buffer, key_buffer_size,
2968 alg, hash, hash_length,
2969 signature, signature_size, signature_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002970#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2971 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Ronald Cron4501c982021-03-19 20:09:32 +01002972 }
2973 else
2974 {
2975 return( PSA_ERROR_INVALID_ARGUMENT );
2976 }
Ronald Cron8a494f32021-02-17 09:49:51 +01002977 }
Ronald Cron4501c982021-03-19 20:09:32 +01002978
2979 (void)key_buffer;
2980 (void)key_buffer_size;
2981 (void)hash;
2982 (void)hash_length;
2983 (void)signature;
2984 (void)signature_size;
2985 (void)signature_length;
2986
2987 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01002988}
Gilles Peskinee59236f2018-01-27 23:32:46 +01002989
2990psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
2991 psa_algorithm_t alg,
2992 const uint8_t *hash,
2993 size_t hash_length,
2994 uint8_t *signature,
2995 size_t signature_size,
2996 size_t *signature_length )
2997{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02002998 return psa_sign_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02002999 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003000 signature, signature_size, signature_length );
itayzafrir5c753392018-05-08 11:18:38 +03003001}
3002
gabor-mezei-armf3c5c862021-05-05 14:18:36 +02003003psa_status_t psa_verify_hash_builtin(
Ronald Cron18659932020-12-08 16:32:23 +01003004 const psa_key_attributes_t *attributes,
3005 const uint8_t *key_buffer, size_t key_buffer_size,
3006 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
3007 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003008{
Ronald Cron99b8ed72021-02-17 10:33:32 +01003009 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003010 {
Ronald Cron4501c982021-03-19 20:09:32 +01003011 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) ||
3012 PSA_ALG_IS_RSA_PSS( alg) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003013 {
Ronald Cron4501c982021-03-19 20:09:32 +01003014#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3015 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
3016 return( mbedtls_psa_rsa_verify_hash(
Ronald Cron072722c2020-12-09 16:36:19 +01003017 attributes,
3018 key_buffer, key_buffer_size,
3019 alg, hash, hash_length,
3020 signature, signature_length ) );
Ronald Cron4501c982021-03-19 20:09:32 +01003021#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3022 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Steven Cooremanacda8342020-07-24 23:09:52 +02003023 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003024 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003025 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003026 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003027 }
itayzafrir5c753392018-05-08 11:18:38 +03003028 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01003029 else
Ronald Cron4501c982021-03-19 20:09:32 +01003030 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003031 {
Ronald Cron4501c982021-03-19 20:09:32 +01003032 if( PSA_ALG_IS_ECDSA( alg ) )
3033 {
3034#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3035 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
3036 return( mbedtls_psa_ecdsa_verify_hash(
3037 attributes,
3038 key_buffer, key_buffer_size,
3039 alg, hash, hash_length,
3040 signature, signature_length ) );
3041#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3042 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
3043 }
3044 else
3045 {
3046 return( PSA_ERROR_INVALID_ARGUMENT );
3047 }
Ronald Cron8a494f32021-02-17 09:49:51 +01003048 }
Ronald Cron4501c982021-03-19 20:09:32 +01003049
3050 (void)key_buffer;
3051 (void)key_buffer_size;
3052 (void)hash;
3053 (void)hash_length;
3054 (void)signature;
3055 (void)signature_length;
3056
3057 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron18659932020-12-08 16:32:23 +01003058}
3059
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003060psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3061 psa_algorithm_t alg,
3062 const uint8_t *hash,
3063 size_t hash_length,
3064 const uint8_t *signature,
3065 size_t signature_length )
3066{
gabor-mezei-armbc0088b2021-04-20 12:11:35 +02003067 return psa_verify_internal(
gabor-mezei-arm56980482021-05-05 13:56:27 +02003068 key, 0, alg, hash, hash_length,
Ronald Cron9f17aa42020-12-08 17:07:25 +01003069 signature, signature_length );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003070}
3071
John Durkop0e005192020-10-31 22:06:54 -07003072#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003073static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3074 mbedtls_rsa_context *rsa )
3075{
3076 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3077 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3078 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3079 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3080}
John Durkop0e005192020-10-31 22:06:54 -07003081#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003082
Ronald Croncf56a0a2020-08-04 09:51:30 +02003083psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003084 psa_algorithm_t alg,
3085 const uint8_t *input,
3086 size_t input_length,
3087 const uint8_t *salt,
3088 size_t salt_length,
3089 uint8_t *output,
3090 size_t output_size,
3091 size_t *output_length )
3092{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003094 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003095 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003096
Darryl Green5cc689a2018-07-24 15:34:10 +01003097 (void) input;
3098 (void) input_length;
3099 (void) salt;
3100 (void) output;
3101 (void) output_size;
3102
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003103 *output_length = 0;
3104
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003105 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3106 return( PSA_ERROR_INVALID_ARGUMENT );
3107
Ronald Cron5c522922020-11-14 16:35:34 +01003108 status = psa_get_and_lock_transparent_key_slot_with_policy(
3109 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003110 if( status != PSA_SUCCESS )
3111 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003112 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3113 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003114 {
3115 status = PSA_ERROR_INVALID_ARGUMENT;
3116 goto exit;
3117 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118
John Durkop6ba40d12020-11-10 08:50:04 -08003119#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3120 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003121 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003122 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003123 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003124 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3125 slot->key.data,
3126 slot->key.bytes,
3127 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003128 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003129 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003130
3131 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003132 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003133 status = PSA_ERROR_BUFFER_TOO_SMALL;
3134 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003135 }
John Durkop0e005192020-10-31 22:06:54 -07003136#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
3138 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003139 status = mbedtls_to_psa_error(
3140 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003141 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003142 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003143 MBEDTLS_RSA_PUBLIC,
3144 input_length,
3145 input,
3146 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147 }
3148 else
John Durkop0e005192020-10-31 22:06:54 -07003149#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3150#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151 if( PSA_ALG_IS_RSA_OAEP( alg ) )
3152 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003153 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003154 status = mbedtls_to_psa_error(
3155 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003156 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003157 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003158 MBEDTLS_RSA_PUBLIC,
3159 salt, salt_length,
3160 input_length,
3161 input,
3162 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003163 }
3164 else
John Durkop0e005192020-10-31 22:06:54 -07003165#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003167 status = PSA_ERROR_INVALID_ARGUMENT;
3168 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003169 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003170rsa_exit:
3171 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003172 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003173
Steven Cooremana2371e52020-07-28 14:30:39 +02003174 mbedtls_rsa_free( rsa );
3175 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003176 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177 else
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) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003181 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003183
3184exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003185 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003186
Ronald Cron5c522922020-11-14 16:35:34 +01003187 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003188}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003189
Ronald Croncf56a0a2020-08-04 09:51:30 +02003190psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003191 psa_algorithm_t alg,
3192 const uint8_t *input,
3193 size_t input_length,
3194 const uint8_t *salt,
3195 size_t salt_length,
3196 uint8_t *output,
3197 size_t output_size,
3198 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003200 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003201 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003202 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003203
Darryl Green5cc689a2018-07-24 15:34:10 +01003204 (void) input;
3205 (void) input_length;
3206 (void) salt;
3207 (void) output;
3208 (void) output_size;
3209
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003210 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003212 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3213 return( PSA_ERROR_INVALID_ARGUMENT );
3214
Ronald Cron5c522922020-11-14 16:35:34 +01003215 status = psa_get_and_lock_transparent_key_slot_with_policy(
3216 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003217 if( status != PSA_SUCCESS )
3218 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003219 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003220 {
3221 status = PSA_ERROR_INVALID_ARGUMENT;
3222 goto exit;
3223 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224
John Durkop6ba40d12020-11-10 08:50:04 -08003225#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3226 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
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 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003229 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003230 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3231 slot->key.data,
3232 slot->key.bytes,
3233 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003234 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003235 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003236
Steven Cooremana2371e52020-07-28 14:30:39 +02003237 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003238 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003239 status = PSA_ERROR_INVALID_ARGUMENT;
3240 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003241 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242
John Durkop0e005192020-10-31 22:06:54 -07003243#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003244 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003246 status = mbedtls_to_psa_error(
3247 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003248 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003249 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003250 MBEDTLS_RSA_PRIVATE,
3251 output_length,
3252 input,
3253 output,
3254 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003255 }
3256 else
John Durkop0e005192020-10-31 22:06:54 -07003257#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3258#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003259 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003260 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003261 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003262 status = mbedtls_to_psa_error(
3263 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003264 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003265 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003266 MBEDTLS_RSA_PRIVATE,
3267 salt, salt_length,
3268 output_length,
3269 input,
3270 output,
3271 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003272 }
3273 else
John Durkop0e005192020-10-31 22:06:54 -07003274#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003275 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003276 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003278
Steven Cooreman4fed4552020-08-03 14:46:03 +02003279rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003280 mbedtls_rsa_free( rsa );
3281 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003282 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283 else
John Durkop6ba40d12020-11-10 08:50:04 -08003284#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3285 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003286 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003287 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003289
3290exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003291 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003292
Ronald Cron5c522922020-11-14 16:35:34 +01003293 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003294}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003295
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003296
3297
mohammad1603503973b2018-03-12 15:59:30 +02003298/****************************************************************/
3299/* Symmetric cryptography */
3300/****************************************************************/
3301
Ronald Cronab99ac22020-10-01 16:38:28 +02003302static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
3303 mbedtls_svc_key_id_t key,
3304 psa_algorithm_t alg,
3305 mbedtls_operation_t cipher_operation )
3306{
3307 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3308 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003309 psa_key_slot_t *slot = NULL;
Ronald Cronab99ac22020-10-01 16:38:28 +02003310 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3311 PSA_KEY_USAGE_ENCRYPT :
3312 PSA_KEY_USAGE_DECRYPT );
3313
3314 /* A context must be freshly initialized before it can be set up. */
Ronald Cron49fafa92021-03-10 08:34:23 +01003315 if( operation->id != 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01003316 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003317 status = PSA_ERROR_BAD_STATE;
3318 goto exit;
3319 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003320
Ronald Cronab99ac22020-10-01 16:38:28 +02003321 if( ! PSA_ALG_IS_CIPHER( alg ) )
Dave Rodgmandb861792021-06-24 16:17:43 +01003322 {
Dave Rodgmancccb05d2021-06-24 11:52:47 +01003323 status = PSA_ERROR_INVALID_ARGUMENT;
3324 goto exit;
3325 }
Ronald Cronab99ac22020-10-01 16:38:28 +02003326
Ronald Cronab99ac22020-10-01 16:38:28 +02003327 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
3328 if( status != PSA_SUCCESS )
3329 goto exit;
3330
Ronald Cron49fafa92021-03-10 08:34:23 +01003331 /* Initialize the operation struct members, except for id. The id member
Ronald Cronab99ac22020-10-01 16:38:28 +02003332 * is used to indicate to psa_cipher_abort that there are resources to free,
Ronald Cron49fafa92021-03-10 08:34:23 +01003333 * so we only set it (in the driver wrapper) after resources have been
3334 * allocated/initialized. */
Ronald Cronab99ac22020-10-01 16:38:28 +02003335 operation->iv_set = 0;
Ronald Cronab99ac22020-10-01 16:38:28 +02003336 if( alg == PSA_ALG_ECB_NO_PADDING )
3337 operation->iv_required = 0;
3338 else
3339 operation->iv_required = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003340 operation->default_iv_length = PSA_CIPHER_IV_LENGTH( slot->attr.type, alg );
Ronald Cronab99ac22020-10-01 16:38:28 +02003341
Ronald Crona4af55f2020-12-14 14:36:06 +01003342 psa_key_attributes_t attributes = {
3343 .core = slot->attr
3344 };
3345
Ronald Cronab99ac22020-10-01 16:38:28 +02003346 /* Try doing the operation through a driver before using software fallback. */
3347 if( cipher_operation == MBEDTLS_ENCRYPT )
Ronald Crona4af55f2020-12-14 14:36:06 +01003348 status = psa_driver_wrapper_cipher_encrypt_setup( operation,
3349 &attributes,
3350 slot->key.data,
3351 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003352 alg );
3353 else
Ronald Crona4af55f2020-12-14 14:36:06 +01003354 status = psa_driver_wrapper_cipher_decrypt_setup( operation,
3355 &attributes,
3356 slot->key.data,
3357 slot->key.bytes,
Ronald Cronab99ac22020-10-01 16:38:28 +02003358 alg );
3359
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003360exit:
Ronald Cron06aa4422021-03-09 17:32:57 +01003361 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003362 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003363
Ronald Cron5c522922020-11-14 16:35:34 +01003364 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003365
Ronald Cron5c522922020-11-14 16:35:34 +01003366 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003367}
3368
Gilles Peskinefe119512018-07-08 21:39:34 +02003369psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003370 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003371 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003372{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003373 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003374}
3375
Gilles Peskinefe119512018-07-08 21:39:34 +02003376psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003377 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003378 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003379{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003380 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003381}
3382
Gilles Peskinefe119512018-07-08 21:39:34 +02003383psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003384 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003385 size_t iv_size,
3386 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003387{
Ronald Cron6d051732020-10-01 14:10:20 +02003388 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003389
Ronald Cron5618a392021-03-26 09:52:26 +01003390 *iv_length = 0;
3391
Ronald Cron49fafa92021-03-10 08:34:23 +01003392 if( operation->id == 0 )
Ronald Cronc45b4af2020-09-29 16:18:05 +02003393 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003394 status = PSA_ERROR_BAD_STATE;
3395 goto exit;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003396 }
3397
Steven Cooreman7df02922020-09-09 15:28:49 +02003398 if( operation->iv_set || ! operation->iv_required )
3399 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003400 status = PSA_ERROR_BAD_STATE;
3401 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003402 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003403
Ronald Cron5618a392021-03-26 09:52:26 +01003404 if( iv_size < operation->default_iv_length )
3405 {
3406 status = PSA_ERROR_BUFFER_TOO_SMALL;
3407 goto exit;
3408 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003409
Ronald Cron5618a392021-03-26 09:52:26 +01003410 status = psa_generate_random( iv, operation->default_iv_length );
3411 if( status != PSA_SUCCESS )
3412 goto exit;
3413
3414 status = psa_driver_wrapper_cipher_set_iv( operation,
3415 iv,
3416 operation->default_iv_length );
3417
3418exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003419 if( status == PSA_SUCCESS )
Ronald Cron5618a392021-03-26 09:52:26 +01003420 {
Steven Cooreman7df02922020-09-09 15:28:49 +02003421 operation->iv_set = 1;
Ronald Cron5618a392021-03-26 09:52:26 +01003422 *iv_length = operation->default_iv_length;
3423 }
Steven Cooreman7df02922020-09-09 15:28:49 +02003424 else
Moran Peker395db872018-05-31 14:07:14 +03003425 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003426
itayzafrir534bd7c2018-08-02 13:56:32 +03003427 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003428}
3429
Gilles Peskinefe119512018-07-08 21:39:34 +02003430psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003431 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003432 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003433{
Ronald Cron6d051732020-10-01 14:10:20 +02003434 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronc45b4af2020-09-29 16:18:05 +02003435
Ronald Cron49fafa92021-03-10 08:34:23 +01003436 if( operation->id == 0 )
Dave Rodgmandb861792021-06-24 16:17:43 +01003437 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003438 status = PSA_ERROR_BAD_STATE;
3439 goto exit;
3440 }
Ronald Cronc45b4af2020-09-29 16:18:05 +02003441
Steven Cooreman7df02922020-09-09 15:28:49 +02003442 if( operation->iv_set || ! operation->iv_required )
Dave Rodgmandb861792021-06-24 16:17:43 +01003443 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003444 status = PSA_ERROR_BAD_STATE;
3445 goto exit;
3446 }
Ronald Crona0d68172021-03-26 10:15:08 +01003447
3448 if( iv_length > PSA_CIPHER_IV_MAX_SIZE )
Dave Rodgmandb861792021-06-24 16:17:43 +01003449 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003450 status = PSA_ERROR_INVALID_ARGUMENT;
3451 goto exit;
3452 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003453
Ronald Crondd24c9b2020-12-15 14:10:01 +01003454 status = psa_driver_wrapper_cipher_set_iv( operation,
3455 iv,
3456 iv_length );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003457
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003458exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003459 if( status == PSA_SUCCESS )
3460 operation->iv_set = 1;
3461 else
mohammad1603503973b2018-03-12 15:59:30 +02003462 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003463 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003464}
3465
Gilles Peskinee553c652018-06-04 16:22:46 +02003466psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3467 const uint8_t *input,
3468 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003469 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003470 size_t output_size,
3471 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003472{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron6d051732020-10-01 14:10:20 +02003474
Ronald Cron49fafa92021-03-10 08:34:23 +01003475 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003476 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003477 status = PSA_ERROR_BAD_STATE;
3478 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003479 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003480
Steven Cooreman7df02922020-09-09 15:28:49 +02003481 if( operation->iv_required && ! operation->iv_set )
3482 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003483 status = PSA_ERROR_BAD_STATE;
3484 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003485 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003486
Ronald Crondd24c9b2020-12-15 14:10:01 +01003487 status = psa_driver_wrapper_cipher_update( operation,
3488 input,
3489 input_length,
3490 output,
3491 output_size,
3492 output_length );
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003493
3494exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003495 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003496 psa_cipher_abort( operation );
Ronald Cron6d051732020-10-01 14:10:20 +02003497
itayzafrir534bd7c2018-08-02 13:56:32 +03003498 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003499}
3500
Gilles Peskinee553c652018-06-04 16:22:46 +02003501psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3502 uint8_t *output,
3503 size_t output_size,
3504 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003505{
David Saadab4ecc272019-02-14 13:48:10 +02003506 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron6d051732020-10-01 14:10:20 +02003507
Ronald Cron49fafa92021-03-10 08:34:23 +01003508 if( operation->id == 0 )
Steven Cooreman7df02922020-09-09 15:28:49 +02003509 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003510 status = PSA_ERROR_BAD_STATE;
3511 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003512 }
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003513
Steven Cooreman7df02922020-09-09 15:28:49 +02003514 if( operation->iv_required && ! operation->iv_set )
3515 {
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003516 status = PSA_ERROR_BAD_STATE;
3517 goto exit;
Steven Cooreman7df02922020-09-09 15:28:49 +02003518 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003519
Ronald Crondd24c9b2020-12-15 14:10:01 +01003520 status = psa_driver_wrapper_cipher_finish( operation,
3521 output,
3522 output_size,
3523 output_length );
Dave Rodgmanc88b0a52021-06-23 11:38:39 +01003524
3525exit:
Steven Cooremanef8575e2020-09-11 11:44:50 +02003526 if( status == PSA_SUCCESS )
3527 return( psa_cipher_abort( operation ) );
3528 else
3529 {
3530 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003531 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003532
Steven Cooremanef8575e2020-09-11 11:44:50 +02003533 return( status );
3534 }
mohammad1603503973b2018-03-12 15:59:30 +02003535}
3536
Gilles Peskinee553c652018-06-04 16:22:46 +02003537psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3538{
Ronald Cron49fafa92021-03-10 08:34:23 +01003539 if( operation->id == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003540 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003541 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003542 * in use. It's ok to call abort on such an object, and there's
3543 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003544 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003545 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003546
Ronald Crondd24c9b2020-12-15 14:10:01 +01003547 psa_driver_wrapper_cipher_abort( operation );
Gilles Peskinee553c652018-06-04 16:22:46 +02003548
Ronald Cron49fafa92021-03-10 08:34:23 +01003549 operation->id = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003550 operation->iv_set = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003551 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003552
Moran Peker395db872018-05-31 14:07:14 +03003553 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003554}
3555
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003556psa_status_t psa_cipher_encrypt( mbedtls_svc_key_id_t key,
3557 psa_algorithm_t alg,
3558 const uint8_t *input,
3559 size_t input_length,
3560 uint8_t *output,
3561 size_t output_size,
3562 size_t *output_length )
3563{
3564 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003565 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3566 psa_key_slot_t *slot;
3567 psa_key_type_t key_type;
3568 size_t iv_length;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003569
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003570 *output_length = 0;
3571
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003572 if( ! PSA_ALG_IS_CIPHER( alg ) )
3573 return( PSA_ERROR_INVALID_ARGUMENT );
3574
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003575 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3576 PSA_KEY_USAGE_ENCRYPT,
3577 alg );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003578 if( status != PSA_SUCCESS )
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003579 return( status );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003580
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003581 psa_key_attributes_t attributes = {
3582 .core = slot->attr
3583 };
3584
3585 key_type = slot->attr.type;
3586 iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg );
3587
3588 if( iv_length > 0 )
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003589 {
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003590 if( output_size < iv_length )
3591 {
3592 status = PSA_ERROR_BUFFER_TOO_SMALL;
3593 goto exit;
3594 }
3595
3596 status = psa_generate_random( output, iv_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003597 if( status != PSA_SUCCESS )
3598 goto exit;
3599 }
3600
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003601 status = psa_driver_wrapper_cipher_encrypt(
3602 &attributes, slot->key.data, slot->key.bytes,
3603 alg, input, input_length,
3604 output, output_size, output_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003605
3606exit:
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003607 unlock_status = psa_unlock_key_slot( slot );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003608
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003609 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003610}
3611
3612psa_status_t psa_cipher_decrypt( mbedtls_svc_key_id_t key,
3613 psa_algorithm_t alg,
3614 const uint8_t *input,
3615 size_t input_length,
3616 uint8_t *output,
3617 size_t output_size,
3618 size_t *output_length )
3619{
3620 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003621 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3622 psa_key_slot_t *slot;
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003623
gabor-mezei-arm52ae8712021-06-25 15:21:11 +02003624 *output_length = 0;
3625
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003626 if( ! PSA_ALG_IS_CIPHER( alg ) )
3627 return( PSA_ERROR_INVALID_ARGUMENT );
3628
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003629 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3630 PSA_KEY_USAGE_DECRYPT,
3631 alg );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003632 if( status != PSA_SUCCESS )
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003633 return( status );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003634
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003635 psa_key_attributes_t attributes = {
3636 .core = slot->attr
3637 };
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003638
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003639 if( input_length < PSA_CIPHER_IV_LENGTH( slot->attr.type, alg ) )
3640 {
3641 status = PSA_ERROR_INVALID_ARGUMENT;
3642 goto exit;
3643 }
3644
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003645 status = psa_driver_wrapper_cipher_decrypt(
3646 &attributes, slot->key.data, slot->key.bytes,
3647 alg, input, input_length,
3648 output, output_size, output_length );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003649
gabor-mezei-arm3fd792d2021-06-25 15:25:38 +02003650exit:
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003651 unlock_status = psa_unlock_key_slot( slot );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003652
gabor-mezei-armfa990b52021-03-25 11:17:10 +01003653 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
gabor-mezei-arm0ef3b852021-03-01 15:04:24 +01003654}
3655
3656
mohammad16035955c982018-04-26 00:53:03 +03003657/****************************************************************/
3658/* AEAD */
3659/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003660
Ronald Croncf56a0a2020-08-04 09:51:30 +02003661psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003662 psa_algorithm_t alg,
3663 const uint8_t *nonce,
3664 size_t nonce_length,
3665 const uint8_t *additional_data,
3666 size_t additional_data_length,
3667 const uint8_t *plaintext,
3668 size_t plaintext_length,
3669 uint8_t *ciphertext,
3670 size_t ciphertext_size,
3671 size_t *ciphertext_length )
3672{
Ronald Cron215633c2021-03-16 17:15:37 +01003673 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3674 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003675
mohammad1603f08a5502018-06-03 15:05:47 +03003676 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003677
Steven Cooremanea7ab132021-03-17 16:28:00 +01003678 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3679 return( PSA_ERROR_NOT_SUPPORTED );
3680
Ronald Cron9a986162021-03-26 12:40:07 +01003681 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003682 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003683 if( status != PSA_SUCCESS )
3684 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003685
Ronald Cron215633c2021-03-16 17:15:37 +01003686 psa_key_attributes_t attributes = {
3687 .core = slot->attr
3688 };
mohammad16035955c982018-04-26 00:53:03 +03003689
Ronald Cronde822812021-03-17 16:08:20 +01003690 status = psa_driver_wrapper_aead_encrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003691 &attributes, slot->key.data, slot->key.bytes,
3692 alg,
3693 nonce, nonce_length,
3694 additional_data, additional_data_length,
3695 plaintext, plaintext_length,
3696 ciphertext, ciphertext_size, ciphertext_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003697
Gilles Peskineedf9a652018-08-17 18:11:56 +02003698 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3699 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003700
Ronald Cron9f310172021-03-16 16:36:37 +01003701 psa_unlock_key_slot( slot );
mohammad16035955c982018-04-26 00:53:03 +03003702
mohammad16035955c982018-04-26 00:53:03 +03003703 return( status );
Gilles Peskineee652a32018-06-01 19:23:52 +02003704}
3705
Ronald Croncf56a0a2020-08-04 09:51:30 +02003706psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03003707 psa_algorithm_t alg,
3708 const uint8_t *nonce,
3709 size_t nonce_length,
3710 const uint8_t *additional_data,
3711 size_t additional_data_length,
3712 const uint8_t *ciphertext,
3713 size_t ciphertext_length,
3714 uint8_t *plaintext,
3715 size_t plaintext_size,
3716 size_t *plaintext_length )
3717{
Ronald Cron215633c2021-03-16 17:15:37 +01003718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3719 psa_key_slot_t *slot;
Gilles Peskine2d277862018-06-18 15:41:12 +02003720
Gilles Peskineee652a32018-06-01 19:23:52 +02003721 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003722
Steven Cooremanea7ab132021-03-17 16:28:00 +01003723 if( !PSA_ALG_IS_AEAD( alg ) || PSA_ALG_IS_WILDCARD( alg ) )
3724 return( PSA_ERROR_NOT_SUPPORTED );
3725
Ronald Cron9a986162021-03-26 12:40:07 +01003726 status = psa_get_and_lock_key_slot_with_policy(
Ronald Cron215633c2021-03-16 17:15:37 +01003727 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003728 if( status != PSA_SUCCESS )
3729 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003730
Ronald Cron215633c2021-03-16 17:15:37 +01003731 psa_key_attributes_t attributes = {
3732 .core = slot->attr
3733 };
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003734
Ronald Cronde822812021-03-17 16:08:20 +01003735 status = psa_driver_wrapper_aead_decrypt(
Ronald Cron215633c2021-03-16 17:15:37 +01003736 &attributes, slot->key.data, slot->key.bytes,
3737 alg,
3738 nonce, nonce_length,
3739 additional_data, additional_data_length,
3740 ciphertext, ciphertext_length,
3741 plaintext, plaintext_size, plaintext_length );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003742
Gilles Peskineedf9a652018-08-17 18:11:56 +02003743 if( status != PSA_SUCCESS && plaintext_size != 0 )
3744 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003745
Ronald Cron215633c2021-03-16 17:15:37 +01003746 psa_unlock_key_slot( slot );
3747
Gilles Peskineedf9a652018-08-17 18:11:56 +02003748 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003749}
3750
Gilles Peskinee59236f2018-01-27 23:32:46 +01003751/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003752/* Generators */
3753/****************************************************************/
3754
John Durkop07cc04a2020-11-16 22:08:34 -08003755#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3756 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3757 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3758#define AT_LEAST_ONE_BUILTIN_KDF
Steven Cooreman9878a162021-05-06 17:58:36 +02003759#endif /* At least one builtin KDF */
3760
3761#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
3762 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3763 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3764static psa_status_t psa_key_derivation_start_hmac(
3765 psa_mac_operation_t *operation,
3766 psa_algorithm_t hash_alg,
3767 const uint8_t *hmac_key,
3768 size_t hmac_key_length )
3769{
3770 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3772 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
3773 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( hmac_key_length ) );
3774 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
3775
Steven Cooreman15f0d922021-05-07 14:14:37 +02003776 operation->is_sign = 1;
3777 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
3778
Steven Cooreman9878a162021-05-06 17:58:36 +02003779 status = psa_driver_wrapper_mac_sign_setup( operation,
3780 &attributes,
3781 hmac_key, hmac_key_length,
3782 PSA_ALG_HMAC( hash_alg ) );
3783
3784 psa_reset_key_attributes( &attributes );
3785 return( status );
3786}
3787#endif /* KDF algorithms reliant on HMAC */
John Durkop07cc04a2020-11-16 22:08:34 -08003788
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003789#define HKDF_STATE_INIT 0 /* no input yet */
3790#define HKDF_STATE_STARTED 1 /* got salt */
3791#define HKDF_STATE_KEYED 2 /* got key */
3792#define HKDF_STATE_OUTPUT 3 /* output started */
3793
Gilles Peskinecbe66502019-05-16 16:59:18 +02003794static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003795 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01003796{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003797 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
3798 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003799 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003800 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003801}
3802
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003803psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003804{
3805 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003806 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01003807 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003808 {
3809 /* The object has (apparently) been initialized but it is not
3810 * in use. It's ok to call abort on such an object, and there's
3811 * nothing to do. */
3812 }
3813 else
John Durkopd0321952020-10-29 21:37:36 -07003814#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003815 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003816 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003817 mbedtls_free( operation->ctx.hkdf.info );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003818 status = psa_mac_abort( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003819 }
John Durkop07cc04a2020-11-16 22:08:34 -08003820 else
3821#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
3822#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3823 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
3824 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003825 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003826 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003827 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003828 if( operation->ctx.tls12_prf.secret != NULL )
3829 {
3830 mbedtls_platform_zeroize( operation->ctx.tls12_prf.secret,
3831 operation->ctx.tls12_prf.secret_length );
3832 mbedtls_free( operation->ctx.tls12_prf.secret );
3833 }
3834
Janos Follath6a1d2622019-06-11 10:37:28 +01003835 if( operation->ctx.tls12_prf.seed != NULL )
3836 {
3837 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
3838 operation->ctx.tls12_prf.seed_length );
3839 mbedtls_free( operation->ctx.tls12_prf.seed );
3840 }
3841
3842 if( operation->ctx.tls12_prf.label != NULL )
3843 {
3844 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
3845 operation->ctx.tls12_prf.label_length );
3846 mbedtls_free( operation->ctx.tls12_prf.label );
3847 }
3848
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003849 status = PSA_SUCCESS;
Janos Follath6a1d2622019-06-11 10:37:28 +01003850
3851 /* We leave the fields Ai and output_block to be erased safely by the
3852 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003853 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003854 else
John Durkop07cc04a2020-11-16 22:08:34 -08003855#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
3856 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003857 {
3858 status = PSA_ERROR_BAD_STATE;
3859 }
Janos Follath083036a2019-06-11 10:22:26 +01003860 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003861 return( status );
3862}
3863
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003864psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003865 size_t *capacity)
3866{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003867 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003868 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003869 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02003870 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003871 }
3872
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003873 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003874 return( PSA_SUCCESS );
3875}
3876
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003877psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003878 size_t capacity )
3879{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003880 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003881 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003882 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003883 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003884 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003885 return( PSA_SUCCESS );
3886}
3887
John Durkopd0321952020-10-29 21:37:36 -07003888#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003889/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02003890 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003891static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Steven Cooremanb27e3502021-04-29 19:11:25 +02003892 psa_algorithm_t hash_alg,
3893 uint8_t *output,
3894 size_t output_length )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003895{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003896 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003897 size_t hmac_output_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003898 psa_status_t status;
3899
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003900 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3901 return( PSA_ERROR_BAD_STATE );
3902 hkdf->state = HKDF_STATE_OUTPUT;
3903
Gilles Peskinebef7f142018-07-12 17:22:21 +02003904 while( output_length != 0 )
3905 {
3906 /* Copy what remains of the current block */
3907 uint8_t n = hash_length - hkdf->offset_in_block;
3908 if( n > output_length )
3909 n = (uint8_t) output_length;
3910 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3911 output += n;
3912 output_length -= n;
3913 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003914 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003915 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003916 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003917 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02003918 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02003919 * object was corrupted or if this function is called directly
3920 * inside the library. */
3921 if( hkdf->block_number == 0xff )
3922 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003923
3924 /* We need a new block */
3925 ++hkdf->block_number;
3926 hkdf->offset_in_block = 0;
Steven Cooremanb27e3502021-04-29 19:11:25 +02003927
Steven Cooreman9878a162021-05-06 17:58:36 +02003928 status = psa_key_derivation_start_hmac( &hkdf->hmac,
3929 hash_alg,
3930 hkdf->prk,
3931 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003932 if( status != PSA_SUCCESS )
3933 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003934
Gilles Peskinebef7f142018-07-12 17:22:21 +02003935 if( hkdf->block_number != 1 )
3936 {
Steven Cooremanb27e3502021-04-29 19:11:25 +02003937 status = psa_mac_update( &hkdf->hmac,
3938 hkdf->output_block,
3939 hash_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003940 if( status != PSA_SUCCESS )
3941 return( status );
3942 }
Steven Cooremanb27e3502021-04-29 19:11:25 +02003943 status = psa_mac_update( &hkdf->hmac,
3944 hkdf->info,
3945 hkdf->info_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003946 if( status != PSA_SUCCESS )
3947 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003948 status = psa_mac_update( &hkdf->hmac,
3949 &hkdf->block_number, 1 );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003950 if( status != PSA_SUCCESS )
3951 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02003952 status = psa_mac_sign_finish( &hkdf->hmac,
3953 hkdf->output_block,
3954 sizeof( hkdf->output_block ),
3955 &hmac_output_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003956 if( status != PSA_SUCCESS )
3957 return( status );
3958 }
3959
3960 return( PSA_SUCCESS );
3961}
John Durkop07cc04a2020-11-16 22:08:34 -08003962#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003963
John Durkop07cc04a2020-11-16 22:08:34 -08003964#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
3965 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01003966static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
3967 psa_tls12_prf_key_derivation_t *tls12_prf,
3968 psa_algorithm_t alg )
3969{
3970 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003971 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Steven Cooreman22dea1d2021-04-29 19:32:25 +02003972 psa_mac_operation_t hmac = PSA_MAC_OPERATION_INIT;
3973 size_t hmac_output_length;
Janos Follathea29bfb2019-06-19 12:21:20 +01003974 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003975
Janos Follath7742fee2019-06-17 12:58:10 +01003976 /* We can't be wanting more output after block 0xff, otherwise
3977 * the capacity check in psa_key_derivation_output_bytes() would have
3978 * prevented this call. It could happen only if the operation
3979 * object was corrupted or if this function is called directly
3980 * inside the library. */
3981 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01003982 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01003983
3984 /* We need a new block */
3985 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01003986 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01003987
3988 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3989 *
3990 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3991 *
3992 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3993 * HMAC_hash(secret, A(2) + seed) +
3994 * HMAC_hash(secret, A(3) + seed) + ...
3995 *
3996 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01003997 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01003998 *
Janos Follathea29bfb2019-06-19 12:21:20 +01003999 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004000 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004001 * is currently extracted as `output_block` and where i is
4002 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004003 */
4004
Steven Cooreman9878a162021-05-06 17:58:36 +02004005 status = psa_key_derivation_start_hmac( &hmac,
4006 hash_alg,
4007 tls12_prf->secret,
4008 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004009 if( status != PSA_SUCCESS )
4010 goto cleanup;
4011
4012 /* Calculate A(i) where i = tls12_prf->block_number. */
4013 if( tls12_prf->block_number == 1 )
4014 {
4015 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4016 * the variable seed and in this instance means it in the context of the
4017 * P_hash function, where seed = label + seed.) */
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004018 status = psa_mac_update( &hmac,
4019 tls12_prf->label,
4020 tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004021 if( status != PSA_SUCCESS )
4022 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004023 status = psa_mac_update( &hmac,
4024 tls12_prf->seed,
4025 tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004026 if( status != PSA_SUCCESS )
4027 goto cleanup;
4028 }
4029 else
4030 {
4031 /* A(i) = HMAC_hash(secret, A(i-1)) */
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004032 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004033 if( status != PSA_SUCCESS )
4034 goto cleanup;
4035 }
4036
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004037 status = psa_mac_sign_finish( &hmac,
4038 tls12_prf->Ai, hash_length,
4039 &hmac_output_length );
4040 if( hmac_output_length != hash_length )
4041 status = PSA_ERROR_CORRUPTION_DETECTED;
Janos Follathea29bfb2019-06-19 12:21:20 +01004042 if( status != PSA_SUCCESS )
4043 goto cleanup;
4044
4045 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
Steven Cooreman9878a162021-05-06 17:58:36 +02004046 status = psa_key_derivation_start_hmac( &hmac,
4047 hash_alg,
4048 tls12_prf->secret,
4049 tls12_prf->secret_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004050 if( status != PSA_SUCCESS )
4051 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004052 status = psa_mac_update( &hmac, tls12_prf->Ai, hash_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004053 if( status != PSA_SUCCESS )
4054 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004055 status = psa_mac_update( &hmac, tls12_prf->label, tls12_prf->label_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004056 if( status != PSA_SUCCESS )
4057 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004058 status = psa_mac_update( &hmac, tls12_prf->seed, tls12_prf->seed_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004059 if( status != PSA_SUCCESS )
4060 goto cleanup;
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004061 status = psa_mac_sign_finish( &hmac,
4062 tls12_prf->output_block, hash_length,
4063 &hmac_output_length );
Janos Follathea29bfb2019-06-19 12:21:20 +01004064 if( status != PSA_SUCCESS )
4065 goto cleanup;
4066
Janos Follath7742fee2019-06-17 12:58:10 +01004067
4068cleanup:
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004069 cleanup_status = psa_mac_abort( &hmac );
Janos Follathea29bfb2019-06-19 12:21:20 +01004070 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4071 status = cleanup_status;
4072
4073 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004074}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004075
Janos Follath844eb0e2019-06-19 12:10:49 +01004076static psa_status_t psa_key_derivation_tls12_prf_read(
4077 psa_tls12_prf_key_derivation_t *tls12_prf,
4078 psa_algorithm_t alg,
4079 uint8_t *output,
4080 size_t output_length )
4081{
4082 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004083 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004084 psa_status_t status;
4085 uint8_t offset, length;
4086
Gilles Peskinef216f0d2021-06-11 22:41:46 +02004087 switch( tls12_prf->state )
4088 {
4089 case PSA_TLS12_PRF_STATE_LABEL_SET:
4090 tls12_prf->state = PSA_TLS12_PRF_STATE_OUTPUT;
4091 break;
4092 case PSA_TLS12_PRF_STATE_OUTPUT:
4093 break;
4094 default:
4095 return( PSA_ERROR_BAD_STATE );
4096 }
4097
Janos Follath844eb0e2019-06-19 12:10:49 +01004098 while( output_length != 0 )
4099 {
4100 /* Check if we have fully processed the current block. */
4101 if( tls12_prf->left_in_block == 0 )
4102 {
4103 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4104 alg );
4105 if( status != PSA_SUCCESS )
4106 return( status );
4107
4108 continue;
4109 }
4110
4111 if( tls12_prf->left_in_block > output_length )
4112 length = (uint8_t) output_length;
4113 else
4114 length = tls12_prf->left_in_block;
4115
4116 offset = hash_length - tls12_prf->left_in_block;
4117 memcpy( output, tls12_prf->output_block + offset, length );
4118 output += length;
4119 output_length -= length;
4120 tls12_prf->left_in_block -= length;
4121 }
4122
4123 return( PSA_SUCCESS );
4124}
John Durkop07cc04a2020-11-16 22:08:34 -08004125#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4126 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004127
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004128psa_status_t psa_key_derivation_output_bytes(
4129 psa_key_derivation_operation_t *operation,
4130 uint8_t *output,
4131 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004132{
4133 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004134 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004135
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004136 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004137 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004138 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004139 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004140 }
4141
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004142 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004143 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004144 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004145 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004146 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004147 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004148 goto exit;
4149 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004150 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004151 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004152 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004153 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004154 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4155 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004156 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004157 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004158 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004159 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004160 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004161
John Durkopd0321952020-10-29 21:37:36 -07004162#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004163 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004164 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004165 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004166 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004167 output, output_length );
4168 }
Janos Follathadbec812019-06-14 11:05:39 +01004169 else
John Durkop07cc04a2020-11-16 22:08:34 -08004170#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4171#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4172 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004173 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004174 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004175 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004176 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004177 kdf_alg, output,
4178 output_length );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004179 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004180 else
John Durkop07cc04a2020-11-16 22:08:34 -08004181#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4182 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004183 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004184 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004185 return( PSA_ERROR_BAD_STATE );
4186 }
4187
4188exit:
4189 if( status != PSA_SUCCESS )
4190 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004191 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004192 * This allows us to differentiate between exhausted operations and
4193 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4194 * operations. */
4195 psa_algorithm_t alg = operation->alg;
4196 psa_key_derivation_abort( operation );
4197 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004198 memset( output, '!', output_length );
4199 }
4200 return( status );
4201}
4202
David Brown7807bf72021-02-09 16:28:23 -07004203#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004204static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4205{
4206 if( data_size >= 8 )
4207 mbedtls_des_key_set_parity( data );
4208 if( data_size >= 16 )
4209 mbedtls_des_key_set_parity( data + 8 );
4210 if( data_size >= 24 )
4211 mbedtls_des_key_set_parity( data + 16 );
4212}
David Brown7807bf72021-02-09 16:28:23 -07004213#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004214
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004215static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004216 psa_key_slot_t *slot,
4217 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004218 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004219{
4220 uint8_t *data = NULL;
4221 size_t bytes = PSA_BITS_TO_BYTES( bits );
4222 psa_status_t status;
4223
Gilles Peskine8e338702019-07-30 20:06:31 +02004224 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004225 return( PSA_ERROR_INVALID_ARGUMENT );
4226 if( bits % 8 != 0 )
4227 return( PSA_ERROR_INVALID_ARGUMENT );
4228 data = mbedtls_calloc( 1, bytes );
4229 if( data == NULL )
4230 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4231
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004232 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004233 if( status != PSA_SUCCESS )
4234 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004235#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004236 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004237 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07004238#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004239
4240 status = psa_allocate_buffer_to_slot( slot, bytes );
4241 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00004242 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01004243
Ronald Cronbf33c932020-11-28 18:06:53 +01004244 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004245 psa_key_attributes_t attributes = {
4246 .core = slot->attr
4247 };
4248
Ronald Cronbf33c932020-11-28 18:06:53 +01004249 status = psa_driver_wrapper_import_key( &attributes,
4250 data, bytes,
4251 slot->key.data,
4252 slot->key.bytes,
4253 &slot->key.bytes, &bits );
4254 if( bits != slot->attr.bits )
4255 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004256
4257exit:
4258 mbedtls_free( data );
4259 return( status );
4260}
4261
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004262psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004263 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004264 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004265{
4266 psa_status_t status;
4267 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004268 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004269
Ronald Cron81709fc2020-11-14 12:10:32 +01004270 *key = MBEDTLS_SVC_KEY_ID_INIT;
4271
Gilles Peskine0f84d622019-09-12 19:03:13 +02004272 /* Reject any attempt to create a zero-length key so that we don't
4273 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4274 if( psa_get_key_bits( attributes ) == 0 )
4275 return( PSA_ERROR_INVALID_ARGUMENT );
4276
Dave Rodgman021e7242021-11-16 10:32:48 +00004277 if( operation->alg == PSA_ALG_NONE )
4278 return( PSA_ERROR_BAD_STATE );
4279
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004280 if( ! operation->can_output_key )
4281 return( PSA_ERROR_NOT_PERMITTED );
4282
Ronald Cron81709fc2020-11-14 12:10:32 +01004283 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
4284 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004285#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4286 if( driver != NULL )
4287 {
4288 /* Deriving a key in a secure element is not implemented yet. */
4289 status = PSA_ERROR_NOT_SUPPORTED;
4290 }
4291#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004292 if( status == PSA_SUCCESS )
4293 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004294 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004295 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004296 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004297 }
4298 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01004299 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004300 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004301 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004302
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004303 return( status );
4304}
4305
Gilles Peskineeab56e42018-07-12 17:12:33 +02004306
4307
4308/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004309/* Key derivation */
4310/****************************************************************/
4311
Steven Cooreman932ffb72021-02-15 12:14:32 +01004312#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004313static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004314 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004315 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004316{
John Durkop07cc04a2020-11-16 22:08:34 -08004317 int is_kdf_alg_supported;
4318
Janos Follath5fe19732019-06-20 15:09:30 +01004319 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4320 * initialisers for this union leave some bytes unspecified.) */
4321 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4322
Gilles Peskine969c5d62019-01-16 15:53:06 +01004323 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07004324#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08004325 if( PSA_ALG_IS_HKDF( kdf_alg ) )
4326 is_kdf_alg_supported = 1;
4327 else
4328#endif
4329#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4330 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
4331 is_kdf_alg_supported = 1;
4332 else
4333#endif
4334#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4335 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
4336 is_kdf_alg_supported = 1;
4337 else
4338#endif
4339 is_kdf_alg_supported = 0;
4340
4341 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004342 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004343 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004344 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004345 if( hash_size == 0 )
4346 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004347 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4348 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004349 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004350 {
4351 return( PSA_ERROR_NOT_SUPPORTED );
4352 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004353 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004354 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004355 }
John Durkop07cc04a2020-11-16 22:08:34 -08004356
4357 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004358}
John Durkop07cc04a2020-11-16 22:08:34 -08004359#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004360
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004361psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004362 psa_algorithm_t alg )
4363{
4364 psa_status_t status;
4365
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004366 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004367 return( PSA_ERROR_BAD_STATE );
4368
Gilles Peskine6843c292019-01-18 16:44:49 +01004369 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4370 return( PSA_ERROR_INVALID_ARGUMENT );
4371 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004372 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004373#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004374 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004375 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004376#else
4377 return( PSA_ERROR_NOT_SUPPORTED );
4378#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004379 }
4380 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4381 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004382#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004383 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004384#else
4385 return( PSA_ERROR_NOT_SUPPORTED );
4386#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004387 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004388 else
4389 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004390
4391 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004393 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004394}
4395
John Durkopd0321952020-10-29 21:37:36 -07004396#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004397static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004398 psa_algorithm_t hash_alg,
4399 psa_key_derivation_step_t step,
4400 const uint8_t *data,
4401 size_t data_length )
4402{
4403 psa_status_t status;
4404 switch( step )
4405 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004406 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004407 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004408 return( PSA_ERROR_BAD_STATE );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004409 else
4410 {
Steven Cooreman9878a162021-05-06 17:58:36 +02004411 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4412 hash_alg,
4413 data, data_length );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004414 if( status != PSA_SUCCESS )
4415 return( status );
4416 hkdf->state = HKDF_STATE_STARTED;
4417 return( PSA_SUCCESS );
4418 }
Gilles Peskine03410b52019-05-16 16:05:19 +02004419 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004420 /* If no salt was provided, use an empty salt. */
4421 if( hkdf->state == HKDF_STATE_INIT )
4422 {
Steven Cooreman9878a162021-05-06 17:58:36 +02004423 status = psa_key_derivation_start_hmac( &hkdf->hmac,
4424 hash_alg,
4425 NULL, 0 );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004426 if( status != PSA_SUCCESS )
4427 return( status );
4428 hkdf->state = HKDF_STATE_STARTED;
4429 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004430 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004431 return( PSA_ERROR_BAD_STATE );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004432 status = psa_mac_update( &hkdf->hmac,
4433 data, data_length );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004434 if( status != PSA_SUCCESS )
4435 return( status );
Steven Cooremanb27e3502021-04-29 19:11:25 +02004436 status = psa_mac_sign_finish( &hkdf->hmac,
4437 hkdf->prk,
4438 sizeof( hkdf->prk ),
4439 &data_length );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004440 if( status != PSA_SUCCESS )
4441 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004442 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004443 hkdf->block_number = 0;
4444 hkdf->state = HKDF_STATE_KEYED;
4445 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004446 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004447 if( hkdf->state == HKDF_STATE_OUTPUT )
4448 return( PSA_ERROR_BAD_STATE );
4449 if( hkdf->info_set )
4450 return( PSA_ERROR_BAD_STATE );
4451 hkdf->info_length = data_length;
4452 if( data_length != 0 )
4453 {
4454 hkdf->info = mbedtls_calloc( 1, data_length );
4455 if( hkdf->info == NULL )
4456 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4457 memcpy( hkdf->info, data, data_length );
4458 }
4459 hkdf->info_set = 1;
4460 return( PSA_SUCCESS );
4461 default:
4462 return( PSA_ERROR_INVALID_ARGUMENT );
4463 }
4464}
John Durkop07cc04a2020-11-16 22:08:34 -08004465#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004466
John Durkop07cc04a2020-11-16 22:08:34 -08004467#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4468 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01004469static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4470 const uint8_t *data,
4471 size_t data_length )
4472{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004473 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01004474 return( PSA_ERROR_BAD_STATE );
4475
Janos Follathd6dce9f2019-07-04 09:11:38 +01004476 if( data_length != 0 )
4477 {
4478 prf->seed = mbedtls_calloc( 1, data_length );
4479 if( prf->seed == NULL )
4480 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004481
Janos Follathd6dce9f2019-07-04 09:11:38 +01004482 memcpy( prf->seed, data, data_length );
4483 prf->seed_length = data_length;
4484 }
Janos Follathf08e2652019-06-13 09:05:41 +01004485
Gilles Peskine43f958b2020-12-13 14:55:14 +01004486 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004487
4488 return( PSA_SUCCESS );
4489}
4490
Janos Follath81550542019-06-13 14:26:34 +01004491static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
Janos Follath81550542019-06-13 14:26:34 +01004492 const uint8_t *data,
4493 size_t data_length )
4494{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004495 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01004496 return( PSA_ERROR_BAD_STATE );
4497
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004498 if( data_length != 0 )
4499 {
4500 prf->secret = mbedtls_calloc( 1, data_length );
4501 if( prf->secret == NULL )
4502 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4503
4504 memcpy( prf->secret, data, data_length );
4505 prf->secret_length = data_length;
4506 }
Janos Follath81550542019-06-13 14:26:34 +01004507
Gilles Peskine43f958b2020-12-13 14:55:14 +01004508 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004509
4510 return( PSA_SUCCESS );
4511}
4512
Janos Follath63028dd2019-06-13 09:15:47 +01004513static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
4514 const uint8_t *data,
4515 size_t data_length )
4516{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004517 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01004518 return( PSA_ERROR_BAD_STATE );
4519
Janos Follathd6dce9f2019-07-04 09:11:38 +01004520 if( data_length != 0 )
4521 {
4522 prf->label = mbedtls_calloc( 1, data_length );
4523 if( prf->label == NULL )
4524 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01004525
Janos Follathd6dce9f2019-07-04 09:11:38 +01004526 memcpy( prf->label, data, data_length );
4527 prf->label_length = data_length;
4528 }
Janos Follath63028dd2019-06-13 09:15:47 +01004529
Gilles Peskine43f958b2020-12-13 14:55:14 +01004530 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004531
4532 return( PSA_SUCCESS );
4533}
4534
Janos Follathb03233e2019-06-11 15:30:30 +01004535static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
Janos Follathb03233e2019-06-11 15:30:30 +01004536 psa_key_derivation_step_t step,
4537 const uint8_t *data,
4538 size_t data_length )
4539{
Janos Follathb03233e2019-06-11 15:30:30 +01004540 switch( step )
4541 {
Janos Follathf08e2652019-06-13 09:05:41 +01004542 case PSA_KEY_DERIVATION_INPUT_SEED:
4543 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01004544 case PSA_KEY_DERIVATION_INPUT_SECRET:
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004545 return( psa_tls12_prf_set_key( prf, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01004546 case PSA_KEY_DERIVATION_INPUT_LABEL:
4547 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01004548 default:
4549 return( PSA_ERROR_INVALID_ARGUMENT );
4550 }
4551}
John Durkop07cc04a2020-11-16 22:08:34 -08004552#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4553 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4554
4555#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4556static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4557 psa_tls12_prf_key_derivation_t *prf,
John Durkop07cc04a2020-11-16 22:08:34 -08004558 const uint8_t *data,
4559 size_t data_length )
4560{
4561 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004562 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08004563 uint8_t *cur = pms;
4564
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004565 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08004566 return( PSA_ERROR_INVALID_ARGUMENT );
4567
4568 /* Quoting RFC 4279, Section 2:
4569 *
4570 * The premaster secret is formed as follows: if the PSK is N octets
4571 * long, concatenate a uint16 with the value N, N zero octets, a second
4572 * uint16 with the value N, and the PSK itself.
4573 */
4574
Joe Subbiani2bbafda2021-06-24 13:00:03 +01004575 *cur++ = MBEDTLS_BYTE_1( data_length );
4576 *cur++ = MBEDTLS_BYTE_0( data_length );
John Durkop07cc04a2020-11-16 22:08:34 -08004577 memset( cur, 0, data_length );
4578 cur += data_length;
4579 *cur++ = pms[0];
4580 *cur++ = pms[1];
4581 memcpy( cur, data, data_length );
4582 cur += data_length;
4583
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004584 status = psa_tls12_prf_set_key( prf, pms, cur - pms );
John Durkop07cc04a2020-11-16 22:08:34 -08004585
4586 mbedtls_platform_zeroize( pms, sizeof( pms ) );
4587 return( status );
4588}
Janos Follath6660f0e2019-06-17 08:44:03 +01004589
4590static psa_status_t psa_tls12_prf_psk_to_ms_input(
4591 psa_tls12_prf_key_derivation_t *prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004592 psa_key_derivation_step_t step,
4593 const uint8_t *data,
4594 size_t data_length )
4595{
4596 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01004597 {
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004598 return( psa_tls12_prf_psk_to_ms_set_key( prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004599 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01004600 }
Janos Follath6660f0e2019-06-17 08:44:03 +01004601
Steven Cooreman22dea1d2021-04-29 19:32:25 +02004602 return( psa_tls12_prf_input( prf, step, data, data_length ) );
Janos Follath6660f0e2019-06-17 08:44:03 +01004603}
John Durkop07cc04a2020-11-16 22:08:34 -08004604#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004605
Gilles Peskineb8965192019-09-24 16:21:10 +02004606/** Check whether the given key type is acceptable for the given
4607 * input step of a key derivation.
4608 *
4609 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
4610 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
4611 * Both secret and non-secret inputs can alternatively have the type
4612 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
4613 * that the input was passed as a buffer rather than via a key object.
4614 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02004615static int psa_key_derivation_check_input_type(
4616 psa_key_derivation_step_t step,
4617 psa_key_type_t key_type )
4618{
4619 switch( step )
4620 {
4621 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02004622 if( key_type == PSA_KEY_TYPE_DERIVE )
4623 return( PSA_SUCCESS );
4624 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004625 return( PSA_SUCCESS );
4626 break;
4627 case PSA_KEY_DERIVATION_INPUT_LABEL:
4628 case PSA_KEY_DERIVATION_INPUT_SALT:
4629 case PSA_KEY_DERIVATION_INPUT_INFO:
4630 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02004631 if( key_type == PSA_KEY_TYPE_RAW_DATA )
4632 return( PSA_SUCCESS );
4633 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02004634 return( PSA_SUCCESS );
4635 break;
4636 }
4637 return( PSA_ERROR_INVALID_ARGUMENT );
4638}
4639
Janos Follathb80a94e2019-06-12 15:54:46 +01004640static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004641 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004642 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004643 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004644 const uint8_t *data,
4645 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004646{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02004647 psa_status_t status;
4648 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
4649
4650 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02004651 if( status != PSA_SUCCESS )
4652 goto exit;
4653
John Durkopd0321952020-10-29 21:37:36 -07004654#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004655 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004656 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004657 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004658 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004659 step, data, data_length );
4660 }
John Durkop07cc04a2020-11-16 22:08:34 -08004661 else
4662#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4663#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4664 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004665 {
Janos Follathb03233e2019-06-11 15:30:30 +01004666 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
Janos Follathb03233e2019-06-11 15:30:30 +01004667 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01004668 }
John Durkop07cc04a2020-11-16 22:08:34 -08004669 else
4670#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
4671#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4672 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01004673 {
4674 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
Janos Follath6660f0e2019-06-17 08:44:03 +01004675 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004676 }
4677 else
John Durkop07cc04a2020-11-16 22:08:34 -08004678#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004679 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004680 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01004681 (void) data;
4682 (void) data_length;
4683 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004684 return( PSA_ERROR_BAD_STATE );
4685 }
4686
Gilles Peskine224b0d62019-09-23 18:13:17 +02004687exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004688 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004689 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004690 return( status );
4691}
4692
Janos Follath51f4a0f2019-06-14 11:35:55 +01004693psa_status_t psa_key_derivation_input_bytes(
4694 psa_key_derivation_operation_t *operation,
4695 psa_key_derivation_step_t step,
4696 const uint8_t *data,
4697 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004698{
Gilles Peskineb8965192019-09-24 16:21:10 +02004699 return( psa_key_derivation_input_internal( operation, step,
4700 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01004701 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004702}
4703
Janos Follath51f4a0f2019-06-14 11:35:55 +01004704psa_status_t psa_key_derivation_input_key(
4705 psa_key_derivation_operation_t *operation,
4706 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004707 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004708{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004709 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004710 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004711 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004712
Ronald Cron5c522922020-11-14 16:35:34 +01004713 status = psa_get_and_lock_transparent_key_slot_with_policy(
4714 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004715 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02004716 {
4717 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004718 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02004719 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004720
4721 /* Passing a key object as a SECRET input unlocks the permission
4722 * to output to a key object. */
4723 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4724 operation->can_output_key = 1;
4725
Ronald Cronf95a2b12020-10-22 15:24:49 +02004726 status = psa_key_derivation_input_internal( operation,
4727 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004728 slot->key.data,
4729 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004730
Ronald Cron5c522922020-11-14 16:35:34 +01004731 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004732
Ronald Cron5c522922020-11-14 16:35:34 +01004733 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004734}
Gilles Peskineea0fb492018-07-12 17:17:20 +02004735
4736
4737
4738/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004739/* Key agreement */
4740/****************************************************************/
4741
John Durkop6ba40d12020-11-10 08:50:04 -08004742#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004743static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4744 size_t peer_key_length,
4745 const mbedtls_ecp_keypair *our_key,
4746 uint8_t *shared_secret,
4747 size_t shared_secret_size,
4748 size_t *shared_secret_length )
4749{
Steven Cooremand4867872020-08-05 16:31:39 +02004750 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004751 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004752 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004753 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01004754 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004755 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004756
Ronald Cron90857082020-11-25 14:58:33 +01004757 status = mbedtls_psa_ecp_load_representation(
4758 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004759 bits,
Ronald Cron90857082020-11-25 14:58:33 +01004760 peer_key,
4761 peer_key_length,
4762 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004763 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004764 goto exit;
4765
Jaeden Amero97271b32019-01-10 19:38:51 +00004766 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02004767 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00004768 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004769 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004770 status = mbedtls_to_psa_error(
4771 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4772 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004773 goto exit;
4774
Jaeden Amero97271b32019-01-10 19:38:51 +00004775 status = mbedtls_to_psa_error(
4776 mbedtls_ecdh_calc_secret( &ecdh,
4777 shared_secret_length,
4778 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004779 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004780 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01004781 if( status != PSA_SUCCESS )
4782 goto exit;
4783 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
4784 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004785
4786exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01004787 if( status != PSA_SUCCESS )
4788 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004789 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02004790 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02004791 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02004792
Jaeden Amero97271b32019-01-10 19:38:51 +00004793 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004794}
John Durkop6ba40d12020-11-10 08:50:04 -08004795#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004796
Gilles Peskine01d718c2018-09-18 12:01:02 +02004797#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4798
Gilles Peskine0216fe12019-04-11 21:23:21 +02004799static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
4800 psa_key_slot_t *private_key,
4801 const uint8_t *peer_key,
4802 size_t peer_key_length,
4803 uint8_t *shared_secret,
4804 size_t shared_secret_size,
4805 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004806{
Gilles Peskine0216fe12019-04-11 21:23:21 +02004807 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004808 {
John Durkop6ba40d12020-11-10 08:50:04 -08004809#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02004810 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02004811 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004812 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02004813 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004814 psa_status_t status = mbedtls_psa_ecp_load_representation(
4815 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01004816 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01004817 private_key->key.data,
4818 private_key->key.bytes,
4819 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004820 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02004821 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02004822 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02004823 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004824 shared_secret, shared_secret_size,
4825 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004826 mbedtls_ecp_keypair_free( ecp );
4827 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02004828 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08004829#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004830 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004831 (void) private_key;
4832 (void) peer_key;
4833 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004834 (void) shared_secret;
4835 (void) shared_secret_size;
4836 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004837 return( PSA_ERROR_NOT_SUPPORTED );
4838 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02004839}
4840
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004841/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02004842 * to potentially free embedded data structures and wipe confidential data.
4843 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004844static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004845 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004846 psa_key_slot_t *private_key,
4847 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004848 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004849{
4850 psa_status_t status;
4851 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4852 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004853 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004854
4855 /* Step 1: run the secret agreement algorithm to generate the shared
4856 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02004857 status = psa_key_agreement_raw_internal( ka_alg,
4858 private_key,
4859 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03004860 shared_secret,
4861 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02004862 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004863 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004864 goto exit;
4865
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004866 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02004867 * the shared secret. A shared secret is permitted wherever a key
4868 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01004869 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02004870 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01004871 shared_secret,
4872 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004873exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004874 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004875 return( status );
4876}
4877
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004878psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004879 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004880 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004881 const uint8_t *peer_key,
4882 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004883{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004884 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004885 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004886 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004887
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004889 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01004890 status = psa_get_and_lock_transparent_key_slot_with_policy(
4891 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004892 if( status != PSA_SUCCESS )
4893 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004895 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004896 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004897 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004899 else
4900 {
4901 /* If a private key has been added as SECRET, we allow the derived
4902 * key material to be used as a key in PSA Crypto. */
4903 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
4904 operation->can_output_key = 1;
4905 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004906
Ronald Cron5c522922020-11-14 16:35:34 +01004907 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004908
Ronald Cron5c522922020-11-14 16:35:34 +01004909 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004910}
4911
Gilles Peskinebe697d82019-05-16 18:00:41 +02004912psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004913 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02004914 const uint8_t *peer_key,
4915 size_t peer_key_length,
4916 uint8_t *output,
4917 size_t output_size,
4918 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02004919{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004921 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004922 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02004923
4924 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4925 {
4926 status = PSA_ERROR_INVALID_ARGUMENT;
4927 goto exit;
4928 }
Ronald Cron5c522922020-11-14 16:35:34 +01004929 status = psa_get_and_lock_transparent_key_slot_with_policy(
4930 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004931 if( status != PSA_SUCCESS )
4932 goto exit;
4933
4934 status = psa_key_agreement_raw_internal( alg, slot,
4935 peer_key, peer_key_length,
4936 output, output_size,
4937 output_length );
4938
4939exit:
4940 if( status != PSA_SUCCESS )
4941 {
4942 /* If an error happens and is not handled properly, the output
4943 * may be used as a key to protect sensitive data. Arrange for such
4944 * a key to be random, which is likely to result in decryption or
4945 * verification errors. This is better than filling the buffer with
4946 * some constant data such as zeros, which would result in the data
4947 * being protected with a reproducible, easily knowable key.
4948 */
4949 psa_generate_random( output, output_size );
4950 *output_length = output_size;
4951 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004952
Ronald Cron5c522922020-11-14 16:35:34 +01004953 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004954
Ronald Cron5c522922020-11-14 16:35:34 +01004955 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02004956}
Gilles Peskine86a440b2018-11-12 18:39:40 +01004957
4958
Gilles Peskine30524eb2020-11-13 17:02:26 +01004959
Gilles Peskine86a440b2018-11-12 18:39:40 +01004960/****************************************************************/
4961/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02004962/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02004963
Gilles Peskine30524eb2020-11-13 17:02:26 +01004964/** Initialize the PSA random generator.
4965 */
4966static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
4967{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004968#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4969 memset( rng, 0, sizeof( *rng ) );
4970#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
4971
Gilles Peskine30524eb2020-11-13 17:02:26 +01004972 /* Set default configuration if
4973 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4974 if( rng->entropy_init == NULL )
4975 rng->entropy_init = mbedtls_entropy_init;
4976 if( rng->entropy_free == NULL )
4977 rng->entropy_free = mbedtls_entropy_free;
4978
4979 rng->entropy_init( &rng->entropy );
4980#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
4981 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
4982 /* The PSA entropy injection feature depends on using NV seed as an entropy
4983 * source. Add NV seed as an entropy source for PSA entropy injection. */
4984 mbedtls_entropy_add_source( &rng->entropy,
4985 mbedtls_nv_seed_poll, NULL,
4986 MBEDTLS_ENTROPY_BLOCK_SIZE,
4987 MBEDTLS_ENTROPY_SOURCE_STRONG );
4988#endif
4989
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004990 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004991#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01004992}
4993
4994/** Deinitialize the PSA random generator.
4995 */
4996static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
4997{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01004998#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
4999 memset( rng, 0, sizeof( *rng ) );
5000#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005001 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005002 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005003#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005004}
5005
5006/** Seed the PSA random generator.
5007 */
5008static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5009{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005010#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5011 /* Do nothing: the external RNG seeds itself. */
5012 (void) rng;
5013 return( PSA_SUCCESS );
5014#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005015 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005016 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5017 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005018 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005019#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005020}
5021
Gilles Peskinee59236f2018-01-27 23:32:46 +01005022psa_status_t psa_generate_random( uint8_t *output,
5023 size_t output_size )
5024{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005025 GUARD_MODULE_INITIALIZED;
5026
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005027#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5028
5029 size_t output_length = 0;
5030 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5031 output, output_size,
5032 &output_length );
5033 if( status != PSA_SUCCESS )
5034 return( status );
5035 /* Breaking up a request into smaller chunks is currently not supported
5036 * for the extrernal RNG interface. */
5037 if( output_length != output_size )
5038 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5039 return( PSA_SUCCESS );
5040
5041#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5042
Gilles Peskine71ddab92021-01-04 21:01:07 +01005043 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005044 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005045 size_t request_size =
5046 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5047 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5048 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005049 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
5050 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02005051 if( ret != 0 )
5052 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01005053 output_size -= request_size;
5054 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005055 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005056 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005057#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005058}
5059
Gilles Peskine8814fc42020-12-14 15:33:44 +01005060/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005061 *
5062 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5063 * `psa_generate_random(...)`. The state parameter is ignored since the
5064 * PSA API doesn't support passing an explicit state.
5065 *
5066 * In the non-external case, psa_generate_random() calls an
5067 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5068 * and semantics as mbedtls_psa_get_random(). As an optimization,
5069 * instead of doing this back-and-forth between the PSA API and the
5070 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5071 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005072 */
5073#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5074int mbedtls_psa_get_random( void *p_rng,
5075 unsigned char *output,
5076 size_t output_size )
5077{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005078 /* This function takes a pointer to the RNG state because that's what
5079 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5080 * its own state internally and doesn't let the caller access that state.
5081 * So we just ignore the state parameter, and in practice we'll pass
5082 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005083 (void) p_rng;
5084 psa_status_t status = psa_generate_random( output, output_size );
5085 if( status == PSA_SUCCESS )
5086 return( 0 );
5087 else
5088 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
5089}
5090#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5091
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005092#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5093#include "mbedtls/entropy_poll.h"
5094
Gilles Peskine7228da22019-07-15 11:06:15 +02005095psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005096 size_t seed_size )
5097{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005098 if( global_data.initialized )
5099 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005100
5101 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5102 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5103 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5104 return( PSA_ERROR_INVALID_ARGUMENT );
5105
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005106 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005107}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005108#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005109
Ronald Cron3772afe2021-02-08 16:10:05 +01005110/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005111 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005112 * \param type The key type
5113 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005114 *
5115 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005116 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005117 * \retval #PSA_ERROR_INVALID_ARGUMENT
5118 * The size in bits of the key is not valid.
5119 * \retval #PSA_ERROR_NOT_SUPPORTED
5120 * The type and/or the size in bits of the key or the combination of
5121 * the two is not supported.
5122 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005123static psa_status_t psa_validate_key_type_and_size_for_key_generation(
5124 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005125{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005127
Gilles Peskinee59236f2018-01-27 23:32:46 +01005128 if( key_type_is_raw_bytes( type ) )
5129 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02005130 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005131 if( status != PSA_SUCCESS )
5132 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005133 }
5134 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005135#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005136 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5137 {
5138 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5139 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005140
Ronald Cron01b2aba2020-10-05 09:42:02 +02005141 /* Accept only byte-aligned keys, for the same reasons as
5142 * in psa_import_rsa_key(). */
5143 if( bits % 8 != 0 )
5144 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005145 }
5146 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005147#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005148
Ronald Cron5c4d3862020-12-07 11:07:24 +01005149#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005150 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5151 {
Ronald Crond81ab562021-02-16 09:01:16 +01005152 /* To avoid empty block, return successfully here. */
5153 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005154 }
5155 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005156#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005157 {
5158 return( PSA_ERROR_NOT_SUPPORTED );
5159 }
5160
5161 return( PSA_SUCCESS );
5162}
5163
Ronald Cron55ed0592020-10-05 10:30:40 +02005164psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005165 const psa_key_attributes_t *attributes,
5166 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005167{
5168 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005169 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005170
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005171 if( ( attributes->domain_parameters == NULL ) &&
5172 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005173 return( PSA_ERROR_INVALID_ARGUMENT );
5174
Ronald Cron01b2aba2020-10-05 09:42:02 +02005175 if( key_type_is_raw_bytes( type ) )
5176 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005177 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005178 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005179 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005180
David Brown12ca5032021-02-11 11:02:00 -07005181#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005182 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005183 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07005184#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005185 }
5186 else
5187
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005188#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
5189 defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005190 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005191 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01005192 return( mbedtls_psa_rsa_generate_key( attributes,
5193 key_buffer,
5194 key_buffer_size,
5195 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005196 }
5197 else
Jaeden Ameroc17f2932021-05-14 08:34:32 +01005198#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
5199 * defined(MBEDTLS_GENPRIME) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005200
John Durkop9814fa22020-11-04 12:28:15 -08005201#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005202 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005203 {
Ronald Cron7023db52020-11-20 18:17:42 +01005204 return( mbedtls_psa_ecp_generate_key( attributes,
5205 key_buffer,
5206 key_buffer_size,
5207 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005208 }
5209 else
John Durkop9814fa22020-11-04 12:28:15 -08005210#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005211 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005212 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005213 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005214 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005215
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005216 return( PSA_SUCCESS );
5217}
Darryl Green0c6575a2018-11-07 16:05:30 +00005218
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005219psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005220 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005221{
5222 psa_status_t status;
5223 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005224 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005225 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005226
Ronald Cron81709fc2020-11-14 12:10:32 +01005227 *key = MBEDTLS_SVC_KEY_ID_INIT;
5228
Gilles Peskine0f84d622019-09-12 19:03:13 +02005229 /* Reject any attempt to create a zero-length key so that we don't
5230 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5231 if( psa_get_key_bits( attributes ) == 0 )
5232 return( PSA_ERROR_INVALID_ARGUMENT );
5233
Przemyslaw Stekiel5b20a7e2021-10-07 11:08:56 +02005234 /* Reject any attempt to create a public key. */
5235 if( PSA_KEY_TYPE_IS_PUBLIC_KEY(attributes->core.type) )
5236 return( PSA_ERROR_INVALID_ARGUMENT );
5237
Ronald Cron81709fc2020-11-14 12:10:32 +01005238 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
5239 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005240 if( status != PSA_SUCCESS )
5241 goto exit;
5242
Ronald Cron977c2472020-10-13 08:32:21 +02005243 /* In the case of a transparent key or an opaque key stored in local
5244 * storage (thus not in the case of generating a key in a secure element
5245 * or cryptoprocessor with storage), we have to allocate a buffer to
5246 * hold the generated key material. */
5247 if( slot->key.data == NULL )
5248 {
5249 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
5250 PSA_KEY_LOCATION_LOCAL_STORAGE )
5251 {
Ronald Cron3772afe2021-02-08 16:10:05 +01005252 status = psa_validate_key_type_and_size_for_key_generation(
5253 attributes->core.type, attributes->core.bits );
5254 if( status != PSA_SUCCESS )
5255 goto exit;
5256
5257 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
5258 attributes->core.type,
5259 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02005260 }
5261 else
5262 {
5263 status = psa_driver_wrapper_get_key_buffer_size(
5264 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01005265 if( status != PSA_SUCCESS )
5266 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02005267 }
Ronald Cron977c2472020-10-13 08:32:21 +02005268
5269 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
5270 if( status != PSA_SUCCESS )
5271 goto exit;
5272 }
5273
Steven Cooreman2a1664c2020-07-20 15:33:08 +02005274 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02005275 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02005276
Ronald Cron2b56bc82020-10-05 10:02:26 +02005277 if( status != PSA_SUCCESS )
5278 psa_remove_key_data_from_memory( slot );
5279
Gilles Peskine11792082019-08-06 18:36:36 +02005280exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005281 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005282 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005283 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005284 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005285
Darryl Green0c6575a2018-11-07 16:05:30 +00005286 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005287}
5288
Gilles Peskinee59236f2018-01-27 23:32:46 +01005289/****************************************************************/
5290/* Module setup */
5291/****************************************************************/
5292
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005293#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005294psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5295 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5296 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5297{
5298 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5299 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005300 global_data.rng.entropy_init = entropy_init;
5301 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01005302 return( PSA_SUCCESS );
5303}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005304#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005305
Gilles Peskinee59236f2018-01-27 23:32:46 +01005306void mbedtls_psa_crypto_free( void )
5307{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005308 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005309 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5310 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005311 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005312 }
5313 /* Wipe all remaining data, including configuration.
5314 * In particular, this sets all state indicator to the value
5315 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005316 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005317#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005318 /* Unregister all secure element drivers, so that we restart from
5319 * a pristine state. */
5320 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005321#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005322}
5323
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005324#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5325/** Recover a transaction that was interrupted by a power failure.
5326 *
5327 * This function is called during initialization, before psa_crypto_init()
5328 * returns. If this function returns a failure status, the initialization
5329 * fails.
5330 */
5331static psa_status_t psa_crypto_recover_transaction(
5332 const psa_crypto_transaction_t *transaction )
5333{
5334 switch( transaction->unknown.type )
5335 {
5336 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5337 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005338 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005339 * is implemented.
5340 * https://github.com/ARMmbed/mbed-crypto/issues/218
5341 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005342 default:
5343 /* We found an unsupported transaction in the storage.
5344 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01005345 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005346 }
5347}
5348#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5349
Gilles Peskinee59236f2018-01-27 23:32:46 +01005350psa_status_t psa_crypto_init( void )
5351{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005352 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005353
Gilles Peskinec6b69072018-11-20 21:42:52 +01005354 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005355 if( global_data.initialized != 0 )
5356 return( PSA_SUCCESS );
5357
Gilles Peskine30524eb2020-11-13 17:02:26 +01005358 /* Initialize and seed the random generator. */
5359 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005360 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01005361 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005362 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005363 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005364 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005365
Gilles Peskine66fb1262018-12-10 16:29:04 +01005366 status = psa_initialize_key_slots( );
5367 if( status != PSA_SUCCESS )
5368 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005369
Gilles Peskined9348f22019-10-01 15:22:29 +02005370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5371 status = psa_init_all_se_drivers( );
5372 if( status != PSA_SUCCESS )
5373 goto exit;
5374#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5375
Gilles Peskine4b734222019-07-24 15:56:31 +02005376#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005377 status = psa_crypto_load_transaction( );
5378 if( status == PSA_SUCCESS )
5379 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005380 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5381 if( status != PSA_SUCCESS )
5382 goto exit;
5383 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005384 }
5385 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5386 {
5387 /* There's no transaction to complete. It's all good. */
5388 status = PSA_SUCCESS;
5389 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005390#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005391
Gilles Peskinec6b69072018-11-20 21:42:52 +01005392 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005393 global_data.initialized = 1;
5394
5395exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005396 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005397 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005398 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005399}
5400
5401#endif /* MBEDTLS_PSA_CRYPTO_C */