blob: fccb8007760690adbb89356001af017bb8e85c5c [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb2b64d32020-12-14 16:43:58 +010043#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010044
Gilles Peskineb46bef22019-07-30 21:32:04 +020045#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020049#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#define mbedtls_calloc calloc
51#define mbedtls_free free
52#endif
53
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010054#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000057#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020058#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/blowfish.h"
60#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
72#include "mbedtls/md2.h"
73#include "mbedtls/md4.h"
74#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010075#include "mbedtls/md.h"
76#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010079#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000080#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
86#include "mbedtls/xtea.h"
87
Gilles Peskine996deb12018-08-01 15:45:45 +020088#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
89
Gilles Peskine9ef733f2018-02-07 21:05:37 +010090/* constant-time buffer comparison */
91static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
92{
93 size_t i;
94 unsigned char diff = 0;
95
96 for( i = 0; i < n; i++ )
97 diff |= a[i] ^ b[i];
98
99 return( diff );
100}
101
102
103
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100104/****************************************************************/
105/* Global data, support functions and library management */
106/****************************************************************/
107
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108static int key_type_is_raw_bytes( psa_key_type_t type )
109{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200110 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111}
112
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100113/* Values for psa_global_data_t::rng_state */
114#define RNG_NOT_INITIALIZED 0
115#define RNG_INITIALIZED 1
116#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100117
Gilles Peskine2d277862018-06-18 15:41:12 +0200118typedef struct
119{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100120 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100127#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
128mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
129 &global_data.rng.drbg;
130#endif
131
itayzafrir0adf0fc2018-09-06 16:24:41 +0300132#define GUARD_MODULE_INITIALIZED \
133 if( global_data.initialized == 0 ) \
134 return( PSA_ERROR_BAD_STATE );
135
Steven Cooreman01164162020-07-20 15:31:37 +0200136psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100138 /* Mbed TLS error codes can combine a high-level error code and a
139 * low-level error code. The low-level error usually reflects the
140 * root cause better, so dispatch on that preferably. */
141 int low_level_ret = - ( -ret & 0x007f );
142 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 {
144 case 0:
145 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100146
147 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
148 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
149 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
150 return( PSA_ERROR_NOT_SUPPORTED );
151 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
152 return( PSA_ERROR_HARDWARE_FAILURE );
153
154 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
155 return( PSA_ERROR_HARDWARE_FAILURE );
156
Gilles Peskine9a944802018-06-21 09:35:35 +0200157 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
158 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
159 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
160 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
161 case MBEDTLS_ERR_ASN1_INVALID_DATA:
162 return( PSA_ERROR_INVALID_ARGUMENT );
163 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
164 return( PSA_ERROR_INSUFFICIENT_MEMORY );
165 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
166 return( PSA_ERROR_BUFFER_TOO_SMALL );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
Jaeden Amero93e21112019-02-20 13:57:28 +0000178#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000179 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
181 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
182#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_CCM_BAD_INPUT:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190 case MBEDTLS_ERR_CCM_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
193 return( PSA_ERROR_HARDWARE_FAILURE );
194
Gilles Peskine26869f22019-05-06 15:25:00 +0200195 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197
198 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
199 return( PSA_ERROR_BAD_STATE );
200 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
201 return( PSA_ERROR_INVALID_SIGNATURE );
202
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
204 return( PSA_ERROR_NOT_SUPPORTED );
205 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
206 return( PSA_ERROR_INVALID_ARGUMENT );
207 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
208 return( PSA_ERROR_INSUFFICIENT_MEMORY );
209 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
210 return( PSA_ERROR_INVALID_PADDING );
211 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200212 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
214 return( PSA_ERROR_INVALID_SIGNATURE );
215 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200216 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
218 return( PSA_ERROR_HARDWARE_FAILURE );
219
220 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
Gilles Peskine82e57d12020-11-13 21:31:17 +0100223#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
224 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100225 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
226 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
229 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
230 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
231 return( PSA_ERROR_NOT_SUPPORTED );
232 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100235
236 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
237 return( PSA_ERROR_NOT_SUPPORTED );
238 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
Gilles Peskinee59236f2018-01-27 23:32:46 +0100241 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
242 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
243 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
244 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100245
246 case MBEDTLS_ERR_GCM_AUTH_FAILED:
247 return( PSA_ERROR_INVALID_SIGNATURE );
248 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200249 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
Gilles Peskine82e57d12020-11-13 21:31:17 +0100253#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
254 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100255 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
256 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
258 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
259 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
260 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
261 return( PSA_ERROR_NOT_SUPPORTED );
262 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
263 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
264#endif
265
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
267 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
268 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
269 return( PSA_ERROR_HARDWARE_FAILURE );
270
271 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MD_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
278 return( PSA_ERROR_STORAGE_FAILURE );
279 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
280 return( PSA_ERROR_HARDWARE_FAILURE );
281
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
283 return( PSA_ERROR_STORAGE_FAILURE );
284 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
289 return( PSA_ERROR_BUFFER_TOO_SMALL );
290 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
297 return( PSA_ERROR_INSUFFICIENT_MEMORY );
298
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
302 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
313 return( PSA_ERROR_NOT_PERMITTED );
314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
321 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
323 return( PSA_ERROR_HARDWARE_FAILURE );
324
Gilles Peskineff2d2002019-05-06 15:26:23 +0200325 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
328 return( PSA_ERROR_NOT_SUPPORTED );
329
Gilles Peskinea5905292018-02-07 20:59:33 +0100330 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
334 return( PSA_ERROR_INVALID_ARGUMENT );
335 case MBEDTLS_ERR_RSA_INVALID_PADDING:
336 return( PSA_ERROR_INVALID_PADDING );
337 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
340 return( PSA_ERROR_INVALID_ARGUMENT );
341 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
342 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200343 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100344 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
345 return( PSA_ERROR_INVALID_SIGNATURE );
346 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
347 return( PSA_ERROR_BUFFER_TOO_SMALL );
348 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100349 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
351 return( PSA_ERROR_NOT_SUPPORTED );
352 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
354
355 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
356 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
357 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
358 return( PSA_ERROR_HARDWARE_FAILURE );
359
360 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
361 return( PSA_ERROR_INVALID_ARGUMENT );
362 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
363 return( PSA_ERROR_HARDWARE_FAILURE );
364
itayzafrir5c753392018-05-08 11:18:38 +0300365 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300367 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300368 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
369 return( PSA_ERROR_BUFFER_TOO_SMALL );
370 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
371 return( PSA_ERROR_NOT_SUPPORTED );
372 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
373 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
374 return( PSA_ERROR_INVALID_SIGNATURE );
375 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
376 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100377 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
378 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300379 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
380 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100381
Janos Follatha13b9052019-11-22 12:48:59 +0000382 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
383 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300384
Gilles Peskinee59236f2018-01-27 23:32:46 +0100385 default:
David Saadab4ecc272019-02-14 13:48:10 +0200386 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100387 }
388}
389
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200390
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200391
392
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100393/****************************************************************/
394/* Key management */
395/****************************************************************/
396
Gilles Peskine73167e12019-07-12 23:44:37 +0200397#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
398static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
399{
Gilles Peskine8e338702019-07-30 20:06:31 +0200400 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200401}
402#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
403
John Durkop07cc04a2020-11-16 22:08:34 -0800404/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
405 * current test driver in key_management.c is using this function
406 * when accelerators are used for ECC key pair and public key.
407 * Once that dependency is resolved these guards can be removed.
408 */
John Durkop9814fa22020-11-04 12:28:15 -0800409#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800410 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
411 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
412 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100413mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100414 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200415{
416 switch( curve )
417 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100418 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100419 switch( byte_length )
420 {
421 case PSA_BITS_TO_BYTES( 192 ):
422 return( MBEDTLS_ECP_DP_SECP192R1 );
423 case PSA_BITS_TO_BYTES( 224 ):
424 return( MBEDTLS_ECP_DP_SECP224R1 );
425 case PSA_BITS_TO_BYTES( 256 ):
426 return( MBEDTLS_ECP_DP_SECP256R1 );
427 case PSA_BITS_TO_BYTES( 384 ):
428 return( MBEDTLS_ECP_DP_SECP384R1 );
429 case PSA_BITS_TO_BYTES( 521 ):
430 return( MBEDTLS_ECP_DP_SECP521R1 );
431 default:
432 return( MBEDTLS_ECP_DP_NONE );
433 }
434 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100435
Paul Elliott8ff510a2020-06-02 17:19:28 +0100436 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100437 switch( byte_length )
438 {
439 case PSA_BITS_TO_BYTES( 256 ):
440 return( MBEDTLS_ECP_DP_BP256R1 );
441 case PSA_BITS_TO_BYTES( 384 ):
442 return( MBEDTLS_ECP_DP_BP384R1 );
443 case PSA_BITS_TO_BYTES( 512 ):
444 return( MBEDTLS_ECP_DP_BP512R1 );
445 default:
446 return( MBEDTLS_ECP_DP_NONE );
447 }
448 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100449
Paul Elliott8ff510a2020-06-02 17:19:28 +0100450 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100451 switch( byte_length )
452 {
453 case PSA_BITS_TO_BYTES( 255 ):
454 return( MBEDTLS_ECP_DP_CURVE25519 );
455 case PSA_BITS_TO_BYTES( 448 ):
456 return( MBEDTLS_ECP_DP_CURVE448 );
457 default:
458 return( MBEDTLS_ECP_DP_NONE );
459 }
460 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100461
Paul Elliott8ff510a2020-06-02 17:19:28 +0100462 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100463 switch( byte_length )
464 {
465 case PSA_BITS_TO_BYTES( 192 ):
466 return( MBEDTLS_ECP_DP_SECP192K1 );
467 case PSA_BITS_TO_BYTES( 224 ):
468 return( MBEDTLS_ECP_DP_SECP224K1 );
469 case PSA_BITS_TO_BYTES( 256 ):
470 return( MBEDTLS_ECP_DP_SECP256K1 );
471 default:
472 return( MBEDTLS_ECP_DP_NONE );
473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
Gilles Peskine12313cd2018-06-20 00:20:32 +0200476 default:
477 return( MBEDTLS_ECP_DP_NONE );
478 }
479}
John Durkop9814fa22020-11-04 12:28:15 -0800480#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800481 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
482 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
483 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200484
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200485static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
486 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487{
488 /* Check that the bit size is acceptable for the key type */
489 switch( type )
490 {
491 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200493 case PSA_KEY_TYPE_DERIVE:
494 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495#if defined(MBEDTLS_AES_C)
496 case PSA_KEY_TYPE_AES:
497 if( bits != 128 && bits != 192 && bits != 256 )
498 return( PSA_ERROR_INVALID_ARGUMENT );
499 break;
500#endif
501#if defined(MBEDTLS_CAMELLIA_C)
502 case PSA_KEY_TYPE_CAMELLIA:
503 if( bits != 128 && bits != 192 && bits != 256 )
504 return( PSA_ERROR_INVALID_ARGUMENT );
505 break;
506#endif
507#if defined(MBEDTLS_DES_C)
508 case PSA_KEY_TYPE_DES:
509 if( bits != 64 && bits != 128 && bits != 192 )
510 return( PSA_ERROR_INVALID_ARGUMENT );
511 break;
512#endif
513#if defined(MBEDTLS_ARC4_C)
514 case PSA_KEY_TYPE_ARC4:
515 if( bits < 8 || bits > 2048 )
516 return( PSA_ERROR_INVALID_ARGUMENT );
517 break;
518#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200519#if defined(MBEDTLS_CHACHA20_C)
520 case PSA_KEY_TYPE_CHACHA20:
521 if( bits != 256 )
522 return( PSA_ERROR_INVALID_ARGUMENT );
523 break;
524#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200525 default:
526 return( PSA_ERROR_NOT_SUPPORTED );
527 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200528 if( bits % 8 != 0 )
529 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200531 return( PSA_SUCCESS );
532}
533
John Durkop0e005192020-10-31 22:06:54 -0700534#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
535 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
536 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800537 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
538 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
539 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200540
Gilles Peskine86a440b2018-11-12 18:39:40 +0100541/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
542 * that are not a multiple of 8) well. For example, there is only
543 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
544 * way to return the exact bit size of a key.
545 * To keep things simple, reject non-byte-aligned key sizes. */
546static psa_status_t psa_check_rsa_key_byte_aligned(
547 const mbedtls_rsa_context *rsa )
548{
549 mbedtls_mpi n;
550 psa_status_t status;
551 mbedtls_mpi_init( &n );
552 status = mbedtls_to_psa_error(
553 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
554 if( status == PSA_SUCCESS )
555 {
556 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
557 status = PSA_ERROR_NOT_SUPPORTED;
558 }
559 mbedtls_mpi_free( &n );
560 return( status );
561}
562
Steven Cooreman7f391872020-07-30 14:57:44 +0200563/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200564 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200565 * \param[in] type The type of key contained in \p data.
566 * \param[in] data The buffer from which to load the representation.
567 * \param[in] data_length The size in bytes of \p data.
568 * \param[out] p_rsa Returns a pointer to an RSA context on success.
569 * The caller is responsible for freeing both the
570 * contents of the context and the context itself
571 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200572 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200573static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
574 const uint8_t *data,
575 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200576 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200577{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000578 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200579 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000580 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200581 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000582
583 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200584 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000585 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200586 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200587 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000588 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200589 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000590 if( status != PSA_SUCCESS )
591 goto exit;
592
593 /* We have something that the pkparse module recognizes. If it is a
594 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200595 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200596 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000597 status = PSA_ERROR_INVALID_ARGUMENT;
598 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200599 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000600
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000601 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
602 * supports non-byte-aligned key sizes, but not well. For example,
603 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200604 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000605 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
606 {
607 status = PSA_ERROR_NOT_SUPPORTED;
608 goto exit;
609 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200610 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200611 if( status != PSA_SUCCESS )
612 goto exit;
613
Steven Cooremana2371e52020-07-28 14:30:39 +0200614 /* Copy out the pointer to the RSA context, and reset the PK context
615 * such that pk_free doesn't free the RSA context we just grabbed. */
616 *p_rsa = mbedtls_pk_rsa( ctx );
617 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000618
619exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200620 mbedtls_pk_free( &ctx );
621 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200622}
623
John Durkop6ba40d12020-11-10 08:50:04 -0800624#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
625 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
626 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
627 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
628 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
629 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
630
631#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
632 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
633
Steven Cooremana01795d2020-07-24 22:48:15 +0200634/** Export an RSA key to export representation
635 *
636 * \param[in] type The type of key (public/private) to export
637 * \param[in] rsa The internal RSA representation from which to export
638 * \param[out] data The buffer to export to
639 * \param[in] data_size The length of the buffer to export to
640 * \param[out] data_length The amount of bytes written to \p data
641 */
642static psa_status_t psa_export_rsa_key( psa_key_type_t type,
643 mbedtls_rsa_context *rsa,
644 uint8_t *data,
645 size_t data_size,
646 size_t *data_length )
647{
648#if defined(MBEDTLS_PK_WRITE_C)
649 int ret;
650 mbedtls_pk_context pk;
651 uint8_t *pos = data + data_size;
652
653 mbedtls_pk_init( &pk );
654 pk.pk_info = &mbedtls_rsa_info;
655 pk.pk_ctx = rsa;
656
657 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200658 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
659 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200660 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
661 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
662 else
663 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
664
665 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200666 {
667 /* Clean up in case pk_write failed halfway through. */
668 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200669 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200670 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200671
672 /* The mbedtls_pk_xxx functions write to the end of the buffer.
673 * Move the data to the beginning and erase remaining data
674 * at the original location. */
675 if( 2 * (size_t) ret <= data_size )
676 {
677 memcpy( data, data + data_size - ret, ret );
678 memset( data + data_size - ret, 0, ret );
679 }
680 else if( (size_t) ret < data_size )
681 {
682 memmove( data, data + data_size - ret, ret );
683 memset( data + ret, 0, data_size - ret );
684 }
685
686 *data_length = ret;
687 return( PSA_SUCCESS );
688#else
689 (void) type;
690 (void) rsa;
691 (void) data;
692 (void) data_size;
693 (void) data_length;
694 return( PSA_ERROR_NOT_SUPPORTED );
695#endif /* MBEDTLS_PK_WRITE_C */
696}
697
698/** Import an RSA key from import representation to a slot
699 *
700 * \param[in,out] slot The slot where to store the export representation to
701 * \param[in] data The buffer containing the import representation
702 * \param[in] data_length The amount of bytes in \p data
703 */
704static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
705 const uint8_t *data,
706 size_t data_length )
707{
708 psa_status_t status;
709 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200710 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200711
Steven Cooremana01795d2020-07-24 22:48:15 +0200712 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200713 status = psa_load_rsa_representation( slot->attr.type,
714 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200715 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200716 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200717 if( status != PSA_SUCCESS )
718 goto exit;
719
720 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200721 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200722
Steven Cooreman75b74362020-07-28 14:30:13 +0200723 /* Re-export the data to PSA export format, such that we can store export
724 * representation in the key slot. Export representation in case of RSA is
725 * the smallest representation that's allowed as input, so a straight-up
726 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200727 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200728 if( output == NULL )
729 {
730 status = PSA_ERROR_INSUFFICIENT_MEMORY;
731 goto exit;
732 }
733
Steven Cooremana01795d2020-07-24 22:48:15 +0200734 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200735 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200736 output,
737 data_length,
738 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200739exit:
740 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200741 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200742 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200743
744 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000745 if( status != PSA_SUCCESS )
746 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200747 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000748 return( status );
749 }
750
Steven Cooremana01795d2020-07-24 22:48:15 +0200751 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100752 slot->key.data = output;
753 slot->key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000754
755 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200756}
John Durkop6ba40d12020-11-10 08:50:04 -0800757
758#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800759 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200760
John Durkop9814fa22020-11-04 12:28:15 -0800761#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800762 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
763 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
764 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
765 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200766/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200767 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200768 * \param[in] type The type of key contained in \p data.
769 * \param[in] data The buffer from which to load the representation.
770 * \param[in] data_length The size in bytes of \p data.
771 * \param[out] p_ecp Returns a pointer to an ECP context on success.
772 * The caller is responsible for freeing both the
773 * contents of the context and the context itself
774 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200775 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200776static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
777 const uint8_t *data,
778 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200779 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100780{
781 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200782 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200783 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200784 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100785
Steven Cooreman4fed4552020-08-03 14:46:03 +0200786 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
787 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100788 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200789 /* A Weierstrass public key is represented as:
790 * - The byte 0x04;
791 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
792 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200793 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200794 */
795 if( ( data_length & 1 ) == 0 )
796 return( PSA_ERROR_INVALID_ARGUMENT );
797 curve_size = data_length / 2;
798
799 /* Montgomery public keys are represented in compressed format, meaning
800 * their curve_size is equal to the amount of input. */
801
802 /* Private keys are represented in uncompressed private random integer
803 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100804 }
805
Steven Cooreman6d839f02020-07-30 11:36:45 +0200806 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200807 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200808 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200809 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200810 mbedtls_ecp_keypair_init( ecp );
811
Gilles Peskine4cd32772019-12-02 20:49:42 +0100812 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200813 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
814 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100815 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200816 {
817 status = PSA_ERROR_INVALID_ARGUMENT;
818 goto exit;
819 }
820
Steven Cooremanacda8342020-07-24 23:09:52 +0200821 status = mbedtls_to_psa_error(
822 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
823 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200824 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200825
Steven Cooreman6d839f02020-07-30 11:36:45 +0200826 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200827 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200828 {
829 /* Load the public value. */
830 status = mbedtls_to_psa_error(
831 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200832 data,
833 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200834 if( status != PSA_SUCCESS )
835 goto exit;
836
837 /* Check that the point is on the curve. */
838 status = mbedtls_to_psa_error(
839 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
840 if( status != PSA_SUCCESS )
841 goto exit;
842 }
843 else
844 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200845 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200846 status = mbedtls_to_psa_error(
847 mbedtls_ecp_read_key( ecp->grp.id,
848 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data,
850 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200851 if( status != PSA_SUCCESS )
852 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200853 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200854
855 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200856exit:
857 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200858 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200859 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200860 mbedtls_free( ecp );
861 }
862
Steven Cooreman29149862020-08-05 15:43:42 +0200863 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100864}
John Durkop6ba40d12020-11-10 08:50:04 -0800865#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
866 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
867 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
868 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
869 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000870
John Durkop6ba40d12020-11-10 08:50:04 -0800871#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
872 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Export an ECP key to export representation
874 *
875 * \param[in] type The type of key (public/private) to export
876 * \param[in] ecp The internal ECP representation from which to export
877 * \param[out] data The buffer to export to
878 * \param[in] data_size The length of the buffer to export to
879 * \param[out] data_length The amount of bytes written to \p data
880 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200881static psa_status_t psa_export_ecp_key( psa_key_type_t type,
882 mbedtls_ecp_keypair *ecp,
883 uint8_t *data,
884 size_t data_size,
885 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200886{
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000888
Steven Cooremanacda8342020-07-24 23:09:52 +0200889 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200890 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200891 /* Check whether the public part is loaded */
892 if( mbedtls_ecp_is_zero( &ecp->Q ) )
893 {
894 /* Calculate the public key */
895 status = mbedtls_to_psa_error(
896 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100897 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200898 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200899 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 }
901
Steven Cooreman4fed4552020-08-03 14:46:03 +0200902 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200903 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
904 MBEDTLS_ECP_PF_UNCOMPRESSED,
905 data_length,
906 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200907 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200908 if( status != PSA_SUCCESS )
909 memset( data, 0, data_size );
910
Steven Cooreman29149862020-08-05 15:43:42 +0200911 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200912 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200913 else
914 {
Steven Cooreman29149862020-08-05 15:43:42 +0200915 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200916 return( PSA_ERROR_BUFFER_TOO_SMALL );
917
918 status = mbedtls_to_psa_error(
919 mbedtls_ecp_write_key( ecp,
920 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200921 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200922 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200923 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200924 else
925 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 return( status );
928 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200929}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200930
Steven Cooreman4fed4552020-08-03 14:46:03 +0200931/** Import an ECP key from import representation to a slot
932 *
933 * \param[in,out] slot The slot where to store the export representation to
934 * \param[in] data The buffer containing the import representation
935 * \param[in] data_length The amount of bytes in \p data
936 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200937static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
938 const uint8_t *data,
939 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100940{
Steven Cooremanacda8342020-07-24 23:09:52 +0200941 psa_status_t status;
942 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200943 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100944
Steven Cooremanacda8342020-07-24 23:09:52 +0200945 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200946 status = psa_load_ecp_representation( slot->attr.type,
947 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200948 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200949 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100950 if( status != PSA_SUCCESS )
951 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100952
Steven Cooremanacda8342020-07-24 23:09:52 +0200953 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200954 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200955 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200956 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200957
Steven Cooremanacda8342020-07-24 23:09:52 +0200958 /* Re-export the data to PSA export format. There is currently no support
959 * for other input formats then the export format, so this is a 1-1
960 * copy operation. */
961 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200962 if( output == NULL )
963 {
964 status = PSA_ERROR_INSUFFICIENT_MEMORY;
965 goto exit;
966 }
967
968 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200969 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200970 output,
971 data_length,
972 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100973exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200974 /* Always free the PK object (will also free contained ECP context) */
975 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200976 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200977
978 /* Free the allocated buffer only on error. */
979 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100980 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200981 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200982 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100983 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200984
985 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100986 slot->key.data = output;
987 slot->key.bytes = data_length;
Steven Cooremanacda8342020-07-24 23:09:52 +0200988
989 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100990}
John Durkop9814fa22020-11-04 12:28:15 -0800991#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
992 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100993
Gilles Peskineb46bef22019-07-30 21:32:04 +0200994/** Return the size of the key in the given slot, in bits.
995 *
996 * \param[in] slot A key slot.
997 *
998 * \return The key size in bits, read from the metadata in the slot.
999 */
1000static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
1001{
1002 return( slot->attr.bits );
1003}
1004
Steven Cooreman75b74362020-07-28 14:30:13 +02001005/** Try to allocate a buffer to an empty key slot.
1006 *
1007 * \param[in,out] slot Key slot to attach buffer to.
1008 * \param[in] buffer_length Requested size of the buffer.
1009 *
1010 * \retval #PSA_SUCCESS
1011 * The buffer has been successfully allocated.
1012 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1013 * Not enough memory was available for allocation.
1014 * \retval #PSA_ERROR_ALREADY_EXISTS
1015 * Trying to allocate a buffer to a non-empty key slot.
1016 */
1017static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
1018 size_t buffer_length )
1019{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001020 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001021 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001022
Ronald Cronea0f8a62020-11-25 17:52:23 +01001023 slot->key.data = mbedtls_calloc( 1, buffer_length );
1024 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001025 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001026
Ronald Cronea0f8a62020-11-25 17:52:23 +01001027 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001028 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001029}
1030
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001031psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1032 const uint8_t* data,
1033 size_t data_length )
1034{
1035 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1036 data_length );
1037 if( status != PSA_SUCCESS )
1038 return( status );
1039
Ronald Cronea0f8a62020-11-25 17:52:23 +01001040 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001041 return( PSA_SUCCESS );
1042}
1043
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001044/** Import key data into a slot.
1045 *
1046 * `slot->type` must have been set previously.
1047 * This function assumes that the slot does not contain any key material yet.
1048 * On failure, the slot content is unchanged.
1049 *
1050 * Persistent storage is not affected.
1051 *
1052 * \param[in,out] slot The key slot to import data into.
1053 * Its `type` field must have previously been set to
1054 * the desired key type.
1055 * It must not contain any key material yet.
1056 * \param[in] data Buffer containing the key material to parse and import.
1057 * \param data_length Size of \p data in bytes.
1058 *
1059 * \retval #PSA_SUCCESS
1060 * \retval #PSA_ERROR_INVALID_ARGUMENT
1061 * \retval #PSA_ERROR_NOT_SUPPORTED
1062 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1063 */
1064static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1065 const uint8_t *data,
1066 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001067{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001068 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001069 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001071 /* zero-length keys are never supported. */
1072 if( data_length == 0 )
1073 return( PSA_ERROR_NOT_SUPPORTED );
1074
Gilles Peskine8e338702019-07-30 20:06:31 +02001075 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001076 {
Steven Cooreman04524762020-10-13 17:43:44 +02001077 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001078
Steven Cooreman75b74362020-07-28 14:30:13 +02001079 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1080 if( data_length > SIZE_MAX / 8 )
1081 return( PSA_ERROR_NOT_SUPPORTED );
1082
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001083 /* Enforce a size limit, and in particular ensure that the bit
1084 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001085 if( bit_size > PSA_MAX_KEY_BITS )
1086 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001087
1088 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001089 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001090 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001091
1092 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001093 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001094 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001095 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001096
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001097 /* Write the actual key size to the slot.
1098 * psa_start_key_creation() wrote the size declared by the
1099 * caller, which may be 0 (meaning unspecified) or wrong. */
1100 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001101
1102 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001103 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001104 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001105 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001106 /* Try validation through accelerators first. */
1107 bit_size = slot->attr.bits;
1108 psa_key_attributes_t attributes = {
1109 .core = slot->attr
1110 };
1111 status = psa_driver_wrapper_validate_key( &attributes,
1112 data,
1113 data_length,
1114 &bit_size );
1115 if( status == PSA_SUCCESS )
1116 {
1117 /* Key has been validated successfully by an accelerator.
1118 * Copy key material into slot. */
1119 status = psa_copy_key_material_into_slot( slot, data, data_length );
1120 if( status != PSA_SUCCESS )
1121 return( status );
1122
1123 slot->attr.bits = (psa_key_bits_t) bit_size;
1124 return( PSA_SUCCESS );
1125 }
1126 else if( status != PSA_ERROR_NOT_SUPPORTED )
1127 return( status );
1128
1129 /* Key format is not supported by any accelerator, try software fallback
1130 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001131#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1132 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001133 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1134 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001135 return( psa_import_ecp_key( slot, data, data_length ) );
1136 }
John Durkop9814fa22020-11-04 12:28:15 -08001137#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1138 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001139#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1140 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001141 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001142 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001143 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001144 }
John Durkop9814fa22020-11-04 12:28:15 -08001145#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1146 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001147
1148 /* Fell through the fallback as well, so have nothing else to try. */
1149 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001150 }
1151 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001152 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001153 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001154 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001155 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001156}
1157
Gilles Peskinef603c712019-01-19 13:40:11 +01001158/** Calculate the intersection of two algorithm usage policies.
1159 *
1160 * Return 0 (which allows no operation) on incompatibility.
1161 */
1162static psa_algorithm_t psa_key_policy_algorithm_intersection(
1163 psa_algorithm_t alg1,
1164 psa_algorithm_t alg2 )
1165{
Gilles Peskine549ea862019-05-22 11:45:59 +02001166 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001167 if( alg1 == alg2 )
1168 return( alg1 );
1169 /* If the policies are from the same hash-and-sign family, check
1170 * if one is a wildcard. If so the other has the specific algorithm. */
1171 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1172 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1173 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1174 {
1175 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1176 return( alg2 );
1177 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1178 return( alg1 );
1179 }
1180 /* If the policies are incompatible, allow nothing. */
1181 return( 0 );
1182}
1183
Gilles Peskined6f371b2019-05-10 19:33:38 +02001184static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1185 psa_algorithm_t requested_alg )
1186{
Gilles Peskine549ea862019-05-22 11:45:59 +02001187 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001188 if( requested_alg == policy_alg )
1189 return( 1 );
1190 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001191 * and requested_alg is the same hash-and-sign family with any hash,
1192 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001193 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1194 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1195 {
1196 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1197 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1198 }
Steven Cooremance48e852020-10-05 16:02:45 +02001199 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001200 * a key derivation with that key agreement should also be allowed. This
1201 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001202 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1203 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1204 {
1205 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1206 policy_alg );
1207 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001208 /* If it isn't permitted, it's forbidden. */
1209 return( 0 );
1210}
1211
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001212/** Test whether a policy permits an algorithm.
1213 *
1214 * The caller must test usage flags separately.
1215 */
1216static int psa_key_policy_permits( const psa_key_policy_t *policy,
1217 psa_algorithm_t alg )
1218{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001219 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1220 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001221}
1222
Gilles Peskinef603c712019-01-19 13:40:11 +01001223/** Restrict a key policy based on a constraint.
1224 *
1225 * \param[in,out] policy The policy to restrict.
1226 * \param[in] constraint The policy constraint to apply.
1227 *
1228 * \retval #PSA_SUCCESS
1229 * \c *policy contains the intersection of the original value of
1230 * \c *policy and \c *constraint.
1231 * \retval #PSA_ERROR_INVALID_ARGUMENT
1232 * \c *policy and \c *constraint are incompatible.
1233 * \c *policy is unchanged.
1234 */
1235static psa_status_t psa_restrict_key_policy(
1236 psa_key_policy_t *policy,
1237 const psa_key_policy_t *constraint )
1238{
1239 psa_algorithm_t intersection_alg =
1240 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001241 psa_algorithm_t intersection_alg2 =
1242 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001243 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1244 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001245 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1246 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001247 policy->usage &= constraint->usage;
1248 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001249 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001250 return( PSA_SUCCESS );
1251}
1252
Ronald Cron5c522922020-11-14 16:35:34 +01001253/** Get the description of a key given its identifier and policy constraints
1254 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001255 *
Ronald Cron5c522922020-11-14 16:35:34 +01001256 * The key must have allow all the usage flags set in \p usage. If \p alg is
1257 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001258 *
Ronald Cron5c522922020-11-14 16:35:34 +01001259 * In case of a persistent key, the function loads the description of the key
1260 * into a key slot if not already done.
1261 *
1262 * On success, the returned key slot is locked. It is the responsibility of
1263 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001264 */
Ronald Cron5c522922020-11-14 16:35:34 +01001265static psa_status_t psa_get_and_lock_key_slot_with_policy(
1266 mbedtls_svc_key_id_t key,
1267 psa_key_slot_t **p_slot,
1268 psa_key_usage_t usage,
1269 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001270{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1272 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001273
Ronald Cron5c522922020-11-14 16:35:34 +01001274 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001275 if( status != PSA_SUCCESS )
1276 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001277 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001278
1279 /* Enforce that usage policy for the key slot contains all the flags
1280 * required by the usage parameter. There is one exception: public
1281 * keys can always be exported, so we treat public key objects as
1282 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001283 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001284 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001285
1286 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001287 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001288 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001289
1290 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001291 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001292 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001293
Darryl Green06fd18d2018-07-16 11:21:11 +01001294 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001295
1296error:
1297 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001298 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001299
1300 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001301}
Darryl Green940d72c2018-07-13 13:18:51 +01001302
Ronald Cron5c522922020-11-14 16:35:34 +01001303/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001304 *
1305 * A transparent key is a key for which the key material is directly
1306 * available, as opposed to a key in a secure element.
1307 *
Ronald Cron5c522922020-11-14 16:35:34 +01001308 * This is a temporary function to use instead of
1309 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1310 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001311 *
Ronald Cron5c522922020-11-14 16:35:34 +01001312 * On success, the returned key slot is locked. It is the responsibility of the
1313 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001314 */
1315#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001316static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1317 mbedtls_svc_key_id_t key,
1318 psa_key_slot_t **p_slot,
1319 psa_key_usage_t usage,
1320 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001321{
Ronald Cron5c522922020-11-14 16:35:34 +01001322 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1323 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001324 if( status != PSA_SUCCESS )
1325 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001326
Gilles Peskineadad8132019-07-25 11:31:23 +02001327 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001328 {
Ronald Cron5c522922020-11-14 16:35:34 +01001329 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001330 *p_slot = NULL;
1331 return( PSA_ERROR_NOT_SUPPORTED );
1332 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001333
Gilles Peskine28f8f302019-07-24 13:30:31 +02001334 return( PSA_SUCCESS );
1335}
1336#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1337/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001338#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1339 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001340#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1341
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001342/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001343static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001344{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001345 /* Data pointer will always be either a valid pointer or NULL in an
1346 * initialized slot, so we can just free it. */
1347 if( slot->key.data != NULL )
1348 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1349
1350 mbedtls_free( slot->key.data );
1351 slot->key.data = NULL;
1352 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001353
1354 return( PSA_SUCCESS );
1355}
1356
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001357/** Completely wipe a slot in memory, including its policy.
1358 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001359psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001360{
1361 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001362
1363 /*
1364 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001365 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001366 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1367 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001368 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001369 */
Ronald Cron5c522922020-11-14 16:35:34 +01001370 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001371 {
1372#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001373 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001374#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001375 status = PSA_ERROR_CORRUPTION_DETECTED;
1376 }
1377
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001378 /* Multipart operations may still be using the key. This is safe
1379 * because all multipart operation objects are independent from
1380 * the key slot: if they need to access the key after the setup
1381 * phase, they have a copy of the key. Note that this means that
1382 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001383 /* At this point, key material and other type-specific content has
1384 * been wiped. Clear remaining metadata. We can call memset and not
1385 * zeroize because the metadata is not particularly sensitive. */
1386 memset( slot, 0, sizeof( *slot ) );
1387 return( status );
1388}
1389
Ronald Croncf56a0a2020-08-04 09:51:30 +02001390psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001391{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001392 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001393 psa_status_t status; /* status of the last operation */
1394 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001395#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1396 psa_se_drv_table_entry_t *driver;
1397#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001398
Ronald Croncf56a0a2020-08-04 09:51:30 +02001399 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001400 return( PSA_SUCCESS );
1401
Ronald Cronf2911112020-10-29 17:51:10 +01001402 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001403 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001404 * key, this will load the key description from persistent memory if not
1405 * done yet. We cannot avoid this loading as without it we don't know if
1406 * the key is operated by an SE or not and this information is needed by
1407 * the current implementation.
1408 */
Ronald Cron5c522922020-11-14 16:35:34 +01001409 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001410 if( status != PSA_SUCCESS )
1411 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001412
Ronald Cronf2911112020-10-29 17:51:10 +01001413 /*
1414 * If the key slot containing the key description is under access by the
1415 * library (apart from the present access), the key cannot be destroyed
1416 * yet. For the time being, just return in error. Eventually (to be
1417 * implemented), the key should be destroyed when all accesses have
1418 * stopped.
1419 */
Ronald Cron5c522922020-11-14 16:35:34 +01001420 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001421 {
Ronald Cron5c522922020-11-14 16:35:34 +01001422 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001423 return( PSA_ERROR_GENERIC_ERROR );
1424 }
1425
Gilles Peskine354f7672019-07-12 23:46:38 +02001426#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001427 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001428 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001429 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001430 /* For a key in a secure element, we need to do three things:
1431 * remove the key file in internal storage, destroy the
1432 * key inside the secure element, and update the driver's
1433 * persistent data. Start a transaction that will encompass these
1434 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001435 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001436 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001437 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001438 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001439 status = psa_crypto_save_transaction( );
1440 if( status != PSA_SUCCESS )
1441 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001442 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001443 /* We should still try to destroy the key in the secure
1444 * element and the key metadata in storage. This is especially
1445 * important if the error is that the storage is full.
1446 * But how to do it exactly without risking an inconsistent
1447 * state after a reset?
1448 * https://github.com/ARMmbed/mbed-crypto/issues/215
1449 */
1450 overall_status = status;
1451 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001452 }
1453
Ronald Cronea0f8a62020-11-25 17:52:23 +01001454 status = psa_destroy_se_key( driver,
1455 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001456 if( overall_status == PSA_SUCCESS )
1457 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001458 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001459#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1460
Darryl Greend49a4992018-06-18 17:27:26 +01001461#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001462 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001463 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001464 status = psa_destroy_persistent_key( slot->attr.id );
1465 if( overall_status == PSA_SUCCESS )
1466 overall_status = status;
1467
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001468 /* TODO: other slots may have a copy of the same key. We should
1469 * invalidate them.
1470 * https://github.com/ARMmbed/mbed-crypto/issues/214
1471 */
Darryl Greend49a4992018-06-18 17:27:26 +01001472 }
1473#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001474
Gilles Peskinefc762652019-07-22 19:30:34 +02001475#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1476 if( driver != NULL )
1477 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001478 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001479 if( overall_status == PSA_SUCCESS )
1480 overall_status = status;
1481 status = psa_crypto_stop_transaction( );
1482 if( overall_status == PSA_SUCCESS )
1483 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001484 }
1485#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1486
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001487#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1488exit:
1489#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001490 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001491 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1492 if( overall_status == PSA_SUCCESS )
1493 overall_status = status;
1494 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001495}
1496
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001497void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001498{
Gilles Peskineb699f072019-04-26 16:06:02 +02001499 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001500 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001501}
1502
Gilles Peskineb699f072019-04-26 16:06:02 +02001503psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1504 psa_key_type_t type,
1505 const uint8_t *data,
1506 size_t data_length )
1507{
1508 uint8_t *copy = NULL;
1509
1510 if( data_length != 0 )
1511 {
1512 copy = mbedtls_calloc( 1, data_length );
1513 if( copy == NULL )
1514 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1515 memcpy( copy, data, data_length );
1516 }
1517 /* After this point, this function is guaranteed to succeed, so it
1518 * can start modifying `*attributes`. */
1519
1520 if( attributes->domain_parameters != NULL )
1521 {
1522 mbedtls_free( attributes->domain_parameters );
1523 attributes->domain_parameters = NULL;
1524 attributes->domain_parameters_size = 0;
1525 }
1526
1527 attributes->domain_parameters = copy;
1528 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001529 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001530 return( PSA_SUCCESS );
1531}
1532
1533psa_status_t psa_get_key_domain_parameters(
1534 const psa_key_attributes_t *attributes,
1535 uint8_t *data, size_t data_size, size_t *data_length )
1536{
1537 if( attributes->domain_parameters_size > data_size )
1538 return( PSA_ERROR_BUFFER_TOO_SMALL );
1539 *data_length = attributes->domain_parameters_size;
1540 if( attributes->domain_parameters_size != 0 )
1541 memcpy( data, attributes->domain_parameters,
1542 attributes->domain_parameters_size );
1543 return( PSA_SUCCESS );
1544}
1545
John Durkop07cc04a2020-11-16 22:08:34 -08001546#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001547 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001548static psa_status_t psa_get_rsa_public_exponent(
1549 const mbedtls_rsa_context *rsa,
1550 psa_key_attributes_t *attributes )
1551{
1552 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001554 uint8_t *buffer = NULL;
1555 size_t buflen;
1556 mbedtls_mpi_init( &mpi );
1557
1558 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1559 if( ret != 0 )
1560 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001561 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1562 {
1563 /* It's the default value, which is reported as an empty string,
1564 * so there's nothing to do. */
1565 goto exit;
1566 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001567
1568 buflen = mbedtls_mpi_size( &mpi );
1569 buffer = mbedtls_calloc( 1, buflen );
1570 if( buffer == NULL )
1571 {
1572 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1573 goto exit;
1574 }
1575 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1576 if( ret != 0 )
1577 goto exit;
1578 attributes->domain_parameters = buffer;
1579 attributes->domain_parameters_size = buflen;
1580
1581exit:
1582 mbedtls_mpi_free( &mpi );
1583 if( ret != 0 )
1584 mbedtls_free( buffer );
1585 return( mbedtls_to_psa_error( ret ) );
1586}
John Durkop07cc04a2020-11-16 22:08:34 -08001587#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001588 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001589
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001590/** Retrieve all the publicly-accessible attributes of a key.
1591 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001592psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001593 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001595 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001596 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001597 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001598
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001599 psa_reset_key_attributes( attributes );
1600
Ronald Cron5c522922020-11-14 16:35:34 +01001601 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001602 if( status != PSA_SUCCESS )
1603 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001604
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001605 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001606 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1607 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1608
1609#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1610 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001611 psa_set_key_slot_number( attributes,
1612 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001613#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001614
Gilles Peskine8e338702019-07-30 20:06:31 +02001615 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001616 {
John Durkop07cc04a2020-11-16 22:08:34 -08001617#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001618 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001619 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001620 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001621#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001622 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001623 * is not yet implemented.
1624 * https://github.com/ARMmbed/mbed-crypto/issues/216
1625 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001626 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001627 break;
1628#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001629 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001630 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001631
Steven Cooreman4fed4552020-08-03 14:46:03 +02001632 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001633 slot->key.data,
1634 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001635 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001636 if( status != PSA_SUCCESS )
1637 break;
1638
Steven Cooremana2371e52020-07-28 14:30:39 +02001639 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001640 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001641 mbedtls_rsa_free( rsa );
1642 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001643 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001644 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001645#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001646 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001647 default:
1648 /* Nothing else to do. */
1649 break;
1650 }
1651
1652 if( status != PSA_SUCCESS )
1653 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001654
Ronald Cron5c522922020-11-14 16:35:34 +01001655 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001656
Ronald Cron5c522922020-11-14 16:35:34 +01001657 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001658}
1659
Gilles Peskinec8000c02019-08-02 20:15:51 +02001660#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1661psa_status_t psa_get_key_slot_number(
1662 const psa_key_attributes_t *attributes,
1663 psa_key_slot_number_t *slot_number )
1664{
1665 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1666 {
1667 *slot_number = attributes->slot_number;
1668 return( PSA_SUCCESS );
1669 }
1670 else
1671 return( PSA_ERROR_INVALID_ARGUMENT );
1672}
1673#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1674
Steven Cooremana01795d2020-07-24 22:48:15 +02001675static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1676 uint8_t *data,
1677 size_t data_size,
1678 size_t *data_length )
1679{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001680 if( slot->key.bytes > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001681 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Cronea0f8a62020-11-25 17:52:23 +01001682 memcpy( data, slot->key.data, slot->key.bytes );
1683 memset( data + slot->key.bytes, 0,
1684 data_size - slot->key.bytes );
1685 *data_length = slot->key.bytes;
Steven Cooremana01795d2020-07-24 22:48:15 +02001686 return( PSA_SUCCESS );
1687}
1688
Gilles Peskinef603c712019-01-19 13:40:11 +01001689static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1690 uint8_t *data,
1691 size_t data_size,
1692 size_t *data_length,
1693 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001694{
Gilles Peskine5d309672019-07-12 23:47:28 +02001695#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1696 const psa_drv_se_t *drv;
1697 psa_drv_se_context_t *drv_context;
1698#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1699
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001700 *data_length = 0;
1701
Gilles Peskine8e338702019-07-30 20:06:31 +02001702 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001703 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001704
Gilles Peskinef9168942019-09-12 19:20:29 +02001705 /* Reject a zero-length output buffer now, since this can never be a
1706 * valid key representation. This way we know that data must be a valid
1707 * pointer and we can do things like memset(data, ..., data_size). */
1708 if( data_size == 0 )
1709 return( PSA_ERROR_BUFFER_TOO_SMALL );
1710
Gilles Peskine5d309672019-07-12 23:47:28 +02001711#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001712 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001713 {
1714 psa_drv_se_export_key_t method;
1715 if( drv->key_management == NULL )
1716 return( PSA_ERROR_NOT_SUPPORTED );
1717 method = ( export_public_key ?
1718 drv->key_management->p_export_public :
1719 drv->key_management->p_export );
1720 if( method == NULL )
1721 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001722 return( method( drv_context,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001723 psa_key_slot_get_slot_number( slot ),
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001724 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001725 }
1726#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1727
Gilles Peskine8e338702019-07-30 20:06:31 +02001728 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001729 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001730 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001731 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001732 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1733 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001734 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001735 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001736 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001737 /* Exporting public -> public */
1738 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1739 }
1740 else if( !export_public_key )
1741 {
1742 /* Exporting private -> private */
1743 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1744 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001745
Steven Cooreman560c28a2020-07-24 23:20:24 +02001746 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001747 * so conversion is needed. Try the accelerators first. */
1748 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1749 data,
1750 data_size,
1751 data_length );
1752
1753 if( status != PSA_ERROR_NOT_SUPPORTED ||
1754 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1755 return( status );
1756
Steven Cooreman560c28a2020-07-24 23:20:24 +02001757 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1758 {
John Durkop0e005192020-10-31 22:06:54 -07001759#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1760 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001761 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001762 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001763 slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001764 slot->key.data,
1765 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001766 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001767 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001768 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001769
Steven Cooreman560c28a2020-07-24 23:20:24 +02001770 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001771 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001772 data,
1773 data_size,
1774 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001775
Steven Cooremana2371e52020-07-28 14:30:39 +02001776 mbedtls_rsa_free( rsa );
1777 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001778
Steven Cooreman560c28a2020-07-24 23:20:24 +02001779 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001780#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001781 /* We don't know how to convert a private RSA key to public. */
1782 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001783#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1784 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001785 }
1786 else
Moran Pekera998bc62018-04-16 18:16:20 +03001787 {
John Durkop6ba40d12020-11-10 08:50:04 -08001788#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1789 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001790 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001791 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001792 slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001793 slot->key.data,
1794 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001795 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001796 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001797 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001798
1799 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1800 PSA_KEY_TYPE_ECC_GET_FAMILY(
1801 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001802 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001803 data,
1804 data_size,
1805 data_length );
1806
Steven Cooremana2371e52020-07-28 14:30:39 +02001807 mbedtls_ecp_keypair_free( ecp );
1808 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001809 return( status );
1810#else
1811 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001812 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001813#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1814 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001815 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001816 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001817 else
1818 {
1819 /* This shouldn't happen in the reference implementation, but
1820 it is valid for a special-purpose implementation to omit
1821 support for exporting certain key types. */
1822 return( PSA_ERROR_NOT_SUPPORTED );
1823 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001824}
1825
Ronald Croncf56a0a2020-08-04 09:51:30 +02001826psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001827 uint8_t *data,
1828 size_t data_size,
1829 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001830{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001831 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001832 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001833 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001834
1835 /* Set the key to empty now, so that even when there are errors, we always
1836 * set data_length to a value between 0 and data_size. On error, setting
1837 * the key to empty is a good choice because an empty key representation is
1838 * unlikely to be accepted anywhere. */
1839 *data_length = 0;
1840
1841 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001842 * which don't require any flag, but
1843 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1844 */
1845 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1846 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001847 if( status != PSA_SUCCESS )
1848 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001849
1850 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001851 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001852
Ronald Cron5c522922020-11-14 16:35:34 +01001853 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001854}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001855
Ronald Croncf56a0a2020-08-04 09:51:30 +02001856psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001857 uint8_t *data,
1858 size_t data_size,
1859 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001860{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001861 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001862 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001863 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001864
1865 /* Set the key to empty now, so that even when there are errors, we always
1866 * set data_length to a value between 0 and data_size. On error, setting
1867 * the key to empty is a good choice because an empty key representation is
1868 * unlikely to be accepted anywhere. */
1869 *data_length = 0;
1870
1871 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001872 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001873 if( status != PSA_SUCCESS )
1874 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001875
1876 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001877 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001878
Ronald Cron5c522922020-11-14 16:35:34 +01001879 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001880}
1881
Gilles Peskine91e8c332019-08-02 19:19:39 +02001882#if defined(static_assert)
1883static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1884 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001885static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001886 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001887static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001888 "One or more key attribute flag is listed as both internal-only and external-only" );
1889#endif
1890
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001891/** Validate that a key policy is internally well-formed.
1892 *
1893 * This function only rejects invalid policies. It does not validate the
1894 * consistency of the policy with respect to other attributes of the key
1895 * such as the key type.
1896 */
1897static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001898{
1899 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001900 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001901 PSA_KEY_USAGE_ENCRYPT |
1902 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001903 PSA_KEY_USAGE_SIGN_HASH |
1904 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001905 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1906 return( PSA_ERROR_INVALID_ARGUMENT );
1907
Gilles Peskine4747d192019-04-17 15:05:45 +02001908 return( PSA_SUCCESS );
1909}
1910
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001911/** Validate the internal consistency of key attributes.
1912 *
1913 * This function only rejects invalid attribute values. If does not
1914 * validate the consistency of the attributes with any key data that may
1915 * be involved in the creation of the key.
1916 *
1917 * Call this function early in the key creation process.
1918 *
1919 * \param[in] attributes Key attributes for the new key.
1920 * \param[out] p_drv On any return, the driver for the key, if any.
1921 * NULL for a transparent key.
1922 *
1923 */
1924static psa_status_t psa_validate_key_attributes(
1925 const psa_key_attributes_t *attributes,
1926 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001927{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001928 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001929 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001930 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001931
Ronald Cron54b90082020-10-29 15:26:43 +01001932 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001933 if( status != PSA_SUCCESS )
1934 return( status );
1935
Ronald Crond2ed4812020-07-17 16:11:30 +02001936 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001937 if( status != PSA_SUCCESS )
1938 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001939
Ronald Cron65f38a32020-10-23 17:11:13 +02001940 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1941 {
1942 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1943 return( PSA_ERROR_INVALID_ARGUMENT );
1944 }
1945 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001946 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001947 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001948 if( status != PSA_SUCCESS )
1949 return( status );
1950 }
1951
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001952 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001953 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001954 return( status );
1955
1956 /* Refuse to create overly large keys.
1957 * Note that this doesn't trigger on import if the attributes don't
1958 * explicitly specify a size (so psa_get_key_bits returns 0), so
1959 * psa_import_key() needs its own checks. */
1960 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1961 return( PSA_ERROR_NOT_SUPPORTED );
1962
Gilles Peskine91e8c332019-08-02 19:19:39 +02001963 /* Reject invalid flags. These should not be reachable through the API. */
1964 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1965 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1966 return( PSA_ERROR_INVALID_ARGUMENT );
1967
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001968 return( PSA_SUCCESS );
1969}
1970
Gilles Peskine4747d192019-04-17 15:05:45 +02001971/** Prepare a key slot to receive key material.
1972 *
1973 * This function allocates a key slot and sets its metadata.
1974 *
1975 * If this function fails, call psa_fail_key_creation().
1976 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001977 * This function is intended to be used as follows:
1978 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001979 * it with the specified attributes, and in case of a volatile key assign it
1980 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001981 * -# Populate the slot with the key material.
1982 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1983 * In case of failure at any step, stop the sequence and call
1984 * psa_fail_key_creation().
1985 *
Ronald Cron5c522922020-11-14 16:35:34 +01001986 * On success, the key slot is locked. It is the responsibility of the caller
1987 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001988 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001989 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001990 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001991 * \param[out] p_slot On success, a pointer to the prepared slot.
1992 * \param[out] p_drv On any return, the driver for the key, if any.
1993 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001994 *
1995 * \retval #PSA_SUCCESS
1996 * The key slot is ready to receive key material.
1997 * \return If this function fails, the key slot is an invalid state.
1998 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001999 */
2000static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02002001 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02002002 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002003 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002004 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02002005{
2006 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02002007 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02002008 psa_key_slot_t *slot;
2009
Gilles Peskinedf179142019-07-15 22:02:14 +02002010 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02002011 *p_drv = NULL;
2012
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002013 status = psa_validate_key_attributes( attributes, p_drv );
2014 if( status != PSA_SUCCESS )
2015 return( status );
2016
Ronald Cronc4d1b512020-07-31 11:26:37 +02002017 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02002018 if( status != PSA_SUCCESS )
2019 return( status );
2020 slot = *p_slot;
2021
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002022 /* We're storing the declared bit-size of the key. It's up to each
2023 * creation mechanism to verify that this information is correct.
2024 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002025 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002026 * is optional (import, copy). In case of a volatile key, assign it the
2027 * volatile key identifier associated to the slot returned to contain its
2028 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002029
2030 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002031 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2032 {
2033#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2034 slot->attr.id = volatile_key_id;
2035#else
2036 slot->attr.id.key_id = volatile_key_id;
2037#endif
2038 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002039
Gilles Peskine91e8c332019-08-02 19:19:39 +02002040 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002041 * external-only flags, query `attributes`. Thanks to the check
2042 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002043 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002044 * may have set. */
2045 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002046
Gilles Peskinecbaff462019-07-12 23:46:04 +02002047#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002048 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002049 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002050 * create the key file in internal storage, create the
2051 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002052 * persistent data. This is done by starting a transaction that will
2053 * encompass these three actions.
2054 * For registering a volatile key, we just need to find an appropriate
2055 * slot number inside the SE. Since the key is designated volatile, creating
2056 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002057 /* The first thing to do is to find a slot number for the new key.
2058 * We save the slot number in persistent storage as part of the
2059 * transaction data. It will be needed to recover if the power
2060 * fails during the key creation process, to clean up on the secure
2061 * element side after restarting. Obtaining a slot number from the
2062 * secure element driver updates its persistent state, but we do not yet
2063 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002064 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002065 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002066 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01002067 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002068 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002069 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002070 if( status != PSA_SUCCESS )
2071 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002072
2073 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002074 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002075 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2076 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01002077 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002078 psa_crypto_transaction.key.id = slot->attr.id;
2079 status = psa_crypto_save_transaction( );
2080 if( status != PSA_SUCCESS )
2081 {
2082 (void) psa_crypto_stop_transaction( );
2083 return( status );
2084 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002085 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01002086
2087 status = psa_copy_key_material_into_slot(
2088 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00002089 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002090
2091 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2092 {
2093 /* Key registration only makes sense with a secure element. */
2094 return( PSA_ERROR_INVALID_ARGUMENT );
2095 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2097
Ronald Cronc4d1b512020-07-31 11:26:37 +02002098 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002099}
Gilles Peskine4747d192019-04-17 15:05:45 +02002100
2101/** Finalize the creation of a key once its key material has been set.
2102 *
2103 * This entails writing the key to persistent storage.
2104 *
2105 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002106 * See the documentation of psa_start_key_creation() for the intended use
2107 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002108 *
Ronald Cron5c522922020-11-14 16:35:34 +01002109 * If the finalization succeeds, the function unlocks the key slot (it was
2110 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2111 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002112 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002113 * \param[in,out] slot Pointer to the slot with key material.
2114 * \param[in] driver The secure element driver for the key,
2115 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002116 * \param[out] key On success, identifier of the key. Note that the
2117 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002118 *
2119 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002120 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002121 * \return If this function fails, the key slot is an invalid state.
2122 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002123 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002124static psa_status_t psa_finish_key_creation(
2125 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002126 psa_se_drv_table_entry_t *driver,
2127 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002128{
2129 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002130 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002131 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002132
2133#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002134 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002135 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002136#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2137 if( driver != NULL )
2138 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002139 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01002140 psa_key_slot_number_t slot_number =
2141 psa_key_slot_get_slot_number( slot ) ;
2142
Gilles Peskineb46bef22019-07-30 21:32:04 +02002143#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01002144 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02002145 sizeof( data.slot_number ),
2146 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002147#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01002148 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002149 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002150 (uint8_t*) &data,
2151 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002152 }
2153 else
2154#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2155 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002156 /* Key material is saved in export representation in the slot, so
2157 * just pass the slot buffer for storage. */
2158 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002159 slot->key.data,
2160 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002161 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002162 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002163#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2164
Gilles Peskinecbaff462019-07-12 23:46:04 +02002165#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002166 /* Finish the transaction for a key creation. This does not
2167 * happen when registering an existing key. Detect this case
2168 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002169 * creation of a persistent key in a secure element requires a transaction,
2170 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002171 if( driver != NULL &&
2172 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002173 {
2174 status = psa_save_se_persistent_data( driver );
2175 if( status != PSA_SUCCESS )
2176 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002177 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002178 return( status );
2179 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002180 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002181 }
2182#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2183
Ronald Cron50972942020-11-14 11:28:25 +01002184 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002185 {
2186 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002187 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002188 if( status != PSA_SUCCESS )
2189 *key = MBEDTLS_SVC_KEY_ID_INIT;
2190 }
Ronald Cron50972942020-11-14 11:28:25 +01002191
Gilles Peskine4747d192019-04-17 15:05:45 +02002192 return( status );
2193}
2194
2195/** Abort the creation of a key.
2196 *
2197 * You may call this function after calling psa_start_key_creation(),
2198 * or after psa_finish_key_creation() fails. In other circumstances, this
2199 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002200 * See the documentation of psa_start_key_creation() for the intended use
2201 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002202 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002203 * \param[in,out] slot Pointer to the slot with key material.
2204 * \param[in] driver The secure element driver for the key,
2205 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002206 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002207static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002208 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002209{
Gilles Peskine011e4282019-06-26 18:34:38 +02002210 (void) driver;
2211
Gilles Peskine4747d192019-04-17 15:05:45 +02002212 if( slot == NULL )
2213 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002214
2215#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002216 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002217 * element, and the failure happened later (when saving metadata
2218 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002219 * element.
2220 * https://github.com/ARMmbed/mbed-crypto/issues/217
2221 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002222
Gilles Peskined7729582019-08-05 15:55:54 +02002223 /* Abort the ongoing transaction if any (there may not be one if
2224 * the creation process failed before starting one, or if the
2225 * key creation is a registration of a key in a secure element).
2226 * Earlier functions must already have done what it takes to undo any
2227 * partial creation. All that's left is to update the transaction data
2228 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002229 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002230#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2231
Gilles Peskine4747d192019-04-17 15:05:45 +02002232 psa_wipe_key_slot( slot );
2233}
2234
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002235/** Validate optional attributes during key creation.
2236 *
2237 * Some key attributes are optional during key creation. If they are
2238 * specified in the attributes structure, check that they are consistent
2239 * with the data in the slot.
2240 *
2241 * This function should be called near the end of key creation, after
2242 * the slot in memory is fully populated but before saving persistent data.
2243 */
2244static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002245 const psa_key_slot_t *slot,
2246 const psa_key_attributes_t *attributes )
2247{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002248 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002249 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002250 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002251 return( PSA_ERROR_INVALID_ARGUMENT );
2252 }
2253
2254 if( attributes->domain_parameters_size != 0 )
2255 {
John Durkop0e005192020-10-31 22:06:54 -07002256#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2257 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002258 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002259 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002260 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002261 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002263
Steven Cooreman7f391872020-07-30 14:57:44 +02002264 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002265 slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002266 slot->key.data,
2267 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002268 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002269 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002270 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002271
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002272 mbedtls_mpi_init( &actual );
2273 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002274 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002275 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002276 mbedtls_rsa_free( rsa );
2277 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002278 if( ret != 0 )
2279 goto rsa_exit;
2280 ret = mbedtls_mpi_read_binary( &required,
2281 attributes->domain_parameters,
2282 attributes->domain_parameters_size );
2283 if( ret != 0 )
2284 goto rsa_exit;
2285 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2286 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2287 rsa_exit:
2288 mbedtls_mpi_free( &actual );
2289 mbedtls_mpi_free( &required );
2290 if( ret != 0)
2291 return( mbedtls_to_psa_error( ret ) );
2292 }
2293 else
John Durkop9814fa22020-11-04 12:28:15 -08002294#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2295 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002296 {
2297 return( PSA_ERROR_INVALID_ARGUMENT );
2298 }
2299 }
2300
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002301 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002302 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002303 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002304 return( PSA_ERROR_INVALID_ARGUMENT );
2305 }
2306
2307 return( PSA_SUCCESS );
2308}
2309
Gilles Peskine4747d192019-04-17 15:05:45 +02002310psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002311 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002312 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002313 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002314{
2315 psa_status_t status;
2316 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002317 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002318
Ronald Cron81709fc2020-11-14 12:10:32 +01002319 *key = MBEDTLS_SVC_KEY_ID_INIT;
2320
Gilles Peskine0f84d622019-09-12 19:03:13 +02002321 /* Reject zero-length symmetric keys (including raw data key objects).
2322 * This also rejects any key which might be encoded as an empty string,
2323 * which is never valid. */
2324 if( data_length == 0 )
2325 return( PSA_ERROR_INVALID_ARGUMENT );
2326
Gilles Peskinedf179142019-07-15 22:02:14 +02002327 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002328 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002329 if( status != PSA_SUCCESS )
2330 goto exit;
2331
Gilles Peskine5d309672019-07-12 23:47:28 +02002332#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2333 if( driver != NULL )
2334 {
2335 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002336 /* The driver should set the number of key bits, however in
2337 * case it doesn't, we initialize bits to an invalid value. */
2338 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002339 if( drv->key_management == NULL ||
2340 drv->key_management->p_import == NULL )
2341 {
2342 status = PSA_ERROR_NOT_SUPPORTED;
2343 goto exit;
2344 }
2345 status = drv->key_management->p_import(
2346 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01002347 psa_key_slot_get_slot_number( slot ),
2348 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002349 if( status != PSA_SUCCESS )
2350 goto exit;
2351 if( bits > PSA_MAX_KEY_BITS )
2352 {
2353 status = PSA_ERROR_NOT_SUPPORTED;
2354 goto exit;
2355 }
2356 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002357 }
2358 else
2359#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002360 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2361 {
2362 /* Importing a key with external lifetime through the driver wrapper
2363 * interface is not yet supported. Return as if this was an invalid
2364 * lifetime. */
2365 status = PSA_ERROR_INVALID_ARGUMENT;
2366 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002367 }
2368 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002369 {
2370 status = psa_import_key_into_slot( slot, data, data_length );
2371 if( status != PSA_SUCCESS )
2372 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002373 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002374 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002375 if( status != PSA_SUCCESS )
2376 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002377
Ronald Cron81709fc2020-11-14 12:10:32 +01002378 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002379exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002380 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002381 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002382
Gilles Peskine4747d192019-04-17 15:05:45 +02002383 return( status );
2384}
2385
Gilles Peskined7729582019-08-05 15:55:54 +02002386#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2387psa_status_t mbedtls_psa_register_se_key(
2388 const psa_key_attributes_t *attributes )
2389{
2390 psa_status_t status;
2391 psa_key_slot_t *slot = NULL;
2392 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002393 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002394
2395 /* Leaving attributes unspecified is not currently supported.
2396 * It could make sense to query the key type and size from the
2397 * secure element, but not all secure elements support this
2398 * and the driver HAL doesn't currently support it. */
2399 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2400 return( PSA_ERROR_NOT_SUPPORTED );
2401 if( psa_get_key_bits( attributes ) == 0 )
2402 return( PSA_ERROR_NOT_SUPPORTED );
2403
2404 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002405 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002406 if( status != PSA_SUCCESS )
2407 goto exit;
2408
Ronald Cron81709fc2020-11-14 12:10:32 +01002409 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002410
2411exit:
2412 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002413 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002414
Gilles Peskined7729582019-08-05 15:55:54 +02002415 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002416 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002417 return( status );
2418}
2419#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2420
Gilles Peskinef603c712019-01-19 13:40:11 +01002421static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002422 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002423{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002424 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002425 source->key.data,
2426 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002427 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002428 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002429
Steven Cooreman398aee52020-10-13 14:35:45 +02002430 target->attr.type = source->attr.type;
2431 target->attr.bits = source->attr.bits;
2432
2433 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002434}
2435
Ronald Croncf56a0a2020-08-04 09:51:30 +02002436psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002437 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002438 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002439{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002440 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002441 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002442 psa_key_slot_t *source_slot = NULL;
2443 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002444 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002445 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002446
Ronald Cron81709fc2020-11-14 12:10:32 +01002447 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2448
Ronald Cron5c522922020-11-14 16:35:34 +01002449 status = psa_get_and_lock_transparent_key_slot_with_policy(
2450 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002451 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002452 goto exit;
2453
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002454 status = psa_validate_optional_attributes( source_slot,
2455 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002456 if( status != PSA_SUCCESS )
2457 goto exit;
2458
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002459 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002460 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002461 if( status != PSA_SUCCESS )
2462 goto exit;
2463
Ronald Cron81709fc2020-11-14 12:10:32 +01002464 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2465 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002466 if( status != PSA_SUCCESS )
2467 goto exit;
2468
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002469#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2470 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002471 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002472 /* Copying to a secure element is not implemented yet. */
2473 status = PSA_ERROR_NOT_SUPPORTED;
2474 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002475 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002476#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002477
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002478 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002479 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002480 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002481
Ronald Cron81709fc2020-11-14 12:10:32 +01002482 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002483exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002484 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002485 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002486
Ronald Cron5c522922020-11-14 16:35:34 +01002487 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002488
Ronald Cron5c522922020-11-14 16:35:34 +01002489 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002490}
2491
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002492
2493
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002494/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002495/* Message digests */
2496/****************************************************************/
2497
John Durkop07cc04a2020-11-16 22:08:34 -08002498#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002499 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2500 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002501 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002502static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002503{
2504 switch( alg )
2505 {
John Durkopee4e6602020-11-27 08:48:46 -08002506#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002507 case PSA_ALG_MD2:
2508 return( &mbedtls_md2_info );
2509#endif
John Durkopee4e6602020-11-27 08:48:46 -08002510#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002511 case PSA_ALG_MD4:
2512 return( &mbedtls_md4_info );
2513#endif
John Durkopee4e6602020-11-27 08:48:46 -08002514#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002515 case PSA_ALG_MD5:
2516 return( &mbedtls_md5_info );
2517#endif
John Durkopee4e6602020-11-27 08:48:46 -08002518#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002519 case PSA_ALG_RIPEMD160:
2520 return( &mbedtls_ripemd160_info );
2521#endif
John Durkopee4e6602020-11-27 08:48:46 -08002522#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002523 case PSA_ALG_SHA_1:
2524 return( &mbedtls_sha1_info );
2525#endif
John Durkopee4e6602020-11-27 08:48:46 -08002526#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002527 case PSA_ALG_SHA_224:
2528 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002529#endif
2530#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002531 case PSA_ALG_SHA_256:
2532 return( &mbedtls_sha256_info );
2533#endif
John Durkopee4e6602020-11-27 08:48:46 -08002534#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002535 case PSA_ALG_SHA_384:
2536 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002537#endif
John Durkopee4e6602020-11-27 08:48:46 -08002538#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002539 case PSA_ALG_SHA_512:
2540 return( &mbedtls_sha512_info );
2541#endif
2542 default:
2543 return( NULL );
2544 }
2545}
John Durkop07cc04a2020-11-16 22:08:34 -08002546#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002547 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2548 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2549 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002550
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002551psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2552{
2553 switch( operation->alg )
2554 {
Gilles Peskine81736312018-06-26 15:04:31 +02002555 case 0:
2556 /* The object has (apparently) been initialized but it is not
2557 * in use. It's ok to call abort on such an object, and there's
2558 * nothing to do. */
2559 break;
John Durkopee4e6602020-11-27 08:48:46 -08002560#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002561 case PSA_ALG_MD2:
2562 mbedtls_md2_free( &operation->ctx.md2 );
2563 break;
2564#endif
John Durkopee4e6602020-11-27 08:48:46 -08002565#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002566 case PSA_ALG_MD4:
2567 mbedtls_md4_free( &operation->ctx.md4 );
2568 break;
2569#endif
John Durkopee4e6602020-11-27 08:48:46 -08002570#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002571 case PSA_ALG_MD5:
2572 mbedtls_md5_free( &operation->ctx.md5 );
2573 break;
2574#endif
John Durkopee4e6602020-11-27 08:48:46 -08002575#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002576 case PSA_ALG_RIPEMD160:
2577 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2578 break;
2579#endif
John Durkopee4e6602020-11-27 08:48:46 -08002580#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002581 case PSA_ALG_SHA_1:
2582 mbedtls_sha1_free( &operation->ctx.sha1 );
2583 break;
2584#endif
John Durkop6ca23272020-12-03 06:01:32 -08002585#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002586 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002587 mbedtls_sha256_free( &operation->ctx.sha256 );
2588 break;
2589#endif
2590#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002591 case PSA_ALG_SHA_256:
2592 mbedtls_sha256_free( &operation->ctx.sha256 );
2593 break;
2594#endif
John Durkop6ca23272020-12-03 06:01:32 -08002595#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002596 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002597 mbedtls_sha512_free( &operation->ctx.sha512 );
2598 break;
2599#endif
2600#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002601 case PSA_ALG_SHA_512:
2602 mbedtls_sha512_free( &operation->ctx.sha512 );
2603 break;
2604#endif
2605 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002606 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002607 }
2608 operation->alg = 0;
2609 return( PSA_SUCCESS );
2610}
2611
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002612psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002613 psa_algorithm_t alg )
2614{
Janos Follath24eed8d2019-11-22 13:21:35 +00002615 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002616
2617 /* A context must be freshly initialized before it can be set up. */
2618 if( operation->alg != 0 )
2619 {
2620 return( PSA_ERROR_BAD_STATE );
2621 }
2622
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002623 switch( alg )
2624 {
John Durkopee4e6602020-11-27 08:48:46 -08002625#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002626 case PSA_ALG_MD2:
2627 mbedtls_md2_init( &operation->ctx.md2 );
2628 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2629 break;
2630#endif
John Durkopee4e6602020-11-27 08:48:46 -08002631#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002632 case PSA_ALG_MD4:
2633 mbedtls_md4_init( &operation->ctx.md4 );
2634 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2635 break;
2636#endif
John Durkopee4e6602020-11-27 08:48:46 -08002637#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002638 case PSA_ALG_MD5:
2639 mbedtls_md5_init( &operation->ctx.md5 );
2640 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2641 break;
2642#endif
John Durkopee4e6602020-11-27 08:48:46 -08002643#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002644 case PSA_ALG_RIPEMD160:
2645 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2646 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2647 break;
2648#endif
John Durkopee4e6602020-11-27 08:48:46 -08002649#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002650 case PSA_ALG_SHA_1:
2651 mbedtls_sha1_init( &operation->ctx.sha1 );
2652 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2653 break;
2654#endif
John Durkopee4e6602020-11-27 08:48:46 -08002655#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002656 case PSA_ALG_SHA_224:
2657 mbedtls_sha256_init( &operation->ctx.sha256 );
2658 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2659 break;
John Durkopee4e6602020-11-27 08:48:46 -08002660#endif
2661#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002662 case PSA_ALG_SHA_256:
2663 mbedtls_sha256_init( &operation->ctx.sha256 );
2664 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2665 break;
2666#endif
John Durkopee4e6602020-11-27 08:48:46 -08002667#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002668 case PSA_ALG_SHA_384:
2669 mbedtls_sha512_init( &operation->ctx.sha512 );
2670 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2671 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002672#endif
John Durkopee4e6602020-11-27 08:48:46 -08002673#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002674 case PSA_ALG_SHA_512:
2675 mbedtls_sha512_init( &operation->ctx.sha512 );
2676 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2677 break;
2678#endif
2679 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002680 return( PSA_ALG_IS_HASH( alg ) ?
2681 PSA_ERROR_NOT_SUPPORTED :
2682 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002683 }
2684 if( ret == 0 )
2685 operation->alg = alg;
2686 else
2687 psa_hash_abort( operation );
2688 return( mbedtls_to_psa_error( ret ) );
2689}
2690
2691psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2692 const uint8_t *input,
2693 size_t input_length )
2694{
Janos Follath24eed8d2019-11-22 13:21:35 +00002695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002696
2697 /* Don't require hash implementations to behave correctly on a
2698 * zero-length input, which may have an invalid pointer. */
2699 if( input_length == 0 )
2700 return( PSA_SUCCESS );
2701
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002702 switch( operation->alg )
2703 {
John Durkopee4e6602020-11-27 08:48:46 -08002704#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002705 case PSA_ALG_MD2:
2706 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2707 input, input_length );
2708 break;
2709#endif
John Durkopee4e6602020-11-27 08:48:46 -08002710#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002711 case PSA_ALG_MD4:
2712 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2713 input, input_length );
2714 break;
2715#endif
John Durkopee4e6602020-11-27 08:48:46 -08002716#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002717 case PSA_ALG_MD5:
2718 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2719 input, input_length );
2720 break;
2721#endif
John Durkopee4e6602020-11-27 08:48:46 -08002722#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002723 case PSA_ALG_RIPEMD160:
2724 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2725 input, input_length );
2726 break;
2727#endif
John Durkopee4e6602020-11-27 08:48:46 -08002728#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002729 case PSA_ALG_SHA_1:
2730 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2731 input, input_length );
2732 break;
2733#endif
John Durkop6ca23272020-12-03 06:01:32 -08002734#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002735 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002736 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2737 input, input_length );
2738 break;
2739#endif
2740#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002741 case PSA_ALG_SHA_256:
2742 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2743 input, input_length );
2744 break;
2745#endif
John Durkop6ca23272020-12-03 06:01:32 -08002746#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002747 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002748 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2749 input, input_length );
2750 break;
2751#endif
2752#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002753 case PSA_ALG_SHA_512:
2754 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2755 input, input_length );
2756 break;
2757#endif
2758 default:
John Durkopee4e6602020-11-27 08:48:46 -08002759 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002760 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002761 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002762
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002763 if( ret != 0 )
2764 psa_hash_abort( operation );
2765 return( mbedtls_to_psa_error( ret ) );
2766}
2767
2768psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2769 uint8_t *hash,
2770 size_t hash_size,
2771 size_t *hash_length )
2772{
itayzafrir40835d42018-08-02 13:14:17 +03002773 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002775 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002776
2777 /* Fill the output buffer with something that isn't a valid hash
2778 * (barring an attack on the hash and deliberately-crafted input),
2779 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002780 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002781 /* If hash_size is 0 then hash may be NULL and then the
2782 * call to memset would have undefined behavior. */
2783 if( hash_size != 0 )
2784 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002785
2786 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002787 {
2788 status = PSA_ERROR_BUFFER_TOO_SMALL;
2789 goto exit;
2790 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002791
2792 switch( operation->alg )
2793 {
John Durkopee4e6602020-11-27 08:48:46 -08002794#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002795 case PSA_ALG_MD2:
2796 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2797 break;
2798#endif
John Durkopee4e6602020-11-27 08:48:46 -08002799#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002800 case PSA_ALG_MD4:
2801 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2802 break;
2803#endif
John Durkopee4e6602020-11-27 08:48:46 -08002804#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002805 case PSA_ALG_MD5:
2806 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2807 break;
2808#endif
John Durkopee4e6602020-11-27 08:48:46 -08002809#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002810 case PSA_ALG_RIPEMD160:
2811 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2812 break;
2813#endif
John Durkopee4e6602020-11-27 08:48:46 -08002814#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002815 case PSA_ALG_SHA_1:
2816 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2817 break;
2818#endif
John Durkop6ca23272020-12-03 06:01:32 -08002819#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002820 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002821 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2822 break;
2823#endif
2824#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002825 case PSA_ALG_SHA_256:
2826 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2827 break;
2828#endif
John Durkop6ca23272020-12-03 06:01:32 -08002829#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002830 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002831 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2832 break;
2833#endif
2834#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002835 case PSA_ALG_SHA_512:
2836 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2837 break;
2838#endif
2839 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002840 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002841 }
itayzafrir40835d42018-08-02 13:14:17 +03002842 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002843
itayzafrir40835d42018-08-02 13:14:17 +03002844exit:
2845 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002846 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002847 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002848 return( psa_hash_abort( operation ) );
2849 }
2850 else
2851 {
2852 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002853 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002854 }
2855}
2856
Gilles Peskine2d277862018-06-18 15:41:12 +02002857psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2858 const uint8_t *hash,
2859 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002860{
2861 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2862 size_t actual_hash_length;
2863 psa_status_t status = psa_hash_finish( operation,
2864 actual_hash, sizeof( actual_hash ),
2865 &actual_hash_length );
2866 if( status != PSA_SUCCESS )
2867 return( status );
2868 if( actual_hash_length != hash_length )
2869 return( PSA_ERROR_INVALID_SIGNATURE );
2870 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2871 return( PSA_ERROR_INVALID_SIGNATURE );
2872 return( PSA_SUCCESS );
2873}
2874
Gilles Peskine0a749c82019-11-28 19:33:58 +01002875psa_status_t psa_hash_compute( psa_algorithm_t alg,
2876 const uint8_t *input, size_t input_length,
2877 uint8_t *hash, size_t hash_size,
2878 size_t *hash_length )
2879{
2880 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2881 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2882
2883 *hash_length = hash_size;
2884 status = psa_hash_setup( &operation, alg );
2885 if( status != PSA_SUCCESS )
2886 goto exit;
2887 status = psa_hash_update( &operation, input, input_length );
2888 if( status != PSA_SUCCESS )
2889 goto exit;
2890 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2891 if( status != PSA_SUCCESS )
2892 goto exit;
2893
2894exit:
2895 if( status == PSA_SUCCESS )
2896 status = psa_hash_abort( &operation );
2897 else
2898 psa_hash_abort( &operation );
2899 return( status );
2900}
2901
2902psa_status_t psa_hash_compare( psa_algorithm_t alg,
2903 const uint8_t *input, size_t input_length,
2904 const uint8_t *hash, size_t hash_length )
2905{
2906 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2907 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2908
2909 status = psa_hash_setup( &operation, alg );
2910 if( status != PSA_SUCCESS )
2911 goto exit;
2912 status = psa_hash_update( &operation, input, input_length );
2913 if( status != PSA_SUCCESS )
2914 goto exit;
2915 status = psa_hash_verify( &operation, hash, hash_length );
2916 if( status != PSA_SUCCESS )
2917 goto exit;
2918
2919exit:
2920 if( status == PSA_SUCCESS )
2921 status = psa_hash_abort( &operation );
2922 else
2923 psa_hash_abort( &operation );
2924 return( status );
2925}
2926
Gilles Peskineeb35d782019-01-22 17:56:16 +01002927psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2928 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002929{
2930 if( target_operation->alg != 0 )
2931 return( PSA_ERROR_BAD_STATE );
2932
2933 switch( source_operation->alg )
2934 {
2935 case 0:
2936 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002937#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002938 case PSA_ALG_MD2:
2939 mbedtls_md2_clone( &target_operation->ctx.md2,
2940 &source_operation->ctx.md2 );
2941 break;
2942#endif
John Durkopee4e6602020-11-27 08:48:46 -08002943#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002944 case PSA_ALG_MD4:
2945 mbedtls_md4_clone( &target_operation->ctx.md4,
2946 &source_operation->ctx.md4 );
2947 break;
2948#endif
John Durkopee4e6602020-11-27 08:48:46 -08002949#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002950 case PSA_ALG_MD5:
2951 mbedtls_md5_clone( &target_operation->ctx.md5,
2952 &source_operation->ctx.md5 );
2953 break;
2954#endif
John Durkopee4e6602020-11-27 08:48:46 -08002955#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002956 case PSA_ALG_RIPEMD160:
2957 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2958 &source_operation->ctx.ripemd160 );
2959 break;
2960#endif
John Durkopee4e6602020-11-27 08:48:46 -08002961#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002962 case PSA_ALG_SHA_1:
2963 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2964 &source_operation->ctx.sha1 );
2965 break;
2966#endif
John Durkop6ca23272020-12-03 06:01:32 -08002967#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002968 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002969 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2970 &source_operation->ctx.sha256 );
2971 break;
2972#endif
2973#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002974 case PSA_ALG_SHA_256:
2975 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2976 &source_operation->ctx.sha256 );
2977 break;
2978#endif
John Durkop6ca23272020-12-03 06:01:32 -08002979#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002980 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002981 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2982 &source_operation->ctx.sha512 );
2983 break;
2984#endif
2985#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002986 case PSA_ALG_SHA_512:
2987 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2988 &source_operation->ctx.sha512 );
2989 break;
2990#endif
2991 default:
2992 return( PSA_ERROR_NOT_SUPPORTED );
2993 }
2994
2995 target_operation->alg = source_operation->alg;
2996 return( PSA_SUCCESS );
2997}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002998
2999
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003000/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01003001/* MAC */
3002/****************************************************************/
3003
Gilles Peskinedc2fc842018-03-07 16:42:59 +01003004static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01003005 psa_algorithm_t alg,
3006 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02003007 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03003008 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03003011 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003012
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003013 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003014 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003015
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
3017 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03003018 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003019 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01003020 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021 mode = MBEDTLS_MODE_STREAM;
3022 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003023 case PSA_ALG_CTR:
3024 mode = MBEDTLS_MODE_CTR;
3025 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003026 case PSA_ALG_CFB:
3027 mode = MBEDTLS_MODE_CFB;
3028 break;
3029 case PSA_ALG_OFB:
3030 mode = MBEDTLS_MODE_OFB;
3031 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003032 case PSA_ALG_ECB_NO_PADDING:
3033 mode = MBEDTLS_MODE_ECB;
3034 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003035 case PSA_ALG_CBC_NO_PADDING:
3036 mode = MBEDTLS_MODE_CBC;
3037 break;
3038 case PSA_ALG_CBC_PKCS7:
3039 mode = MBEDTLS_MODE_CBC;
3040 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003041 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042 mode = MBEDTLS_MODE_CCM;
3043 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003044 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003045 mode = MBEDTLS_MODE_GCM;
3046 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003047 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3048 mode = MBEDTLS_MODE_CHACHAPOLY;
3049 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050 default:
3051 return( NULL );
3052 }
3053 }
3054 else if( alg == PSA_ALG_CMAC )
3055 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003056 else
3057 return( NULL );
3058
3059 switch( key_type )
3060 {
3061 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003062 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003063 break;
3064 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003065 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3066 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003067 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003068 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003070 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003071 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3072 * but two-key Triple-DES is functionally three-key Triple-DES
3073 * with K1=K3, so that's how we present it to mbedtls. */
3074 if( key_bits == 128 )
3075 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003076 break;
3077 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003078 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003079 break;
3080 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003081 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003082 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003083 case PSA_KEY_TYPE_CHACHA20:
3084 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3085 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003086 default:
3087 return( NULL );
3088 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003089 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003090 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091
Jaeden Amero23bbb752018-06-26 14:16:54 +01003092 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3093 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003094}
3095
John Durkop6ba40d12020-11-10 08:50:04 -08003096#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003097static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003098{
Gilles Peskine2d277862018-06-18 15:41:12 +02003099 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003100 {
3101 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003102 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003103 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003104 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003105 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003106 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003107 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003108 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003109 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003110 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003111 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003112 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003113 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003114 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003115 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003116 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003117 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003118 return( 128 );
3119 default:
3120 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003121 }
3122}
John Durkop07cc04a2020-11-16 22:08:34 -08003123#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003124
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003125/* Initialize the MAC operation structure. Once this function has been
3126 * called, psa_mac_abort can run and will do the right thing. */
3127static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3128 psa_algorithm_t alg )
3129{
3130 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3131
3132 operation->alg = alg;
3133 operation->key_set = 0;
3134 operation->iv_set = 0;
3135 operation->iv_required = 0;
3136 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003137 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003138
3139#if defined(MBEDTLS_CMAC_C)
3140 if( alg == PSA_ALG_CMAC )
3141 {
3142 operation->iv_required = 0;
3143 mbedtls_cipher_init( &operation->ctx.cmac );
3144 status = PSA_SUCCESS;
3145 }
3146 else
3147#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003148#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003149 if( PSA_ALG_IS_HMAC( operation->alg ) )
3150 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003151 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3152 operation->ctx.hmac.hash_ctx.alg = 0;
3153 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003154 }
3155 else
John Durkopd0321952020-10-29 21:37:36 -07003156#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003157 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003158 if( ! PSA_ALG_IS_MAC( alg ) )
3159 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003160 }
3161
3162 if( status != PSA_SUCCESS )
3163 memset( operation, 0, sizeof( *operation ) );
3164 return( status );
3165}
3166
John Durkop6ba40d12020-11-10 08:50:04 -08003167#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003168static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3169{
Gilles Peskine3f108122018-12-07 18:14:53 +01003170 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003171 return( psa_hash_abort( &hmac->hash_ctx ) );
3172}
John Durkop6ba40d12020-11-10 08:50:04 -08003173#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003174
Gilles Peskine8c9def32018-02-08 10:02:12 +01003175psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3176{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003177 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003178 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003179 /* The object has (apparently) been initialized but it is not
3180 * in use. It's ok to call abort on such an object, and there's
3181 * nothing to do. */
3182 return( PSA_SUCCESS );
3183 }
3184 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003185#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003186 if( operation->alg == PSA_ALG_CMAC )
3187 {
3188 mbedtls_cipher_free( &operation->ctx.cmac );
3189 }
3190 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003191#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003192#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003193 if( PSA_ALG_IS_HMAC( operation->alg ) )
3194 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003195 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003196 }
3197 else
John Durkopd0321952020-10-29 21:37:36 -07003198#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003199 {
3200 /* Sanity check (shouldn't happen: operation->alg should
3201 * always have been initialized to a valid value). */
3202 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003203 }
Moran Peker41deec42018-04-04 15:43:05 +03003204
Gilles Peskine8c9def32018-02-08 10:02:12 +01003205 operation->alg = 0;
3206 operation->key_set = 0;
3207 operation->iv_set = 0;
3208 operation->iv_required = 0;
3209 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003210 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003211
Gilles Peskine8c9def32018-02-08 10:02:12 +01003212 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003213
3214bad_state:
3215 /* If abort is called on an uninitialized object, we can't trust
3216 * anything. Wipe the object in case it contains confidential data.
3217 * This may result in a memory leak if a pointer gets overwritten,
3218 * but it's too late to do anything about this. */
3219 memset( operation, 0, sizeof( *operation ) );
3220 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003221}
3222
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003223#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003224static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003225 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003226 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003227 const mbedtls_cipher_info_t *cipher_info )
3228{
Janos Follath24eed8d2019-11-22 13:21:35 +00003229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003230
3231 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003232
3233 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3234 if( ret != 0 )
3235 return( ret );
3236
3237 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003238 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003239 key_bits );
3240 return( ret );
3241}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003242#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003243
John Durkop6ba40d12020-11-10 08:50:04 -08003244#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003245static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3246 const uint8_t *key,
3247 size_t key_length,
3248 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003249{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003250 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003251 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003252 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003253 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003254 psa_status_t status;
3255
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003256 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3257 * overflow below. This should never trigger if the hash algorithm
3258 * is implemented correctly. */
3259 /* The size checks against the ipad and opad buffers cannot be written
3260 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3261 * because that triggers -Wlogical-op on GCC 7.3. */
3262 if( block_size > sizeof( ipad ) )
3263 return( PSA_ERROR_NOT_SUPPORTED );
3264 if( block_size > sizeof( hmac->opad ) )
3265 return( PSA_ERROR_NOT_SUPPORTED );
3266 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003267 return( PSA_ERROR_NOT_SUPPORTED );
3268
Gilles Peskined223b522018-06-11 18:12:58 +02003269 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003270 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003271 status = psa_hash_compute( hash_alg, key, key_length,
3272 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003273 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003274 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003275 }
Gilles Peskine96889972018-07-12 17:07:03 +02003276 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3277 * but it is permitted. It is common when HMAC is used in HKDF, for
3278 * example. Don't call `memcpy` in the 0-length because `key` could be
3279 * an invalid pointer which would make the behavior undefined. */
3280 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003281 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003282
Gilles Peskined223b522018-06-11 18:12:58 +02003283 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3284 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003285 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003286 ipad[i] ^= 0x36;
3287 memset( ipad + key_length, 0x36, block_size - key_length );
3288
3289 /* Copy the key material from ipad to opad, flipping the requisite bits,
3290 * and filling the rest of opad with the requisite constant. */
3291 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003292 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3293 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003294
Gilles Peskine01126fa2018-07-12 17:04:55 +02003295 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003296 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003297 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003298
Gilles Peskine01126fa2018-07-12 17:04:55 +02003299 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003300
3301cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003302 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003303
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003304 return( status );
3305}
John Durkop6ba40d12020-11-10 08:50:04 -08003306#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003307
Gilles Peskine89167cb2018-07-08 20:12:23 +02003308static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003309 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003310 psa_algorithm_t alg,
3311 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003312{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003313 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003314 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003315 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003316 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003317 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003318 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003319 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003320 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003321
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003322 /* A context must be freshly initialized before it can be set up. */
3323 if( operation->alg != 0 )
3324 {
3325 return( PSA_ERROR_BAD_STATE );
3326 }
3327
Gilles Peskined911eb72018-08-14 15:18:45 +02003328 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003329 if( status != PSA_SUCCESS )
3330 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003331 if( is_sign )
3332 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003333
Ronald Cron5c522922020-11-14 16:35:34 +01003334 status = psa_get_and_lock_transparent_key_slot_with_policy(
3335 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003336 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003337 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003338 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003339
Gilles Peskine8c9def32018-02-08 10:02:12 +01003340#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003341 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003342 {
3343 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003344 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003345 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003347 if( cipher_info == NULL )
3348 {
3349 status = PSA_ERROR_NOT_SUPPORTED;
3350 goto exit;
3351 }
3352 operation->mac_size = cipher_info->block_size;
3353 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3354 status = mbedtls_to_psa_error( ret );
3355 }
3356 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003357#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003358#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003359 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003360 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003361 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003362 if( hash_alg == 0 )
3363 {
3364 status = PSA_ERROR_NOT_SUPPORTED;
3365 goto exit;
3366 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003367
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003368 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003369 /* Sanity check. This shouldn't fail on a valid configuration. */
3370 if( operation->mac_size == 0 ||
3371 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3372 {
3373 status = PSA_ERROR_NOT_SUPPORTED;
3374 goto exit;
3375 }
3376
Gilles Peskine8e338702019-07-30 20:06:31 +02003377 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003378 {
3379 status = PSA_ERROR_INVALID_ARGUMENT;
3380 goto exit;
3381 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003382
Gilles Peskine01126fa2018-07-12 17:04:55 +02003383 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003384 slot->key.data,
3385 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003386 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003387 }
3388 else
John Durkopd0321952020-10-29 21:37:36 -07003389#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003390 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003391 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003392 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003393 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003394
Gilles Peskined911eb72018-08-14 15:18:45 +02003395 if( truncated == 0 )
3396 {
3397 /* The "normal" case: untruncated algorithm. Nothing to do. */
3398 }
3399 else if( truncated < 4 )
3400 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003401 /* A very short MAC is too short for security since it can be
3402 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3403 * so we make this our minimum, even though 32 bits is still
3404 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003405 status = PSA_ERROR_NOT_SUPPORTED;
3406 }
3407 else if( truncated > operation->mac_size )
3408 {
3409 /* It's impossible to "truncate" to a larger length. */
3410 status = PSA_ERROR_INVALID_ARGUMENT;
3411 }
3412 else
3413 operation->mac_size = truncated;
3414
Gilles Peskinefbfac682018-07-08 20:51:54 +02003415exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003416 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003417 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003418 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003419 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003420 else
3421 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003422 operation->key_set = 1;
3423 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003424
Ronald Cron5c522922020-11-14 16:35:34 +01003425 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003426
Ronald Cron5c522922020-11-14 16:35:34 +01003427 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003428}
3429
Gilles Peskine89167cb2018-07-08 20:12:23 +02003430psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003431 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003432 psa_algorithm_t alg )
3433{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003434 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003435}
3436
3437psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003438 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003439 psa_algorithm_t alg )
3440{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003441 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003442}
3443
Gilles Peskine8c9def32018-02-08 10:02:12 +01003444psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3445 const uint8_t *input,
3446 size_t input_length )
3447{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003448 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003449 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003450 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003451 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003452 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003453 operation->has_input = 1;
3454
Gilles Peskine8c9def32018-02-08 10:02:12 +01003455#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003456 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003457 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003458 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3459 input, input_length );
3460 status = mbedtls_to_psa_error( ret );
3461 }
3462 else
3463#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003464#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003465 if( PSA_ALG_IS_HMAC( operation->alg ) )
3466 {
3467 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3468 input_length );
3469 }
3470 else
John Durkopd0321952020-10-29 21:37:36 -07003471#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003472 {
3473 /* This shouldn't happen if `operation` was initialized by
3474 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003475 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003476 }
3477
Gilles Peskinefbfac682018-07-08 20:51:54 +02003478 if( status != PSA_SUCCESS )
3479 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003480 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003481}
3482
John Durkop6ba40d12020-11-10 08:50:04 -08003483#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003484static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3485 uint8_t *mac,
3486 size_t mac_size )
3487{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003488 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003489 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3490 size_t hash_size = 0;
3491 size_t block_size = psa_get_hash_block_size( hash_alg );
3492 psa_status_t status;
3493
Gilles Peskine01126fa2018-07-12 17:04:55 +02003494 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3495 if( status != PSA_SUCCESS )
3496 return( status );
3497 /* From here on, tmp needs to be wiped. */
3498
3499 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3500 if( status != PSA_SUCCESS )
3501 goto exit;
3502
3503 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3504 if( status != PSA_SUCCESS )
3505 goto exit;
3506
3507 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3508 if( status != PSA_SUCCESS )
3509 goto exit;
3510
Gilles Peskined911eb72018-08-14 15:18:45 +02003511 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3512 if( status != PSA_SUCCESS )
3513 goto exit;
3514
3515 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003516
3517exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003518 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003519 return( status );
3520}
John Durkop6ba40d12020-11-10 08:50:04 -08003521#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003522
mohammad16036df908f2018-04-02 08:34:15 -07003523static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003524 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003525 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003526{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003527 if( ! operation->key_set )
3528 return( PSA_ERROR_BAD_STATE );
3529 if( operation->iv_required && ! operation->iv_set )
3530 return( PSA_ERROR_BAD_STATE );
3531
Gilles Peskine8c9def32018-02-08 10:02:12 +01003532 if( mac_size < operation->mac_size )
3533 return( PSA_ERROR_BUFFER_TOO_SMALL );
3534
Gilles Peskine8c9def32018-02-08 10:02:12 +01003535#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003536 if( operation->alg == PSA_ALG_CMAC )
3537 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003538 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003539 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3540 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003541 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003542 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003543 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003544 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003545 else
3546#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003547#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003548 if( PSA_ALG_IS_HMAC( operation->alg ) )
3549 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003550 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003551 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003552 }
3553 else
John Durkopd0321952020-10-29 21:37:36 -07003554#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003555 {
3556 /* This shouldn't happen if `operation` was initialized by
3557 * a setup function. */
3558 return( PSA_ERROR_BAD_STATE );
3559 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003560}
3561
Gilles Peskineacd4be32018-07-08 19:56:25 +02003562psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3563 uint8_t *mac,
3564 size_t mac_size,
3565 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003566{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003567 psa_status_t status;
3568
Jaeden Amero252ef282019-02-15 14:05:35 +00003569 if( operation->alg == 0 )
3570 {
3571 return( PSA_ERROR_BAD_STATE );
3572 }
3573
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003574 /* Fill the output buffer with something that isn't a valid mac
3575 * (barring an attack on the mac and deliberately-crafted input),
3576 * in case the caller doesn't check the return status properly. */
3577 *mac_length = mac_size;
3578 /* If mac_size is 0 then mac may be NULL and then the
3579 * call to memset would have undefined behavior. */
3580 if( mac_size != 0 )
3581 memset( mac, '!', mac_size );
3582
Gilles Peskine89167cb2018-07-08 20:12:23 +02003583 if( ! operation->is_sign )
3584 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003585 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003586 }
mohammad16036df908f2018-04-02 08:34:15 -07003587
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003588 status = psa_mac_finish_internal( operation, mac, mac_size );
3589
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003590 if( status == PSA_SUCCESS )
3591 {
3592 status = psa_mac_abort( operation );
3593 if( status == PSA_SUCCESS )
3594 *mac_length = operation->mac_size;
3595 else
3596 memset( mac, '!', mac_size );
3597 }
3598 else
3599 psa_mac_abort( operation );
3600 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003601}
3602
Gilles Peskineacd4be32018-07-08 19:56:25 +02003603psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3604 const uint8_t *mac,
3605 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003606{
Gilles Peskine828ed142018-06-18 23:25:51 +02003607 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003608 psa_status_t status;
3609
Jaeden Amero252ef282019-02-15 14:05:35 +00003610 if( operation->alg == 0 )
3611 {
3612 return( PSA_ERROR_BAD_STATE );
3613 }
3614
Gilles Peskine89167cb2018-07-08 20:12:23 +02003615 if( operation->is_sign )
3616 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003617 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003618 }
3619 if( operation->mac_size != mac_length )
3620 {
3621 status = PSA_ERROR_INVALID_SIGNATURE;
3622 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003623 }
mohammad16036df908f2018-04-02 08:34:15 -07003624
3625 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003626 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003627 if( status != PSA_SUCCESS )
3628 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003629
3630 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3631 status = PSA_ERROR_INVALID_SIGNATURE;
3632
3633cleanup:
3634 if( status == PSA_SUCCESS )
3635 status = psa_mac_abort( operation );
3636 else
3637 psa_mac_abort( operation );
3638
Gilles Peskine3f108122018-12-07 18:14:53 +01003639 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003640
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003641 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003642}
3643
3644
Gilles Peskine20035e32018-02-03 22:44:14 +01003645
Gilles Peskine20035e32018-02-03 22:44:14 +01003646/****************************************************************/
3647/* Asymmetric cryptography */
3648/****************************************************************/
3649
John Durkop6ba40d12020-11-10 08:50:04 -08003650#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3651 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003652/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003653 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003654static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3655 size_t hash_length,
3656 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003657{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003658 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003659 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003660 *md_alg = mbedtls_md_get_type( md_info );
3661
3662 /* The Mbed TLS RSA module uses an unsigned int for hash length
3663 * parameters. Validate that it fits so that we don't risk an
3664 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003665#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003666 if( hash_length > UINT_MAX )
3667 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003668#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003669
John Durkop0e005192020-10-31 22:06:54 -07003670#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003671 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3672 * must be correct. */
3673 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3674 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003675 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003676 if( md_info == NULL )
3677 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003678 if( mbedtls_md_get_size( md_info ) != hash_length )
3679 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003680 }
John Durkop0e005192020-10-31 22:06:54 -07003681#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003682
John Durkop0e005192020-10-31 22:06:54 -07003683#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003684 /* PSS requires a hash internally. */
3685 if( PSA_ALG_IS_RSA_PSS( alg ) )
3686 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003687 if( md_info == NULL )
3688 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003689 }
John Durkop0e005192020-10-31 22:06:54 -07003690#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003691
Gilles Peskine61b91d42018-06-08 16:09:36 +02003692 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003693}
3694
Gilles Peskine2b450e32018-06-27 15:42:46 +02003695static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3696 psa_algorithm_t alg,
3697 const uint8_t *hash,
3698 size_t hash_length,
3699 uint8_t *signature,
3700 size_t signature_size,
3701 size_t *signature_length )
3702{
3703 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003705 mbedtls_md_type_t md_alg;
3706
3707 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3708 if( status != PSA_SUCCESS )
3709 return( status );
3710
Gilles Peskine630a18a2018-06-29 17:49:35 +02003711 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003712 return( PSA_ERROR_BUFFER_TOO_SMALL );
3713
John Durkop0e005192020-10-31 22:06:54 -07003714#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003715 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3716 {
3717 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3718 MBEDTLS_MD_NONE );
3719 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003720 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003721 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003722 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003723 md_alg,
3724 (unsigned int) hash_length,
3725 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003726 signature );
3727 }
3728 else
John Durkop0e005192020-10-31 22:06:54 -07003729#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3730#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003731 if( PSA_ALG_IS_RSA_PSS( alg ) )
3732 {
3733 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3734 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003735 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003736 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003737 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003738 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003739 (unsigned int) hash_length,
3740 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003741 signature );
3742 }
3743 else
John Durkop0e005192020-10-31 22:06:54 -07003744#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003745 {
3746 return( PSA_ERROR_INVALID_ARGUMENT );
3747 }
3748
3749 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003750 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003751 return( mbedtls_to_psa_error( ret ) );
3752}
3753
3754static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3755 psa_algorithm_t alg,
3756 const uint8_t *hash,
3757 size_t hash_length,
3758 const uint8_t *signature,
3759 size_t signature_length )
3760{
3761 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003762 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003763 mbedtls_md_type_t md_alg;
3764
3765 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3766 if( status != PSA_SUCCESS )
3767 return( status );
3768
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003769 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3770 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003771
John Durkop0e005192020-10-31 22:06:54 -07003772#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003773 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3774 {
3775 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3776 MBEDTLS_MD_NONE );
3777 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003778 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003779 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003780 MBEDTLS_RSA_PUBLIC,
3781 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003782 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003783 hash,
3784 signature );
3785 }
3786 else
John Durkop0e005192020-10-31 22:06:54 -07003787#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3788#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003789 if( PSA_ALG_IS_RSA_PSS( alg ) )
3790 {
3791 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3792 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003793 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003794 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003795 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003796 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003797 (unsigned int) hash_length,
3798 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003799 signature );
3800 }
3801 else
John Durkop0e005192020-10-31 22:06:54 -07003802#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003803 {
3804 return( PSA_ERROR_INVALID_ARGUMENT );
3805 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003806
3807 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3808 * the rest of the signature is invalid". This has little use in
3809 * practice and PSA doesn't report this distinction. */
3810 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3811 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003812 return( mbedtls_to_psa_error( ret ) );
3813}
John Durkop6ba40d12020-11-10 08:50:04 -08003814#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3815 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003816
John Durkop6ba40d12020-11-10 08:50:04 -08003817#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3818 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003819/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3820 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3821 * (even though these functions don't modify it). */
3822static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3823 psa_algorithm_t alg,
3824 const uint8_t *hash,
3825 size_t hash_length,
3826 uint8_t *signature,
3827 size_t signature_size,
3828 size_t *signature_length )
3829{
Janos Follath24eed8d2019-11-22 13:21:35 +00003830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003831 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003832 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003833 mbedtls_mpi_init( &r );
3834 mbedtls_mpi_init( &s );
3835
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003836 if( signature_size < 2 * curve_bytes )
3837 {
3838 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3839 goto cleanup;
3840 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003841
John Durkop0ea39e02020-10-13 19:58:20 -07003842#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003843 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3844 {
3845 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3846 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3847 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003848 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3849 &ecp->d, hash,
3850 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003851 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003852 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003853 }
3854 else
John Durkop0ea39e02020-10-13 19:58:20 -07003855#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003856 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003857 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003858 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3859 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003860 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003861 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003862 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003863
3864 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3865 signature,
3866 curve_bytes ) );
3867 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3868 signature + curve_bytes,
3869 curve_bytes ) );
3870
3871cleanup:
3872 mbedtls_mpi_free( &r );
3873 mbedtls_mpi_free( &s );
3874 if( ret == 0 )
3875 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003876 return( mbedtls_to_psa_error( ret ) );
3877}
3878
3879static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3880 const uint8_t *hash,
3881 size_t hash_length,
3882 const uint8_t *signature,
3883 size_t signature_length )
3884{
Janos Follath24eed8d2019-11-22 13:21:35 +00003885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003886 mbedtls_mpi r, s;
3887 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3888 mbedtls_mpi_init( &r );
3889 mbedtls_mpi_init( &s );
3890
3891 if( signature_length != 2 * curve_bytes )
3892 return( PSA_ERROR_INVALID_SIGNATURE );
3893
3894 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3895 signature,
3896 curve_bytes ) );
3897 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3898 signature + curve_bytes,
3899 curve_bytes ) );
3900
Steven Cooremanacda8342020-07-24 23:09:52 +02003901 /* Check whether the public part is loaded. If not, load it. */
3902 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3903 {
3904 MBEDTLS_MPI_CHK(
3905 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003906 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003907 }
3908
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003909 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3910 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003911
3912cleanup:
3913 mbedtls_mpi_free( &r );
3914 mbedtls_mpi_free( &s );
3915 return( mbedtls_to_psa_error( ret ) );
3916}
John Durkop6ba40d12020-11-10 08:50:04 -08003917#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3918 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003919
Ronald Croncf56a0a2020-08-04 09:51:30 +02003920psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003921 psa_algorithm_t alg,
3922 const uint8_t *hash,
3923 size_t hash_length,
3924 uint8_t *signature,
3925 size_t signature_size,
3926 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003927{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003928 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003929 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003930 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003931
3932 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003933 /* Immediately reject a zero-length signature buffer. This guarantees
3934 * that signature must be a valid pointer. (On the other hand, the hash
3935 * buffer can in principle be empty since it doesn't actually have
3936 * to be a hash.) */
3937 if( signature_size == 0 )
3938 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003939
Ronald Cron5c522922020-11-14 16:35:34 +01003940 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3941 PSA_KEY_USAGE_SIGN_HASH,
3942 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003943 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003944 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003945 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003946 {
3947 status = PSA_ERROR_INVALID_ARGUMENT;
3948 goto exit;
3949 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003950
Steven Cooremancd84cb42020-07-16 20:28:36 +02003951 /* Try any of the available accelerators first */
3952 status = psa_driver_wrapper_sign_hash( slot,
3953 alg,
3954 hash,
3955 hash_length,
3956 signature,
3957 signature_size,
3958 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003959 if( status != PSA_ERROR_NOT_SUPPORTED ||
3960 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003961 goto exit;
3962
Steven Cooreman7a250572020-07-17 16:43:05 +02003963 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003964#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3965 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003966 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003967 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003968 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003969
Steven Cooreman4fed4552020-08-03 14:46:03 +02003970 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003971 slot->key.data,
3972 slot->key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003973 &rsa );
3974 if( status != PSA_SUCCESS )
3975 goto exit;
3976
Steven Cooremana2371e52020-07-28 14:30:39 +02003977 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003978 alg,
3979 hash, hash_length,
3980 signature, signature_size,
3981 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003982
Steven Cooremana2371e52020-07-28 14:30:39 +02003983 mbedtls_rsa_free( rsa );
3984 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003985 }
3986 else
John Durkop6ba40d12020-11-10 08:50:04 -08003987#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3988 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003989 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003990 {
John Durkop9814fa22020-11-04 12:28:15 -08003991#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3992 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003993 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003994#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003995 PSA_ALG_IS_ECDSA( alg )
3996#else
3997 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3998#endif
3999 )
Steven Cooremanacda8342020-07-24 23:09:52 +02004000 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004001 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004002 status = psa_load_ecp_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004003 slot->key.data,
4004 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004005 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004006 if( status != PSA_SUCCESS )
4007 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004008 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004009 alg,
4010 hash, hash_length,
4011 signature, signature_size,
4012 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004013 mbedtls_ecp_keypair_free( ecp );
4014 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004015 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004016 else
John Durkop9814fa22020-11-04 12:28:15 -08004017#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4018 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004019 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004020 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004021 }
itayzafrir5c753392018-05-08 11:18:38 +03004022 }
4023 else
itayzafrir5c753392018-05-08 11:18:38 +03004024 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004025 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004026 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004027
4028exit:
4029 /* Fill the unused part of the output buffer (the whole buffer on error,
4030 * the trailing part on success) with something that isn't a valid mac
4031 * (barring an attack on the mac and deliberately-crafted input),
4032 * in case the caller doesn't check the return status properly. */
4033 if( status == PSA_SUCCESS )
4034 memset( signature + *signature_length, '!',
4035 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02004036 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004037 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004038 /* If signature_size is 0 then we have nothing to do. We must not call
4039 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02004040
Ronald Cron5c522922020-11-14 16:35:34 +01004041 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004042
Ronald Cron5c522922020-11-14 16:35:34 +01004043 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03004044}
4045
Ronald Croncf56a0a2020-08-04 09:51:30 +02004046psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004047 psa_algorithm_t alg,
4048 const uint8_t *hash,
4049 size_t hash_length,
4050 const uint8_t *signature,
4051 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004052{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004053 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004054 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004055 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004056
Ronald Cron5c522922020-11-14 16:35:34 +01004057 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4058 PSA_KEY_USAGE_VERIFY_HASH,
4059 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004060 if( status != PSA_SUCCESS )
4061 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004062
Steven Cooreman55ae2172020-07-17 19:46:15 +02004063 /* Try any of the available accelerators first */
4064 status = psa_driver_wrapper_verify_hash( slot,
4065 alg,
4066 hash,
4067 hash_length,
4068 signature,
4069 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004070 if( status != PSA_ERROR_NOT_SUPPORTED ||
4071 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004072 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004073
John Durkop6ba40d12020-11-10 08:50:04 -08004074#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4075 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004076 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004077 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004078 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004079
Steven Cooreman4fed4552020-08-03 14:46:03 +02004080 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004081 slot->key.data,
4082 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004083 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004084 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004085 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004086
Steven Cooremana2371e52020-07-28 14:30:39 +02004087 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004088 alg,
4089 hash, hash_length,
4090 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004091 mbedtls_rsa_free( rsa );
4092 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004093 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004094 }
4095 else
John Durkop6ba40d12020-11-10 08:50:04 -08004096#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4097 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004098 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004099 {
John Durkop9814fa22020-11-04 12:28:15 -08004100#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4101 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004102 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004103 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004104 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004105 status = psa_load_ecp_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004106 slot->key.data,
4107 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004108 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004109 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004110 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004111 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004112 hash, hash_length,
4113 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004114 mbedtls_ecp_keypair_free( ecp );
4115 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004116 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004117 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004118 else
John Durkop9814fa22020-11-04 12:28:15 -08004119#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4120 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004121 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004122 status = PSA_ERROR_INVALID_ARGUMENT;
4123 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004124 }
itayzafrir5c753392018-05-08 11:18:38 +03004125 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004126 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004127 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004128 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004129 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004130
4131exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004132 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004133
Ronald Cron5c522922020-11-14 16:35:34 +01004134 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004135}
4136
John Durkop0e005192020-10-31 22:06:54 -07004137#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004138static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4139 mbedtls_rsa_context *rsa )
4140{
4141 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4142 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4143 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4144 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4145}
John Durkop0e005192020-10-31 22:06:54 -07004146#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004147
Ronald Croncf56a0a2020-08-04 09:51:30 +02004148psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004149 psa_algorithm_t alg,
4150 const uint8_t *input,
4151 size_t input_length,
4152 const uint8_t *salt,
4153 size_t salt_length,
4154 uint8_t *output,
4155 size_t output_size,
4156 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004157{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004158 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004159 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004160 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004161
Darryl Green5cc689a2018-07-24 15:34:10 +01004162 (void) input;
4163 (void) input_length;
4164 (void) salt;
4165 (void) output;
4166 (void) output_size;
4167
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004168 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004169
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004170 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4171 return( PSA_ERROR_INVALID_ARGUMENT );
4172
Ronald Cron5c522922020-11-14 16:35:34 +01004173 status = psa_get_and_lock_transparent_key_slot_with_policy(
4174 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004175 if( status != PSA_SUCCESS )
4176 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004177 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4178 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004179 {
4180 status = PSA_ERROR_INVALID_ARGUMENT;
4181 goto exit;
4182 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004183
John Durkop6ba40d12020-11-10 08:50:04 -08004184#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4185 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004186 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004187 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004188 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004189 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004190 slot->key.data,
4191 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004192 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004193 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004194 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004195
4196 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004197 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004198 status = PSA_ERROR_BUFFER_TOO_SMALL;
4199 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004200 }
John Durkop0e005192020-10-31 22:06:54 -07004201#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004202 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004203 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004204 status = mbedtls_to_psa_error(
4205 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004206 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004207 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004208 MBEDTLS_RSA_PUBLIC,
4209 input_length,
4210 input,
4211 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004212 }
4213 else
John Durkop0e005192020-10-31 22:06:54 -07004214#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4215#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004216 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004217 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004218 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004219 status = mbedtls_to_psa_error(
4220 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004221 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004222 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004223 MBEDTLS_RSA_PUBLIC,
4224 salt, salt_length,
4225 input_length,
4226 input,
4227 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004228 }
4229 else
John Durkop0e005192020-10-31 22:06:54 -07004230#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004231 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004232 status = PSA_ERROR_INVALID_ARGUMENT;
4233 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004234 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004235rsa_exit:
4236 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004237 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004238
Steven Cooremana2371e52020-07-28 14:30:39 +02004239 mbedtls_rsa_free( rsa );
4240 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004241 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004242 else
John Durkop9814fa22020-11-04 12:28:15 -08004243#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004244 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004245 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004246 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004247 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004248
4249exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004250 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004251
Ronald Cron5c522922020-11-14 16:35:34 +01004252 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004253}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004254
Ronald Croncf56a0a2020-08-04 09:51:30 +02004255psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004256 psa_algorithm_t alg,
4257 const uint8_t *input,
4258 size_t input_length,
4259 const uint8_t *salt,
4260 size_t salt_length,
4261 uint8_t *output,
4262 size_t output_size,
4263 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004264{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004265 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004266 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004267 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004268
Darryl Green5cc689a2018-07-24 15:34:10 +01004269 (void) input;
4270 (void) input_length;
4271 (void) salt;
4272 (void) output;
4273 (void) output_size;
4274
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004275 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004276
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004277 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4278 return( PSA_ERROR_INVALID_ARGUMENT );
4279
Ronald Cron5c522922020-11-14 16:35:34 +01004280 status = psa_get_and_lock_transparent_key_slot_with_policy(
4281 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004282 if( status != PSA_SUCCESS )
4283 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004284 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004285 {
4286 status = PSA_ERROR_INVALID_ARGUMENT;
4287 goto exit;
4288 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004289
John Durkop6ba40d12020-11-10 08:50:04 -08004290#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4291 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004292 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004293 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004294 mbedtls_rsa_context *rsa = NULL;
4295 status = psa_load_rsa_representation( slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004296 slot->key.data,
4297 slot->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004298 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004299 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004300 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004301
Steven Cooremana2371e52020-07-28 14:30:39 +02004302 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004303 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004304 status = PSA_ERROR_INVALID_ARGUMENT;
4305 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004306 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004307
John Durkop0e005192020-10-31 22:06:54 -07004308#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004309 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004310 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004311 status = mbedtls_to_psa_error(
4312 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004313 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004314 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004315 MBEDTLS_RSA_PRIVATE,
4316 output_length,
4317 input,
4318 output,
4319 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004320 }
4321 else
John Durkop0e005192020-10-31 22:06:54 -07004322#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4323#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004324 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004325 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004326 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004327 status = mbedtls_to_psa_error(
4328 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004329 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004330 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004331 MBEDTLS_RSA_PRIVATE,
4332 salt, salt_length,
4333 output_length,
4334 input,
4335 output,
4336 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004337 }
4338 else
John Durkop0e005192020-10-31 22:06:54 -07004339#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004341 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004342 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004343
Steven Cooreman4fed4552020-08-03 14:46:03 +02004344rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004345 mbedtls_rsa_free( rsa );
4346 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004347 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004348 else
John Durkop6ba40d12020-11-10 08:50:04 -08004349#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4350 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004351 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004352 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004353 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004354
4355exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004356 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004357
Ronald Cron5c522922020-11-14 16:35:34 +01004358 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004359}
Gilles Peskine20035e32018-02-03 22:44:14 +01004360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004361
4362
mohammad1603503973b2018-03-12 15:59:30 +02004363/****************************************************************/
4364/* Symmetric cryptography */
4365/****************************************************************/
4366
Gilles Peskinee553c652018-06-04 16:22:46 +02004367static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004368 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004369 psa_algorithm_t alg,
4370 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004371{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004372 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004373 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004374 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004375 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004376 size_t key_bits;
4377 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004378 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4379 PSA_KEY_USAGE_ENCRYPT :
4380 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004381
Steven Cooremand3feccd2020-09-01 15:56:14 +02004382 /* A context must be freshly initialized before it can be set up. */
4383 if( operation->alg != 0 )
4384 return( PSA_ERROR_BAD_STATE );
4385
Steven Cooremana07b9972020-09-10 14:54:14 +02004386 /* The requested algorithm must be one that can be processed by cipher. */
4387 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004388 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004389
Steven Cooremanef8575e2020-09-11 11:44:50 +02004390 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004391 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004392 if( status != PSA_SUCCESS )
4393 goto exit;
4394
4395 /* Initialize the operation struct members, except for alg. The alg member
4396 * is used to indicate to psa_cipher_abort that there are resources to free,
4397 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004398 operation->key_set = 0;
4399 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004400 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004401 operation->iv_size = 0;
4402 operation->block_size = 0;
4403 if( alg == PSA_ALG_ECB_NO_PADDING )
4404 operation->iv_required = 0;
4405 else
4406 operation->iv_required = 1;
4407
Steven Cooremana07b9972020-09-10 14:54:14 +02004408 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004409 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004410 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004411 slot,
4412 alg );
4413 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004414 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004415 slot,
4416 alg );
4417
Steven Cooremanef8575e2020-09-11 11:44:50 +02004418 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004419 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004420 /* Once the driver context is initialised, it needs to be freed using
4421 * psa_cipher_abort. Indicate this through setting alg. */
4422 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004423 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004424
Steven Cooremand3feccd2020-09-01 15:56:14 +02004425 if( status != PSA_ERROR_NOT_SUPPORTED ||
4426 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4427 goto exit;
4428
Steven Cooremana07b9972020-09-10 14:54:14 +02004429 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004430 * available for the given algorithm & key. */
4431 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004432
Steven Cooremana07b9972020-09-10 14:54:14 +02004433 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004434 * psa_cipher_abort. Indicate there is something to be freed through setting
4435 * alg, and indicate the operation is being done using mbedtls crypto through
4436 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004437 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004438 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004439
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004440 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004441 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004442 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004443 {
4444 status = PSA_ERROR_NOT_SUPPORTED;
4445 goto exit;
4446 }
mohammad1603503973b2018-03-12 15:59:30 +02004447
mohammad1603503973b2018-03-12 15:59:30 +02004448 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004449 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004450 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004451
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004452#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004453 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004454 {
4455 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004456 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004457 memcpy( keys, slot->key.data, 16 );
4458 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004459 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4460 keys,
4461 192, cipher_operation );
4462 }
4463 else
4464#endif
4465 {
4466 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004467 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004468 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004469 }
Moran Peker41deec42018-04-04 15:43:05 +03004470 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004471 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004472
mohammad16038481e742018-03-18 13:57:31 +02004473#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004474 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004475 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004476 case PSA_ALG_CBC_NO_PADDING:
4477 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4478 MBEDTLS_PADDING_NONE );
4479 break;
4480 case PSA_ALG_CBC_PKCS7:
4481 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4482 MBEDTLS_PADDING_PKCS7 );
4483 break;
4484 default:
4485 /* The algorithm doesn't involve padding. */
4486 ret = 0;
4487 break;
4488 }
4489 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004490 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004491#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4492
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004493 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004494 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004495 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4496 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004497 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004498 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004499 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004500#if defined(MBEDTLS_CHACHA20_C)
4501 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004502 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004503 operation->iv_size = 12;
4504#endif
mohammad1603503973b2018-03-12 15:59:30 +02004505
Steven Cooreman7df02922020-09-09 15:28:49 +02004506 status = PSA_SUCCESS;
4507
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004508exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004509 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004510 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004511 if( status == PSA_SUCCESS )
4512 {
4513 /* Update operation flags for both driver and software implementations */
4514 operation->key_set = 1;
4515 }
4516 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004517 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004518
Ronald Cron5c522922020-11-14 16:35:34 +01004519 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004520
Ronald Cron5c522922020-11-14 16:35:34 +01004521 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004522}
4523
Gilles Peskinefe119512018-07-08 21:39:34 +02004524psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004525 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004526 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004527{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004528 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004529}
4530
Gilles Peskinefe119512018-07-08 21:39:34 +02004531psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004532 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004533 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004534{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004535 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004536}
4537
Gilles Peskinefe119512018-07-08 21:39:34 +02004538psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004539 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004540 size_t iv_size,
4541 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004542{
itayzafrir534bd7c2018-08-02 13:56:32 +03004543 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004545 if( operation->iv_set || ! operation->iv_required )
4546 {
4547 return( PSA_ERROR_BAD_STATE );
4548 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004549
Steven Cooremanef8575e2020-09-11 11:44:50 +02004550 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004551 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004552 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004553 iv,
4554 iv_size,
4555 iv_length );
4556 goto exit;
4557 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004558
Moran Peker41deec42018-04-04 15:43:05 +03004559 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004560 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004561 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004562 goto exit;
4563 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004564 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004565 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004566 if( ret != 0 )
4567 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004568 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004569 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004570 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004571
mohammad16038481e742018-03-18 13:57:31 +02004572 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004573 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004574
Moran Peker395db872018-05-31 14:07:14 +03004575exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004576 if( status == PSA_SUCCESS )
4577 operation->iv_set = 1;
4578 else
Moran Peker395db872018-05-31 14:07:14 +03004579 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004580 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004581}
4582
Gilles Peskinefe119512018-07-08 21:39:34 +02004583psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004584 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004585 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004586{
itayzafrir534bd7c2018-08-02 13:56:32 +03004587 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004589 if( operation->iv_set || ! operation->iv_required )
4590 {
4591 return( PSA_ERROR_BAD_STATE );
4592 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004593
Steven Cooremanef8575e2020-09-11 11:44:50 +02004594 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004595 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004596 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004597 iv,
4598 iv_length );
4599 goto exit;
4600 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004601
Moran Pekera28258c2018-05-29 16:25:04 +03004602 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004603 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004604 status = PSA_ERROR_INVALID_ARGUMENT;
4605 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004606 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004607 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4608 status = mbedtls_to_psa_error( ret );
4609exit:
4610 if( status == PSA_SUCCESS )
4611 operation->iv_set = 1;
4612 else
mohammad1603503973b2018-03-12 15:59:30 +02004613 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004614 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004615}
4616
Steven Cooreman177deba2020-09-07 17:14:14 +02004617/* Process input for which the algorithm is set to ECB mode. This requires
4618 * manual processing, since the PSA API is defined as being able to process
4619 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4620 * underlying mbedtls_cipher_update only takes full blocks. */
4621static psa_status_t psa_cipher_update_ecb_internal(
4622 mbedtls_cipher_context_t *ctx,
4623 const uint8_t *input,
4624 size_t input_length,
4625 uint8_t *output,
4626 size_t output_size,
4627 size_t *output_length )
4628{
4629 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4630 size_t block_size = ctx->cipher_info->block_size;
4631 size_t internal_output_length = 0;
4632 *output_length = 0;
4633
4634 if( input_length == 0 )
4635 {
4636 status = PSA_SUCCESS;
4637 goto exit;
4638 }
4639
4640 if( ctx->unprocessed_len > 0 )
4641 {
4642 /* Fill up to block size, and run the block if there's a full one. */
4643 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4644
4645 if( input_length < bytes_to_copy )
4646 bytes_to_copy = input_length;
4647
4648 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4649 input, bytes_to_copy );
4650 input_length -= bytes_to_copy;
4651 input += bytes_to_copy;
4652 ctx->unprocessed_len += bytes_to_copy;
4653
4654 if( ctx->unprocessed_len == block_size )
4655 {
4656 status = mbedtls_to_psa_error(
4657 mbedtls_cipher_update( ctx,
4658 ctx->unprocessed_data,
4659 block_size,
4660 output, &internal_output_length ) );
4661
4662 if( status != PSA_SUCCESS )
4663 goto exit;
4664
4665 output += internal_output_length;
4666 output_size -= internal_output_length;
4667 *output_length += internal_output_length;
4668 ctx->unprocessed_len = 0;
4669 }
4670 }
4671
4672 while( input_length >= block_size )
4673 {
4674 /* Run all full blocks we have, one by one */
4675 status = mbedtls_to_psa_error(
4676 mbedtls_cipher_update( ctx, input,
4677 block_size,
4678 output, &internal_output_length ) );
4679
4680 if( status != PSA_SUCCESS )
4681 goto exit;
4682
4683 input_length -= block_size;
4684 input += block_size;
4685
4686 output += internal_output_length;
4687 output_size -= internal_output_length;
4688 *output_length += internal_output_length;
4689 }
4690
4691 if( input_length > 0 )
4692 {
4693 /* Save unprocessed bytes for later processing */
4694 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4695 input, input_length );
4696 ctx->unprocessed_len += input_length;
4697 }
4698
4699 status = PSA_SUCCESS;
4700
4701exit:
4702 return( status );
4703}
4704
Gilles Peskinee553c652018-06-04 16:22:46 +02004705psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4706 const uint8_t *input,
4707 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004708 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004709 size_t output_size,
4710 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004711{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004712 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004713 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004714 if( operation->alg == 0 )
4715 {
4716 return( PSA_ERROR_BAD_STATE );
4717 }
4718 if( operation->iv_required && ! operation->iv_set )
4719 {
4720 return( PSA_ERROR_BAD_STATE );
4721 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004722
Steven Cooremanef8575e2020-09-11 11:44:50 +02004723 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004724 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004725 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004726 input,
4727 input_length,
4728 output,
4729 output_size,
4730 output_length );
4731 goto exit;
4732 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004733
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004734 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004735 {
4736 /* Take the unprocessed partial block left over from previous
4737 * update calls, if any, plus the input to this call. Remove
4738 * the last partial block, if any. You get the data that will be
4739 * output in this call. */
4740 expected_output_size =
4741 ( operation->ctx.cipher.unprocessed_len + input_length )
4742 / operation->block_size * operation->block_size;
4743 }
4744 else
4745 {
4746 expected_output_size = input_length;
4747 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004748
Gilles Peskine89d789c2018-06-04 17:17:16 +02004749 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004750 {
4751 status = PSA_ERROR_BUFFER_TOO_SMALL;
4752 goto exit;
4753 }
mohammad160382759612018-03-12 18:16:40 +02004754
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004755 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4756 {
4757 /* mbedtls_cipher_update has an API inconsistency: it will only
4758 * process a single block at a time in ECB mode. Abstract away that
4759 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004760 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4761 input,
4762 input_length,
4763 output,
4764 output_size,
4765 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004766 }
4767 else
4768 {
4769 status = mbedtls_to_psa_error(
4770 mbedtls_cipher_update( &operation->ctx.cipher, input,
4771 input_length, output, output_length ) );
4772 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004773exit:
4774 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004775 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004776 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004777}
4778
Gilles Peskinee553c652018-06-04 16:22:46 +02004779psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4780 uint8_t *output,
4781 size_t output_size,
4782 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004783{
David Saadab4ecc272019-02-14 13:48:10 +02004784 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004785 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004786 if( operation->alg == 0 )
4787 {
4788 return( PSA_ERROR_BAD_STATE );
4789 }
4790 if( operation->iv_required && ! operation->iv_set )
4791 {
4792 return( PSA_ERROR_BAD_STATE );
4793 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004794
Steven Cooremanef8575e2020-09-11 11:44:50 +02004795 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004796 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004797 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004798 output,
4799 output_size,
4800 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004801 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004802 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004803
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004804 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004805 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004806 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004807 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004808 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004809 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004810 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004811 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004812 }
4813
Steven Cooremanef8575e2020-09-11 11:44:50 +02004814 status = mbedtls_to_psa_error(
4815 mbedtls_cipher_finish( &operation->ctx.cipher,
4816 temp_output_buffer,
4817 output_length ) );
4818 if( status != PSA_SUCCESS )
4819 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004820
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004821 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004822 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004823 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004824 memcpy( output, temp_output_buffer, *output_length );
4825 else
Janos Follath315b51c2018-07-09 16:04:51 +01004826 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004827
Steven Cooremanef8575e2020-09-11 11:44:50 +02004828exit:
4829 if( operation->mbedtls_in_use == 1 )
4830 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004831
Steven Cooremanef8575e2020-09-11 11:44:50 +02004832 if( status == PSA_SUCCESS )
4833 return( psa_cipher_abort( operation ) );
4834 else
4835 {
4836 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004837 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004838
Steven Cooremanef8575e2020-09-11 11:44:50 +02004839 return( status );
4840 }
mohammad1603503973b2018-03-12 15:59:30 +02004841}
4842
Gilles Peskinee553c652018-06-04 16:22:46 +02004843psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4844{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004845 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004846 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004847 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004848 * in use. It's ok to call abort on such an object, and there's
4849 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004850 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004851 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004852
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004853 /* Sanity check (shouldn't happen: operation->alg should
4854 * always have been initialized to a valid value). */
4855 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4856 return( PSA_ERROR_BAD_STATE );
4857
Steven Cooremanef8575e2020-09-11 11:44:50 +02004858 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004859 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004860 else
4861 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004862
Moran Peker41deec42018-04-04 15:43:05 +03004863 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004864 operation->key_set = 0;
4865 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004866 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004867 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004868 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004869 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004870
Moran Peker395db872018-05-31 14:07:14 +03004871 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004872}
4873
Gilles Peskinea0655c32018-04-30 17:06:50 +02004874
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004875
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004876
mohammad16035955c982018-04-26 00:53:03 +03004877/****************************************************************/
4878/* AEAD */
4879/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004880
Gilles Peskineedf9a652018-08-17 18:11:56 +02004881typedef struct
4882{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004883 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004884 const mbedtls_cipher_info_t *cipher_info;
4885 union
4886 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004887 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004888#if defined(MBEDTLS_CCM_C)
4889 mbedtls_ccm_context ccm;
4890#endif /* MBEDTLS_CCM_C */
4891#if defined(MBEDTLS_GCM_C)
4892 mbedtls_gcm_context gcm;
4893#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004894#if defined(MBEDTLS_CHACHAPOLY_C)
4895 mbedtls_chachapoly_context chachapoly;
4896#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004897 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004898 psa_algorithm_t core_alg;
4899 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004900 uint8_t tag_length;
4901} aead_operation_t;
4902
Ronald Cronf95a2b12020-10-22 15:24:49 +02004903#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4904
Gilles Peskine30a9e412019-01-14 18:36:12 +01004905static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004906{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004907 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004908 {
4909#if defined(MBEDTLS_CCM_C)
4910 case PSA_ALG_CCM:
4911 mbedtls_ccm_free( &operation->ctx.ccm );
4912 break;
4913#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004914#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004915 case PSA_ALG_GCM:
4916 mbedtls_gcm_free( &operation->ctx.gcm );
4917 break;
4918#endif /* MBEDTLS_GCM_C */
4919 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004920
Ronald Cron5c522922020-11-14 16:35:34 +01004921 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004922}
4923
4924static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004925 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004926 psa_key_usage_t usage,
4927 psa_algorithm_t alg )
4928{
4929 psa_status_t status;
4930 size_t key_bits;
4931 mbedtls_cipher_id_t cipher_id;
4932
Ronald Cron5c522922020-11-14 16:35:34 +01004933 status = psa_get_and_lock_transparent_key_slot_with_policy(
4934 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004935 if( status != PSA_SUCCESS )
4936 return( status );
4937
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004938 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004939
4940 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004941 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004942 &cipher_id );
4943 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004944 {
4945 status = PSA_ERROR_NOT_SUPPORTED;
4946 goto cleanup;
4947 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004948
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004949 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004950 {
4951#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004952 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4953 operation->core_alg = PSA_ALG_CCM;
4954 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004955 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4956 * The call to mbedtls_ccm_encrypt_and_tag or
4957 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004958 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004959 {
4960 status = PSA_ERROR_INVALID_ARGUMENT;
4961 goto cleanup;
4962 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004963 mbedtls_ccm_init( &operation->ctx.ccm );
4964 status = mbedtls_to_psa_error(
4965 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004966 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004967 (unsigned int) key_bits ) );
4968 if( status != 0 )
4969 goto cleanup;
4970 break;
4971#endif /* MBEDTLS_CCM_C */
4972
4973#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004974 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4975 operation->core_alg = PSA_ALG_GCM;
4976 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004977 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4978 * The call to mbedtls_gcm_crypt_and_tag or
4979 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004980 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004981 {
4982 status = PSA_ERROR_INVALID_ARGUMENT;
4983 goto cleanup;
4984 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004985 mbedtls_gcm_init( &operation->ctx.gcm );
4986 status = mbedtls_to_psa_error(
4987 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004988 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004989 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004990 if( status != 0 )
4991 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004992 break;
4993#endif /* MBEDTLS_GCM_C */
4994
Gilles Peskine26869f22019-05-06 15:25:00 +02004995#if defined(MBEDTLS_CHACHAPOLY_C)
4996 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4997 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4998 operation->full_tag_length = 16;
4999 /* We only support the default tag length. */
5000 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02005001 {
5002 status = PSA_ERROR_NOT_SUPPORTED;
5003 goto cleanup;
5004 }
Gilles Peskine26869f22019-05-06 15:25:00 +02005005 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
5006 status = mbedtls_to_psa_error(
5007 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005008 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02005009 if( status != 0 )
5010 goto cleanup;
5011 break;
5012#endif /* MBEDTLS_CHACHAPOLY_C */
5013
Gilles Peskineedf9a652018-08-17 18:11:56 +02005014 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02005015 status = PSA_ERROR_NOT_SUPPORTED;
5016 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005017 }
5018
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005019 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
5020 {
5021 status = PSA_ERROR_INVALID_ARGUMENT;
5022 goto cleanup;
5023 }
5024 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005025
Gilles Peskineedf9a652018-08-17 18:11:56 +02005026 return( PSA_SUCCESS );
5027
5028cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005029 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005030 return( status );
5031}
5032
Ronald Croncf56a0a2020-08-04 09:51:30 +02005033psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005034 psa_algorithm_t alg,
5035 const uint8_t *nonce,
5036 size_t nonce_length,
5037 const uint8_t *additional_data,
5038 size_t additional_data_length,
5039 const uint8_t *plaintext,
5040 size_t plaintext_length,
5041 uint8_t *ciphertext,
5042 size_t ciphertext_size,
5043 size_t *ciphertext_length )
5044{
mohammad16035955c982018-04-26 00:53:03 +03005045 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005046 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03005047 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005048
mohammad1603f08a5502018-06-03 15:05:47 +03005049 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005050
Ronald Croncf56a0a2020-08-04 09:51:30 +02005051 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005052 if( status != PSA_SUCCESS )
5053 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005054
Gilles Peskineedf9a652018-08-17 18:11:56 +02005055 /* For all currently supported modes, the tag is at the end of the
5056 * ciphertext. */
5057 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5058 {
5059 status = PSA_ERROR_BUFFER_TOO_SMALL;
5060 goto exit;
5061 }
5062 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005063
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005064#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005065 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005066 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005067 status = mbedtls_to_psa_error(
5068 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5069 MBEDTLS_GCM_ENCRYPT,
5070 plaintext_length,
5071 nonce, nonce_length,
5072 additional_data, additional_data_length,
5073 plaintext, ciphertext,
5074 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005075 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005076 else
5077#endif /* MBEDTLS_GCM_C */
5078#if defined(MBEDTLS_CCM_C)
5079 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005080 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005081 status = mbedtls_to_psa_error(
5082 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5083 plaintext_length,
5084 nonce, nonce_length,
5085 additional_data,
5086 additional_data_length,
5087 plaintext, ciphertext,
5088 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005089 }
mohammad16035c8845f2018-05-09 05:40:09 -07005090 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005091#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005092#if defined(MBEDTLS_CHACHAPOLY_C)
5093 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5094 {
5095 if( nonce_length != 12 || operation.tag_length != 16 )
5096 {
5097 status = PSA_ERROR_NOT_SUPPORTED;
5098 goto exit;
5099 }
5100 status = mbedtls_to_psa_error(
5101 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5102 plaintext_length,
5103 nonce,
5104 additional_data,
5105 additional_data_length,
5106 plaintext,
5107 ciphertext,
5108 tag ) );
5109 }
5110 else
5111#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005112 {
mohammad1603554faad2018-06-03 15:07:38 +03005113 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005114 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005115
Gilles Peskineedf9a652018-08-17 18:11:56 +02005116 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5117 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005118
Gilles Peskineedf9a652018-08-17 18:11:56 +02005119exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005120 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005121 if( status == PSA_SUCCESS )
5122 *ciphertext_length = plaintext_length + operation.tag_length;
5123 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005124}
5125
Gilles Peskineee652a32018-06-01 19:23:52 +02005126/* Locate the tag in a ciphertext buffer containing the encrypted data
5127 * followed by the tag. Return the length of the part preceding the tag in
5128 * *plaintext_length. This is the size of the plaintext in modes where
5129 * the encrypted data has the same size as the plaintext, such as
5130 * CCM and GCM. */
5131static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5132 const uint8_t *ciphertext,
5133 size_t ciphertext_length,
5134 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005135 const uint8_t **p_tag )
5136{
5137 size_t payload_length;
5138 if( tag_length > ciphertext_length )
5139 return( PSA_ERROR_INVALID_ARGUMENT );
5140 payload_length = ciphertext_length - tag_length;
5141 if( payload_length > plaintext_size )
5142 return( PSA_ERROR_BUFFER_TOO_SMALL );
5143 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005144 return( PSA_SUCCESS );
5145}
5146
Ronald Croncf56a0a2020-08-04 09:51:30 +02005147psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005148 psa_algorithm_t alg,
5149 const uint8_t *nonce,
5150 size_t nonce_length,
5151 const uint8_t *additional_data,
5152 size_t additional_data_length,
5153 const uint8_t *ciphertext,
5154 size_t ciphertext_length,
5155 uint8_t *plaintext,
5156 size_t plaintext_size,
5157 size_t *plaintext_length )
5158{
mohammad16035955c982018-04-26 00:53:03 +03005159 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005160 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005161 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005162
Gilles Peskineee652a32018-06-01 19:23:52 +02005163 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005164
Ronald Croncf56a0a2020-08-04 09:51:30 +02005165 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005166 if( status != PSA_SUCCESS )
5167 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005168
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005169 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5170 ciphertext, ciphertext_length,
5171 plaintext_size, &tag );
5172 if( status != PSA_SUCCESS )
5173 goto exit;
5174
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005175#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005176 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005177 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005178 status = mbedtls_to_psa_error(
5179 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5180 ciphertext_length - operation.tag_length,
5181 nonce, nonce_length,
5182 additional_data,
5183 additional_data_length,
5184 tag, operation.tag_length,
5185 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005186 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005187 else
5188#endif /* MBEDTLS_GCM_C */
5189#if defined(MBEDTLS_CCM_C)
5190 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005191 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005192 status = mbedtls_to_psa_error(
5193 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5194 ciphertext_length - operation.tag_length,
5195 nonce, nonce_length,
5196 additional_data,
5197 additional_data_length,
5198 ciphertext, plaintext,
5199 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005200 }
mohammad160339574652018-06-01 04:39:53 -07005201 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005202#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005203#if defined(MBEDTLS_CHACHAPOLY_C)
5204 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5205 {
5206 if( nonce_length != 12 || operation.tag_length != 16 )
5207 {
5208 status = PSA_ERROR_NOT_SUPPORTED;
5209 goto exit;
5210 }
5211 status = mbedtls_to_psa_error(
5212 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5213 ciphertext_length - operation.tag_length,
5214 nonce,
5215 additional_data,
5216 additional_data_length,
5217 tag,
5218 ciphertext,
5219 plaintext ) );
5220 }
5221 else
5222#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005223 {
mohammad1603554faad2018-06-03 15:07:38 +03005224 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005225 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005226
Gilles Peskineedf9a652018-08-17 18:11:56 +02005227 if( status != PSA_SUCCESS && plaintext_size != 0 )
5228 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005229
Gilles Peskineedf9a652018-08-17 18:11:56 +02005230exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005231 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005232 if( status == PSA_SUCCESS )
5233 *plaintext_length = ciphertext_length - operation.tag_length;
5234 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005235}
5236
Gilles Peskinea0655c32018-04-30 17:06:50 +02005237
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005238
Gilles Peskine20035e32018-02-03 22:44:14 +01005239/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005240/* Generators */
5241/****************************************************************/
5242
John Durkop07cc04a2020-11-16 22:08:34 -08005243#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5244 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5245 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5246#define AT_LEAST_ONE_BUILTIN_KDF
5247#endif
5248
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005249#define HKDF_STATE_INIT 0 /* no input yet */
5250#define HKDF_STATE_STARTED 1 /* got salt */
5251#define HKDF_STATE_KEYED 2 /* got key */
5252#define HKDF_STATE_OUTPUT 3 /* output started */
5253
Gilles Peskinecbe66502019-05-16 16:59:18 +02005254static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005256{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5258 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005259 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005261}
5262
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005264{
5265 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005266 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005267 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005268 {
5269 /* The object has (apparently) been initialized but it is not
5270 * in use. It's ok to call abort on such an object, and there's
5271 * nothing to do. */
5272 }
5273 else
John Durkopd0321952020-10-29 21:37:36 -07005274#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005275 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005276 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 mbedtls_free( operation->ctx.hkdf.info );
5278 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005279 }
John Durkop07cc04a2020-11-16 22:08:34 -08005280 else
5281#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5282#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5283 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5284 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005286 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005287 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005288 if( operation->ctx.tls12_prf.seed != NULL )
5289 {
5290 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5291 operation->ctx.tls12_prf.seed_length );
5292 mbedtls_free( operation->ctx.tls12_prf.seed );
5293 }
5294
5295 if( operation->ctx.tls12_prf.label != NULL )
5296 {
5297 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5298 operation->ctx.tls12_prf.label_length );
5299 mbedtls_free( operation->ctx.tls12_prf.label );
5300 }
5301
5302 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5303
5304 /* We leave the fields Ai and output_block to be erased safely by the
5305 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005306 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005307 else
John Durkop07cc04a2020-11-16 22:08:34 -08005308#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5309 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005310 {
5311 status = PSA_ERROR_BAD_STATE;
5312 }
Janos Follath083036a2019-06-11 10:22:26 +01005313 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005314 return( status );
5315}
5316
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005317psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005318 size_t *capacity)
5319{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005321 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005323 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005324 }
5325
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005326 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005327 return( PSA_SUCCESS );
5328}
5329
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005331 size_t capacity )
5332{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005333 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005334 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005335 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005336 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005338 return( PSA_SUCCESS );
5339}
5340
John Durkopd0321952020-10-29 21:37:36 -07005341#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005343 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005344static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005345 psa_algorithm_t hash_alg,
5346 uint8_t *output,
5347 size_t output_length )
5348{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005349 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005350 psa_status_t status;
5351
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005352 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5353 return( PSA_ERROR_BAD_STATE );
5354 hkdf->state = HKDF_STATE_OUTPUT;
5355
Gilles Peskinebef7f142018-07-12 17:22:21 +02005356 while( output_length != 0 )
5357 {
5358 /* Copy what remains of the current block */
5359 uint8_t n = hash_length - hkdf->offset_in_block;
5360 if( n > output_length )
5361 n = (uint8_t) output_length;
5362 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5363 output += n;
5364 output_length -= n;
5365 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005366 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005367 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005368 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005369 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005370 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005371 * object was corrupted or if this function is called directly
5372 * inside the library. */
5373 if( hkdf->block_number == 0xff )
5374 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005375
5376 /* We need a new block */
5377 ++hkdf->block_number;
5378 hkdf->offset_in_block = 0;
5379 status = psa_hmac_setup_internal( &hkdf->hmac,
5380 hkdf->prk, hash_length,
5381 hash_alg );
5382 if( status != PSA_SUCCESS )
5383 return( status );
5384 if( hkdf->block_number != 1 )
5385 {
5386 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5387 hkdf->output_block,
5388 hash_length );
5389 if( status != PSA_SUCCESS )
5390 return( status );
5391 }
5392 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5393 hkdf->info,
5394 hkdf->info_length );
5395 if( status != PSA_SUCCESS )
5396 return( status );
5397 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5398 &hkdf->block_number, 1 );
5399 if( status != PSA_SUCCESS )
5400 return( status );
5401 status = psa_hmac_finish_internal( &hkdf->hmac,
5402 hkdf->output_block,
5403 sizeof( hkdf->output_block ) );
5404 if( status != PSA_SUCCESS )
5405 return( status );
5406 }
5407
5408 return( PSA_SUCCESS );
5409}
John Durkop07cc04a2020-11-16 22:08:34 -08005410#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005411
John Durkop07cc04a2020-11-16 22:08:34 -08005412#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5413 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005414static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5415 psa_tls12_prf_key_derivation_t *tls12_prf,
5416 psa_algorithm_t alg )
5417{
5418 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005419 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005420 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5421 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005422
Janos Follath7742fee2019-06-17 12:58:10 +01005423 /* We can't be wanting more output after block 0xff, otherwise
5424 * the capacity check in psa_key_derivation_output_bytes() would have
5425 * prevented this call. It could happen only if the operation
5426 * object was corrupted or if this function is called directly
5427 * inside the library. */
5428 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005429 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005430
5431 /* We need a new block */
5432 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005433 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005434
5435 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5436 *
5437 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5438 *
5439 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5440 * HMAC_hash(secret, A(2) + seed) +
5441 * HMAC_hash(secret, A(3) + seed) + ...
5442 *
5443 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005444 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005445 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005446 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005447 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005448 * is currently extracted as `output_block` and where i is
5449 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005450 */
5451
Janos Follathea29bfb2019-06-19 12:21:20 +01005452 /* Save the hash context before using it, to preserve the hash state with
5453 * only the inner padding in it. We need this, because inner padding depends
5454 * on the key (secret in the RFC's terminology). */
5455 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5456 if( status != PSA_SUCCESS )
5457 goto cleanup;
5458
5459 /* Calculate A(i) where i = tls12_prf->block_number. */
5460 if( tls12_prf->block_number == 1 )
5461 {
5462 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5463 * the variable seed and in this instance means it in the context of the
5464 * P_hash function, where seed = label + seed.) */
5465 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5466 tls12_prf->label, tls12_prf->label_length );
5467 if( status != PSA_SUCCESS )
5468 goto cleanup;
5469 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5470 tls12_prf->seed, tls12_prf->seed_length );
5471 if( status != PSA_SUCCESS )
5472 goto cleanup;
5473 }
5474 else
5475 {
5476 /* A(i) = HMAC_hash(secret, A(i-1)) */
5477 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5478 tls12_prf->Ai, hash_length );
5479 if( status != PSA_SUCCESS )
5480 goto cleanup;
5481 }
5482
5483 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5484 tls12_prf->Ai, hash_length );
5485 if( status != PSA_SUCCESS )
5486 goto cleanup;
5487 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5488 if( status != PSA_SUCCESS )
5489 goto cleanup;
5490
5491 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5492 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5493 tls12_prf->Ai, hash_length );
5494 if( status != PSA_SUCCESS )
5495 goto cleanup;
5496 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5497 tls12_prf->label, tls12_prf->label_length );
5498 if( status != PSA_SUCCESS )
5499 goto cleanup;
5500 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5501 tls12_prf->seed, tls12_prf->seed_length );
5502 if( status != PSA_SUCCESS )
5503 goto cleanup;
5504 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5505 tls12_prf->output_block, hash_length );
5506 if( status != PSA_SUCCESS )
5507 goto cleanup;
5508 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5509 if( status != PSA_SUCCESS )
5510 goto cleanup;
5511
Janos Follath7742fee2019-06-17 12:58:10 +01005512
5513cleanup:
5514
Janos Follathea29bfb2019-06-19 12:21:20 +01005515 cleanup_status = psa_hash_abort( &backup );
5516 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5517 status = cleanup_status;
5518
5519 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005520}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005521
Janos Follath844eb0e2019-06-19 12:10:49 +01005522static psa_status_t psa_key_derivation_tls12_prf_read(
5523 psa_tls12_prf_key_derivation_t *tls12_prf,
5524 psa_algorithm_t alg,
5525 uint8_t *output,
5526 size_t output_length )
5527{
5528 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005529 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005530 psa_status_t status;
5531 uint8_t offset, length;
5532
5533 while( output_length != 0 )
5534 {
5535 /* Check if we have fully processed the current block. */
5536 if( tls12_prf->left_in_block == 0 )
5537 {
5538 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5539 alg );
5540 if( status != PSA_SUCCESS )
5541 return( status );
5542
5543 continue;
5544 }
5545
5546 if( tls12_prf->left_in_block > output_length )
5547 length = (uint8_t) output_length;
5548 else
5549 length = tls12_prf->left_in_block;
5550
5551 offset = hash_length - tls12_prf->left_in_block;
5552 memcpy( output, tls12_prf->output_block + offset, length );
5553 output += length;
5554 output_length -= length;
5555 tls12_prf->left_in_block -= length;
5556 }
5557
5558 return( PSA_SUCCESS );
5559}
John Durkop07cc04a2020-11-16 22:08:34 -08005560#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5561 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005562
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005563psa_status_t psa_key_derivation_output_bytes(
5564 psa_key_derivation_operation_t *operation,
5565 uint8_t *output,
5566 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005567{
5568 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005569 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005570
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005571 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005572 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005573 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005574 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005575 }
5576
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005577 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005578 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005579 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005580 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005581 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005582 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005583 goto exit;
5584 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005585 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005586 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005587 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005588 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005589 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5590 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005591 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005592 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005593 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005594 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005595 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005596
John Durkopd0321952020-10-29 21:37:36 -07005597#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005598 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005599 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005600 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005601 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005602 output, output_length );
5603 }
Janos Follathadbec812019-06-14 11:05:39 +01005604 else
John Durkop07cc04a2020-11-16 22:08:34 -08005605#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5606#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5607 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005608 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005609 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005610 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005611 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005612 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005613 output_length );
5614 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005615 else
John Durkop07cc04a2020-11-16 22:08:34 -08005616#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5617 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005618 {
5619 return( PSA_ERROR_BAD_STATE );
5620 }
5621
5622exit:
5623 if( status != PSA_SUCCESS )
5624 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005625 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005626 * This allows us to differentiate between exhausted operations and
5627 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5628 * operations. */
5629 psa_algorithm_t alg = operation->alg;
5630 psa_key_derivation_abort( operation );
5631 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005632 memset( output, '!', output_length );
5633 }
5634 return( status );
5635}
5636
Gilles Peskine08542d82018-07-19 17:05:42 +02005637#if defined(MBEDTLS_DES_C)
5638static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5639{
5640 if( data_size >= 8 )
5641 mbedtls_des_key_set_parity( data );
5642 if( data_size >= 16 )
5643 mbedtls_des_key_set_parity( data + 8 );
5644 if( data_size >= 24 )
5645 mbedtls_des_key_set_parity( data + 16 );
5646}
5647#endif /* MBEDTLS_DES_C */
5648
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005649static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005650 psa_key_slot_t *slot,
5651 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005652 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005653{
5654 uint8_t *data = NULL;
5655 size_t bytes = PSA_BITS_TO_BYTES( bits );
5656 psa_status_t status;
5657
Gilles Peskine8e338702019-07-30 20:06:31 +02005658 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005659 return( PSA_ERROR_INVALID_ARGUMENT );
5660 if( bits % 8 != 0 )
5661 return( PSA_ERROR_INVALID_ARGUMENT );
5662 data = mbedtls_calloc( 1, bytes );
5663 if( data == NULL )
5664 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5665
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005666 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005667 if( status != PSA_SUCCESS )
5668 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005669#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005670 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005671 psa_des_set_key_parity( data, bytes );
5672#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005673 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005674
5675exit:
5676 mbedtls_free( data );
5677 return( status );
5678}
5679
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005680psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005681 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005682 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005683{
5684 psa_status_t status;
5685 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005686 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005687
Ronald Cron81709fc2020-11-14 12:10:32 +01005688 *key = MBEDTLS_SVC_KEY_ID_INIT;
5689
Gilles Peskine0f84d622019-09-12 19:03:13 +02005690 /* Reject any attempt to create a zero-length key so that we don't
5691 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5692 if( psa_get_key_bits( attributes ) == 0 )
5693 return( PSA_ERROR_INVALID_ARGUMENT );
5694
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005695 if( ! operation->can_output_key )
5696 return( PSA_ERROR_NOT_PERMITTED );
5697
Ronald Cron81709fc2020-11-14 12:10:32 +01005698 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5699 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005700#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5701 if( driver != NULL )
5702 {
5703 /* Deriving a key in a secure element is not implemented yet. */
5704 status = PSA_ERROR_NOT_SUPPORTED;
5705 }
5706#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005707 if( status == PSA_SUCCESS )
5708 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005709 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005710 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005711 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005712 }
5713 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005714 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005715 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005716 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005717
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005718 return( status );
5719}
5720
Gilles Peskineeab56e42018-07-12 17:12:33 +02005721
5722
5723/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005724/* Key derivation */
5725/****************************************************************/
5726
John Durkop07cc04a2020-11-16 22:08:34 -08005727#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005728static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005729 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005730 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005731{
John Durkop07cc04a2020-11-16 22:08:34 -08005732 int is_kdf_alg_supported;
5733
Janos Follath5fe19732019-06-20 15:09:30 +01005734 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5735 * initialisers for this union leave some bytes unspecified.) */
5736 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5737
Gilles Peskine969c5d62019-01-16 15:53:06 +01005738 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005739#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005740 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5741 is_kdf_alg_supported = 1;
5742 else
5743#endif
5744#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5745 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5746 is_kdf_alg_supported = 1;
5747 else
5748#endif
5749#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5750 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5751 is_kdf_alg_supported = 1;
5752 else
5753#endif
5754 is_kdf_alg_supported = 0;
5755
5756 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005757 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005758 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005759 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005760 if( hash_size == 0 )
5761 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005762 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5763 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005764 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005765 {
5766 return( PSA_ERROR_NOT_SUPPORTED );
5767 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005769 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005770 }
John Durkop07cc04a2020-11-16 22:08:34 -08005771
5772 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005773}
John Durkop07cc04a2020-11-16 22:08:34 -08005774#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005775
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005776psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005777 psa_algorithm_t alg )
5778{
5779 psa_status_t status;
5780
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005781 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005782 return( PSA_ERROR_BAD_STATE );
5783
Gilles Peskine6843c292019-01-18 16:44:49 +01005784 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5785 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005786#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005787 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005788 {
5789 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005790 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005791 }
5792 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5793 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005794 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005795 }
John Durkop07cc04a2020-11-16 22:08:34 -08005796#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005797 else
5798 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005799
5800 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005801 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005802 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005803}
5804
John Durkopd0321952020-10-29 21:37:36 -07005805#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005806static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005807 psa_algorithm_t hash_alg,
5808 psa_key_derivation_step_t step,
5809 const uint8_t *data,
5810 size_t data_length )
5811{
5812 psa_status_t status;
5813 switch( step )
5814 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005815 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005816 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005817 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005818 status = psa_hmac_setup_internal( &hkdf->hmac,
5819 data, data_length,
5820 hash_alg );
5821 if( status != PSA_SUCCESS )
5822 return( status );
5823 hkdf->state = HKDF_STATE_STARTED;
5824 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005825 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005826 /* If no salt was provided, use an empty salt. */
5827 if( hkdf->state == HKDF_STATE_INIT )
5828 {
5829 status = psa_hmac_setup_internal( &hkdf->hmac,
5830 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005831 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005832 if( status != PSA_SUCCESS )
5833 return( status );
5834 hkdf->state = HKDF_STATE_STARTED;
5835 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005836 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005837 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005838 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5839 data, data_length );
5840 if( status != PSA_SUCCESS )
5841 return( status );
5842 status = psa_hmac_finish_internal( &hkdf->hmac,
5843 hkdf->prk,
5844 sizeof( hkdf->prk ) );
5845 if( status != PSA_SUCCESS )
5846 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005847 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005848 hkdf->block_number = 0;
5849 hkdf->state = HKDF_STATE_KEYED;
5850 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005851 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005852 if( hkdf->state == HKDF_STATE_OUTPUT )
5853 return( PSA_ERROR_BAD_STATE );
5854 if( hkdf->info_set )
5855 return( PSA_ERROR_BAD_STATE );
5856 hkdf->info_length = data_length;
5857 if( data_length != 0 )
5858 {
5859 hkdf->info = mbedtls_calloc( 1, data_length );
5860 if( hkdf->info == NULL )
5861 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5862 memcpy( hkdf->info, data, data_length );
5863 }
5864 hkdf->info_set = 1;
5865 return( PSA_SUCCESS );
5866 default:
5867 return( PSA_ERROR_INVALID_ARGUMENT );
5868 }
5869}
John Durkop07cc04a2020-11-16 22:08:34 -08005870#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005871
John Durkop07cc04a2020-11-16 22:08:34 -08005872#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5873 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005874static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5875 const uint8_t *data,
5876 size_t data_length )
5877{
5878 if( prf->state != TLS12_PRF_STATE_INIT )
5879 return( PSA_ERROR_BAD_STATE );
5880
Janos Follathd6dce9f2019-07-04 09:11:38 +01005881 if( data_length != 0 )
5882 {
5883 prf->seed = mbedtls_calloc( 1, data_length );
5884 if( prf->seed == NULL )
5885 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005886
Janos Follathd6dce9f2019-07-04 09:11:38 +01005887 memcpy( prf->seed, data, data_length );
5888 prf->seed_length = data_length;
5889 }
Janos Follathf08e2652019-06-13 09:05:41 +01005890
5891 prf->state = TLS12_PRF_STATE_SEED_SET;
5892
5893 return( PSA_SUCCESS );
5894}
5895
Janos Follath81550542019-06-13 14:26:34 +01005896static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5897 psa_algorithm_t hash_alg,
5898 const uint8_t *data,
5899 size_t data_length )
5900{
5901 psa_status_t status;
5902 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5903 return( PSA_ERROR_BAD_STATE );
5904
5905 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5906 if( status != PSA_SUCCESS )
5907 return( status );
5908
5909 prf->state = TLS12_PRF_STATE_KEY_SET;
5910
5911 return( PSA_SUCCESS );
5912}
5913
Janos Follath63028dd2019-06-13 09:15:47 +01005914static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5915 const uint8_t *data,
5916 size_t data_length )
5917{
5918 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5919 return( PSA_ERROR_BAD_STATE );
5920
Janos Follathd6dce9f2019-07-04 09:11:38 +01005921 if( data_length != 0 )
5922 {
5923 prf->label = mbedtls_calloc( 1, data_length );
5924 if( prf->label == NULL )
5925 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005926
Janos Follathd6dce9f2019-07-04 09:11:38 +01005927 memcpy( prf->label, data, data_length );
5928 prf->label_length = data_length;
5929 }
Janos Follath63028dd2019-06-13 09:15:47 +01005930
5931 prf->state = TLS12_PRF_STATE_LABEL_SET;
5932
5933 return( PSA_SUCCESS );
5934}
5935
Janos Follathb03233e2019-06-11 15:30:30 +01005936static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5937 psa_algorithm_t hash_alg,
5938 psa_key_derivation_step_t step,
5939 const uint8_t *data,
5940 size_t data_length )
5941{
Janos Follathb03233e2019-06-11 15:30:30 +01005942 switch( step )
5943 {
Janos Follathf08e2652019-06-13 09:05:41 +01005944 case PSA_KEY_DERIVATION_INPUT_SEED:
5945 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005946 case PSA_KEY_DERIVATION_INPUT_SECRET:
5947 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005948 case PSA_KEY_DERIVATION_INPUT_LABEL:
5949 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005950 default:
5951 return( PSA_ERROR_INVALID_ARGUMENT );
5952 }
5953}
John Durkop07cc04a2020-11-16 22:08:34 -08005954#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5955 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5956
5957#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5958static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5959 psa_tls12_prf_key_derivation_t *prf,
5960 psa_algorithm_t hash_alg,
5961 const uint8_t *data,
5962 size_t data_length )
5963{
5964 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005965 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005966 uint8_t *cur = pms;
5967
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005968 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005969 return( PSA_ERROR_INVALID_ARGUMENT );
5970
5971 /* Quoting RFC 4279, Section 2:
5972 *
5973 * The premaster secret is formed as follows: if the PSK is N octets
5974 * long, concatenate a uint16 with the value N, N zero octets, a second
5975 * uint16 with the value N, and the PSK itself.
5976 */
5977
5978 *cur++ = ( data_length >> 8 ) & 0xff;
5979 *cur++ = ( data_length >> 0 ) & 0xff;
5980 memset( cur, 0, data_length );
5981 cur += data_length;
5982 *cur++ = pms[0];
5983 *cur++ = pms[1];
5984 memcpy( cur, data, data_length );
5985 cur += data_length;
5986
5987 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5988
5989 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5990 return( status );
5991}
Janos Follath6660f0e2019-06-17 08:44:03 +01005992
5993static psa_status_t psa_tls12_prf_psk_to_ms_input(
5994 psa_tls12_prf_key_derivation_t *prf,
5995 psa_algorithm_t hash_alg,
5996 psa_key_derivation_step_t step,
5997 const uint8_t *data,
5998 size_t data_length )
5999{
6000 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01006001 {
Janos Follath6660f0e2019-06-17 08:44:03 +01006002 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
6003 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01006004 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006005
6006 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
6007}
John Durkop07cc04a2020-11-16 22:08:34 -08006008#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006009
Gilles Peskineb8965192019-09-24 16:21:10 +02006010/** Check whether the given key type is acceptable for the given
6011 * input step of a key derivation.
6012 *
6013 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6014 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6015 * Both secret and non-secret inputs can alternatively have the type
6016 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6017 * that the input was passed as a buffer rather than via a key object.
6018 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006019static int psa_key_derivation_check_input_type(
6020 psa_key_derivation_step_t step,
6021 psa_key_type_t key_type )
6022{
6023 switch( step )
6024 {
6025 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02006026 if( key_type == PSA_KEY_TYPE_DERIVE )
6027 return( PSA_SUCCESS );
6028 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006029 return( PSA_SUCCESS );
6030 break;
6031 case PSA_KEY_DERIVATION_INPUT_LABEL:
6032 case PSA_KEY_DERIVATION_INPUT_SALT:
6033 case PSA_KEY_DERIVATION_INPUT_INFO:
6034 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02006035 if( key_type == PSA_KEY_TYPE_RAW_DATA )
6036 return( PSA_SUCCESS );
6037 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006038 return( PSA_SUCCESS );
6039 break;
6040 }
6041 return( PSA_ERROR_INVALID_ARGUMENT );
6042}
6043
Janos Follathb80a94e2019-06-12 15:54:46 +01006044static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006045 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006046 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006047 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006048 const uint8_t *data,
6049 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006050{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006051 psa_status_t status;
6052 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6053
6054 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006055 if( status != PSA_SUCCESS )
6056 goto exit;
6057
John Durkopd0321952020-10-29 21:37:36 -07006058#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006059 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006060 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006061 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006062 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006063 step, data, data_length );
6064 }
John Durkop07cc04a2020-11-16 22:08:34 -08006065 else
6066#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6067#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6068 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006069 {
Janos Follathb03233e2019-06-11 15:30:30 +01006070 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6071 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6072 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006073 }
John Durkop07cc04a2020-11-16 22:08:34 -08006074 else
6075#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6076#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6077 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006078 {
6079 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6080 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6081 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006082 }
6083 else
John Durkop07cc04a2020-11-16 22:08:34 -08006084#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006085 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006086 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006087 return( PSA_ERROR_BAD_STATE );
6088 }
6089
Gilles Peskine224b0d62019-09-23 18:13:17 +02006090exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006091 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006092 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006093 return( status );
6094}
6095
Janos Follath51f4a0f2019-06-14 11:35:55 +01006096psa_status_t psa_key_derivation_input_bytes(
6097 psa_key_derivation_operation_t *operation,
6098 psa_key_derivation_step_t step,
6099 const uint8_t *data,
6100 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006101{
Gilles Peskineb8965192019-09-24 16:21:10 +02006102 return( psa_key_derivation_input_internal( operation, step,
6103 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006104 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006105}
6106
Janos Follath51f4a0f2019-06-14 11:35:55 +01006107psa_status_t psa_key_derivation_input_key(
6108 psa_key_derivation_operation_t *operation,
6109 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006110 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006111{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006113 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006114 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006115
Ronald Cron5c522922020-11-14 16:35:34 +01006116 status = psa_get_and_lock_transparent_key_slot_with_policy(
6117 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006118 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006119 {
6120 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006121 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006122 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006123
6124 /* Passing a key object as a SECRET input unlocks the permission
6125 * to output to a key object. */
6126 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6127 operation->can_output_key = 1;
6128
Ronald Cronf95a2b12020-10-22 15:24:49 +02006129 status = psa_key_derivation_input_internal( operation,
6130 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006131 slot->key.data,
6132 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006133
Ronald Cron5c522922020-11-14 16:35:34 +01006134 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006135
Ronald Cron5c522922020-11-14 16:35:34 +01006136 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006137}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006138
6139
6140
6141/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006142/* Key agreement */
6143/****************************************************************/
6144
John Durkop6ba40d12020-11-10 08:50:04 -08006145#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006146static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6147 size_t peer_key_length,
6148 const mbedtls_ecp_keypair *our_key,
6149 uint8_t *shared_secret,
6150 size_t shared_secret_size,
6151 size_t *shared_secret_length )
6152{
Steven Cooremand4867872020-08-05 16:31:39 +02006153 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006154 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006155 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006156 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006157 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006158 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006159
Steven Cooreman4fed4552020-08-03 14:46:03 +02006160 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6161 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006162 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006163 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006164 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006165 goto exit;
6166
Jaeden Amero97271b32019-01-10 19:38:51 +00006167 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006168 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006169 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006170 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006171 status = mbedtls_to_psa_error(
6172 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6173 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006174 goto exit;
6175
Jaeden Amero97271b32019-01-10 19:38:51 +00006176 status = mbedtls_to_psa_error(
6177 mbedtls_ecdh_calc_secret( &ecdh,
6178 shared_secret_length,
6179 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006180 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006181 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006182 if( status != PSA_SUCCESS )
6183 goto exit;
6184 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6185 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006186
6187exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006188 if( status != PSA_SUCCESS )
6189 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006190 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006191 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006192 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006193
Jaeden Amero97271b32019-01-10 19:38:51 +00006194 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006195}
John Durkop6ba40d12020-11-10 08:50:04 -08006196#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006197
Gilles Peskine01d718c2018-09-18 12:01:02 +02006198#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6199
Gilles Peskine0216fe12019-04-11 21:23:21 +02006200static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6201 psa_key_slot_t *private_key,
6202 const uint8_t *peer_key,
6203 size_t peer_key_length,
6204 uint8_t *shared_secret,
6205 size_t shared_secret_size,
6206 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006207{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006208 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006209 {
John Durkop6ba40d12020-11-10 08:50:04 -08006210#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006211 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006212 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006213 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006214 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006215 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006216 private_key->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006217 private_key->key.data,
6218 private_key->key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006219 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006220 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006221 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006222 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006223 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006224 shared_secret, shared_secret_size,
6225 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006226 mbedtls_ecp_keypair_free( ecp );
6227 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006228 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006229#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006230 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006231 (void) private_key;
6232 (void) peer_key;
6233 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006234 (void) shared_secret;
6235 (void) shared_secret_size;
6236 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006237 return( PSA_ERROR_NOT_SUPPORTED );
6238 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006239}
6240
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006241/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006242 * to potentially free embedded data structures and wipe confidential data.
6243 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006244static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006245 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006246 psa_key_slot_t *private_key,
6247 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006248 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006249{
6250 psa_status_t status;
6251 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6252 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006253 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006254
6255 /* Step 1: run the secret agreement algorithm to generate the shared
6256 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006257 status = psa_key_agreement_raw_internal( ka_alg,
6258 private_key,
6259 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006260 shared_secret,
6261 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006262 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006263 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006264 goto exit;
6265
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006266 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006267 * the shared secret. A shared secret is permitted wherever a key
6268 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006269 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006270 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006271 shared_secret,
6272 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006273exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006274 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006275 return( status );
6276}
6277
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006278psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006279 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006280 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006281 const uint8_t *peer_key,
6282 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006283{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006285 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006286 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006287
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006288 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006289 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006290 status = psa_get_and_lock_transparent_key_slot_with_policy(
6291 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006292 if( status != PSA_SUCCESS )
6293 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006294 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006295 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006296 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006297 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006298 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006299 else
6300 {
6301 /* If a private key has been added as SECRET, we allow the derived
6302 * key material to be used as a key in PSA Crypto. */
6303 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6304 operation->can_output_key = 1;
6305 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006306
Ronald Cron5c522922020-11-14 16:35:34 +01006307 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006308
Ronald Cron5c522922020-11-14 16:35:34 +01006309 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006310}
6311
Gilles Peskinebe697d82019-05-16 18:00:41 +02006312psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006313 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006314 const uint8_t *peer_key,
6315 size_t peer_key_length,
6316 uint8_t *output,
6317 size_t output_size,
6318 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006319{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006320 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006321 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006322 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006323
6324 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6325 {
6326 status = PSA_ERROR_INVALID_ARGUMENT;
6327 goto exit;
6328 }
Ronald Cron5c522922020-11-14 16:35:34 +01006329 status = psa_get_and_lock_transparent_key_slot_with_policy(
6330 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006331 if( status != PSA_SUCCESS )
6332 goto exit;
6333
6334 status = psa_key_agreement_raw_internal( alg, slot,
6335 peer_key, peer_key_length,
6336 output, output_size,
6337 output_length );
6338
6339exit:
6340 if( status != PSA_SUCCESS )
6341 {
6342 /* If an error happens and is not handled properly, the output
6343 * may be used as a key to protect sensitive data. Arrange for such
6344 * a key to be random, which is likely to result in decryption or
6345 * verification errors. This is better than filling the buffer with
6346 * some constant data such as zeros, which would result in the data
6347 * being protected with a reproducible, easily knowable key.
6348 */
6349 psa_generate_random( output, output_size );
6350 *output_length = output_size;
6351 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006352
Ronald Cron5c522922020-11-14 16:35:34 +01006353 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006354
Ronald Cron5c522922020-11-14 16:35:34 +01006355 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006356}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006357
6358
Gilles Peskine30524eb2020-11-13 17:02:26 +01006359
Gilles Peskine86a440b2018-11-12 18:39:40 +01006360/****************************************************************/
6361/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006362/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006363
Gilles Peskine30524eb2020-11-13 17:02:26 +01006364/** Initialize the PSA random generator.
6365 */
6366static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6367{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006368#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6369 memset( rng, 0, sizeof( *rng ) );
6370#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6371
Gilles Peskine30524eb2020-11-13 17:02:26 +01006372 /* Set default configuration if
6373 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6374 if( rng->entropy_init == NULL )
6375 rng->entropy_init = mbedtls_entropy_init;
6376 if( rng->entropy_free == NULL )
6377 rng->entropy_free = mbedtls_entropy_free;
6378
6379 rng->entropy_init( &rng->entropy );
6380#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6381 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6382 /* The PSA entropy injection feature depends on using NV seed as an entropy
6383 * source. Add NV seed as an entropy source for PSA entropy injection. */
6384 mbedtls_entropy_add_source( &rng->entropy,
6385 mbedtls_nv_seed_poll, NULL,
6386 MBEDTLS_ENTROPY_BLOCK_SIZE,
6387 MBEDTLS_ENTROPY_SOURCE_STRONG );
6388#endif
6389
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006390 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006391#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006392}
6393
6394/** Deinitialize the PSA random generator.
6395 */
6396static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6397{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006398#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6399 memset( rng, 0, sizeof( *rng ) );
6400#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006401 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006402 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006403#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006404}
6405
6406/** Seed the PSA random generator.
6407 */
6408static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6409{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006410#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6411 /* Do nothing: the external RNG seeds itself. */
6412 (void) rng;
6413 return( PSA_SUCCESS );
6414#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006415 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006416 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6417 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006418 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006419#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006420}
6421
Gilles Peskinee59236f2018-01-27 23:32:46 +01006422psa_status_t psa_generate_random( uint8_t *output,
6423 size_t output_size )
6424{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006425 GUARD_MODULE_INITIALIZED;
6426
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006427#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6428
6429 size_t output_length = 0;
6430 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6431 output, output_size,
6432 &output_length );
6433 if( status != PSA_SUCCESS )
6434 return( status );
6435 /* Breaking up a request into smaller chunks is currently not supported
6436 * for the extrernal RNG interface. */
6437 if( output_length != output_size )
6438 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6439 return( PSA_SUCCESS );
6440
6441#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6442
Gilles Peskine71ddab92021-01-04 21:01:07 +01006443 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006444 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006445 size_t request_size =
6446 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6447 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6448 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006449 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6450 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006451 if( ret != 0 )
6452 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006453 output_size -= request_size;
6454 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006455 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006456 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006457#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006458}
6459
Gilles Peskine8814fc42020-12-14 15:33:44 +01006460/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006461 *
6462 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6463 * `psa_generate_random(...)`. The state parameter is ignored since the
6464 * PSA API doesn't support passing an explicit state.
6465 *
6466 * In the non-external case, psa_generate_random() calls an
6467 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6468 * and semantics as mbedtls_psa_get_random(). As an optimization,
6469 * instead of doing this back-and-forth between the PSA API and the
6470 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6471 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006472 */
6473#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6474int mbedtls_psa_get_random( void *p_rng,
6475 unsigned char *output,
6476 size_t output_size )
6477{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006478 /* This function takes a pointer to the RNG state because that's what
6479 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6480 * its own state internally and doesn't let the caller access that state.
6481 * So we just ignore the state parameter, and in practice we'll pass
6482 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006483 (void) p_rng;
6484 psa_status_t status = psa_generate_random( output, output_size );
6485 if( status == PSA_SUCCESS )
6486 return( 0 );
6487 else
6488 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6489}
6490#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6491
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006492#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6493#include "mbedtls/entropy_poll.h"
6494
Gilles Peskine7228da22019-07-15 11:06:15 +02006495psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006496 size_t seed_size )
6497{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006498 if( global_data.initialized )
6499 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006500
6501 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6502 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6503 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6504 return( PSA_ERROR_INVALID_ARGUMENT );
6505
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006506 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006507}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006508#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006509
John Durkop07cc04a2020-11-16 22:08:34 -08006510#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006511static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6512 size_t domain_parameters_size,
6513 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006514{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006515 size_t i;
6516 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006517
Gilles Peskinee56e8782019-04-26 17:34:02 +02006518 if( domain_parameters_size == 0 )
6519 {
6520 *exponent = 65537;
6521 return( PSA_SUCCESS );
6522 }
6523
6524 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6525 * support values that fit in a 32-bit integer, which is larger than
6526 * int on just about every platform anyway. */
6527 if( domain_parameters_size > sizeof( acc ) )
6528 return( PSA_ERROR_NOT_SUPPORTED );
6529 for( i = 0; i < domain_parameters_size; i++ )
6530 acc = ( acc << 8 ) | domain_parameters[i];
6531 if( acc > INT_MAX )
6532 return( PSA_ERROR_NOT_SUPPORTED );
6533 *exponent = acc;
6534 return( PSA_SUCCESS );
6535}
John Durkop07cc04a2020-11-16 22:08:34 -08006536#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006537
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006538static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006539 psa_key_slot_t *slot, size_t bits,
6540 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006541{
Gilles Peskine8e338702019-07-30 20:06:31 +02006542 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006543
Gilles Peskinee56e8782019-04-26 17:34:02 +02006544 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006545 return( PSA_ERROR_INVALID_ARGUMENT );
6546
Gilles Peskinee59236f2018-01-27 23:32:46 +01006547 if( key_type_is_raw_bytes( type ) )
6548 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006549 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006550
6551 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006552 if( status != PSA_SUCCESS )
6553 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006554
6555 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006556 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6557 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006558 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006559
Ronald Cronea0f8a62020-11-25 17:52:23 +01006560 status = psa_generate_random( slot->key.data,
6561 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006562 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006563 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006564
6565 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006566#if defined(MBEDTLS_DES_C)
6567 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006568 psa_des_set_key_parity( slot->key.data,
6569 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006570#endif /* MBEDTLS_DES_C */
6571 }
6572 else
6573
John Durkop07cc04a2020-11-16 22:08:34 -08006574#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006575 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006576 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006577 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006578 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006579 int exponent;
6580 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006581 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6582 return( PSA_ERROR_NOT_SUPPORTED );
6583 /* Accept only byte-aligned keys, for the same reasons as
6584 * in psa_import_rsa_key(). */
6585 if( bits % 8 != 0 )
6586 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006587 status = psa_read_rsa_exponent( domain_parameters,
6588 domain_parameters_size,
6589 &exponent );
6590 if( status != PSA_SUCCESS )
6591 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006592 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6593 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006594 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006595 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006596 (unsigned int) bits,
6597 exponent );
6598 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006599 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006600
6601 /* Make sure to always have an export representation available */
6602 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6603
Steven Cooreman75b74362020-07-28 14:30:13 +02006604 status = psa_allocate_buffer_to_slot( slot, bytes );
6605 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006606 {
6607 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006608 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006609 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006610
6611 status = psa_export_rsa_key( type,
6612 &rsa,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006613 slot->key.data,
Steven Cooremana01795d2020-07-24 22:48:15 +02006614 bytes,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006615 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006616 mbedtls_rsa_free( &rsa );
6617 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006618 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006619 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006620 }
6621 else
John Durkop07cc04a2020-11-16 22:08:34 -08006622#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006623
John Durkop9814fa22020-11-04 12:28:15 -08006624#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006625 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006626 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006627 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006628 mbedtls_ecp_group_id grp_id =
6629 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006630 const mbedtls_ecp_curve_info *curve_info =
6631 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006632 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006634 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006635 return( PSA_ERROR_NOT_SUPPORTED );
6636 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6637 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006638 mbedtls_ecp_keypair_init( &ecp );
6639 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006640 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006641 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006642 if( ret != 0 )
6643 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006644 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006645 return( mbedtls_to_psa_error( ret ) );
6646 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006647
6648
6649 /* Make sure to always have an export representation available */
6650 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006651 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6652 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006653 {
6654 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006655 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006656 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006657
6658 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006659 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006660
6661 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006662 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006663 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006664 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006665 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006666 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006667 }
6668 else
John Durkop9814fa22020-11-04 12:28:15 -08006669#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006670 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006671 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006672 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006673
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006674 return( PSA_SUCCESS );
6675}
Darryl Green0c6575a2018-11-07 16:05:30 +00006676
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006677psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006678 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006679{
6680 psa_status_t status;
6681 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006682 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006683
Ronald Cron81709fc2020-11-14 12:10:32 +01006684 *key = MBEDTLS_SVC_KEY_ID_INIT;
6685
Gilles Peskine0f84d622019-09-12 19:03:13 +02006686 /* Reject any attempt to create a zero-length key so that we don't
6687 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6688 if( psa_get_key_bits( attributes ) == 0 )
6689 return( PSA_ERROR_INVALID_ARGUMENT );
6690
Ronald Cron81709fc2020-11-14 12:10:32 +01006691 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6692 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006693 if( status != PSA_SUCCESS )
6694 goto exit;
6695
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006696 status = psa_driver_wrapper_generate_key( attributes,
6697 slot );
6698 if( status != PSA_ERROR_NOT_SUPPORTED ||
6699 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6700 goto exit;
6701
6702 status = psa_generate_key_internal(
6703 slot, attributes->core.bits,
6704 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006705
6706exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006707 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006708 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006709 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006710 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006711
Darryl Green0c6575a2018-11-07 16:05:30 +00006712 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006713}
6714
6715
Gilles Peskinee59236f2018-01-27 23:32:46 +01006716
6717/****************************************************************/
6718/* Module setup */
6719/****************************************************************/
6720
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006721#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006722psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6723 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6724 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6725{
6726 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6727 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006728 global_data.rng.entropy_init = entropy_init;
6729 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006730 return( PSA_SUCCESS );
6731}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006732#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006733
Gilles Peskinee59236f2018-01-27 23:32:46 +01006734void mbedtls_psa_crypto_free( void )
6735{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006736 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006737 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6738 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006739 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006740 }
6741 /* Wipe all remaining data, including configuration.
6742 * In particular, this sets all state indicator to the value
6743 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006744 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006745#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006746 /* Unregister all secure element drivers, so that we restart from
6747 * a pristine state. */
6748 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006749#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006750}
6751
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006752#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6753/** Recover a transaction that was interrupted by a power failure.
6754 *
6755 * This function is called during initialization, before psa_crypto_init()
6756 * returns. If this function returns a failure status, the initialization
6757 * fails.
6758 */
6759static psa_status_t psa_crypto_recover_transaction(
6760 const psa_crypto_transaction_t *transaction )
6761{
6762 switch( transaction->unknown.type )
6763 {
6764 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6765 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006766 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006767 * is implemented.
6768 * https://github.com/ARMmbed/mbed-crypto/issues/218
6769 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006770 default:
6771 /* We found an unsupported transaction in the storage.
6772 * We don't know what state the storage is in. Give up. */
6773 return( PSA_ERROR_STORAGE_FAILURE );
6774 }
6775}
6776#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6777
Gilles Peskinee59236f2018-01-27 23:32:46 +01006778psa_status_t psa_crypto_init( void )
6779{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006780 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006781
Gilles Peskinec6b69072018-11-20 21:42:52 +01006782 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006783 if( global_data.initialized != 0 )
6784 return( PSA_SUCCESS );
6785
Gilles Peskine30524eb2020-11-13 17:02:26 +01006786 /* Initialize and seed the random generator. */
6787 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006788 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006789 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006790 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006791 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006792 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006793
Gilles Peskine66fb1262018-12-10 16:29:04 +01006794 status = psa_initialize_key_slots( );
6795 if( status != PSA_SUCCESS )
6796 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006797
Gilles Peskined9348f22019-10-01 15:22:29 +02006798#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6799 status = psa_init_all_se_drivers( );
6800 if( status != PSA_SUCCESS )
6801 goto exit;
6802#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6803
Gilles Peskine4b734222019-07-24 15:56:31 +02006804#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006805 status = psa_crypto_load_transaction( );
6806 if( status == PSA_SUCCESS )
6807 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006808 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6809 if( status != PSA_SUCCESS )
6810 goto exit;
6811 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006812 }
6813 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6814 {
6815 /* There's no transaction to complete. It's all good. */
6816 status = PSA_SUCCESS;
6817 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006818#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006819
Gilles Peskinec6b69072018-11-20 21:42:52 +01006820 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006821 global_data.initialized = 1;
6822
6823exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006824 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006825 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006826 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006827}
6828
6829#endif /* MBEDTLS_PSA_CRYPTO_C */