blob: 39144a3782fab9d4518ebc5a123002035db48957 [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. */
752 slot->data.key.data = output;
753 slot->data.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. */
986 slot->data.key.data = output;
987 slot->data.key.bytes = data_length;
988
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{
1020 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001021 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001022
1023 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
1024 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001025 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001026
1027 slot->data.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
1040 memcpy( slot->data.key.data, data, data_length );
1041 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{
Gilles Peskine73167e12019-07-12 23:44:37 +02001345#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Fredrik Strupe462aa572020-12-17 10:44:38 +01001346 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) &&
1347 psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001348 {
1349 /* No key material to clean. */
1350 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001351 else
1352#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001353 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001354 /* Data pointer will always be either a valid pointer or NULL in an
1355 * initialized slot, so we can just free it. */
1356 if( slot->data.key.data != NULL )
1357 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001358 mbedtls_free( slot->data.key.data );
1359 slot->data.key.data = NULL;
1360 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001361 }
Darryl Green40225ba2018-11-15 14:48:15 +00001362
1363 return( PSA_SUCCESS );
1364}
1365
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001366/** Completely wipe a slot in memory, including its policy.
1367 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001368psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001369{
1370 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001371
1372 /*
1373 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001374 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001375 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1376 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001377 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001378 */
Ronald Cron5c522922020-11-14 16:35:34 +01001379 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001380 {
1381#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001382 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001383#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001384 status = PSA_ERROR_CORRUPTION_DETECTED;
1385 }
1386
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001387 /* Multipart operations may still be using the key. This is safe
1388 * because all multipart operation objects are independent from
1389 * the key slot: if they need to access the key after the setup
1390 * phase, they have a copy of the key. Note that this means that
1391 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001392 /* At this point, key material and other type-specific content has
1393 * been wiped. Clear remaining metadata. We can call memset and not
1394 * zeroize because the metadata is not particularly sensitive. */
1395 memset( slot, 0, sizeof( *slot ) );
1396 return( status );
1397}
1398
Ronald Croncf56a0a2020-08-04 09:51:30 +02001399psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001400{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001401 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001402 psa_status_t status; /* status of the last operation */
1403 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001404#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1405 psa_se_drv_table_entry_t *driver;
1406#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001407
Ronald Croncf56a0a2020-08-04 09:51:30 +02001408 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001409 return( PSA_SUCCESS );
1410
Ronald Cronf2911112020-10-29 17:51:10 +01001411 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001412 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001413 * key, this will load the key description from persistent memory if not
1414 * done yet. We cannot avoid this loading as without it we don't know if
1415 * the key is operated by an SE or not and this information is needed by
1416 * the current implementation.
1417 */
Ronald Cron5c522922020-11-14 16:35:34 +01001418 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001419 if( status != PSA_SUCCESS )
1420 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001421
Ronald Cronf2911112020-10-29 17:51:10 +01001422 /*
1423 * If the key slot containing the key description is under access by the
1424 * library (apart from the present access), the key cannot be destroyed
1425 * yet. For the time being, just return in error. Eventually (to be
1426 * implemented), the key should be destroyed when all accesses have
1427 * stopped.
1428 */
Ronald Cron5c522922020-11-14 16:35:34 +01001429 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001430 {
Ronald Cron5c522922020-11-14 16:35:34 +01001431 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001432 return( PSA_ERROR_GENERIC_ERROR );
1433 }
1434
Gilles Peskine354f7672019-07-12 23:46:38 +02001435#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001436 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001437 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001438 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001439 /* For a key in a secure element, we need to do three things:
1440 * remove the key file in internal storage, destroy the
1441 * key inside the secure element, and update the driver's
1442 * persistent data. Start a transaction that will encompass these
1443 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001444 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001445 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001446 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001447 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001448 status = psa_crypto_save_transaction( );
1449 if( status != PSA_SUCCESS )
1450 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001451 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001452 /* We should still try to destroy the key in the secure
1453 * element and the key metadata in storage. This is especially
1454 * important if the error is that the storage is full.
1455 * But how to do it exactly without risking an inconsistent
1456 * state after a reset?
1457 * https://github.com/ARMmbed/mbed-crypto/issues/215
1458 */
1459 overall_status = status;
1460 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001461 }
1462
Gilles Peskine354f7672019-07-12 23:46:38 +02001463 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001464 if( overall_status == PSA_SUCCESS )
1465 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001466 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001467#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1468
Darryl Greend49a4992018-06-18 17:27:26 +01001469#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001470 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001471 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001472 status = psa_destroy_persistent_key( slot->attr.id );
1473 if( overall_status == PSA_SUCCESS )
1474 overall_status = status;
1475
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001476 /* TODO: other slots may have a copy of the same key. We should
1477 * invalidate them.
1478 * https://github.com/ARMmbed/mbed-crypto/issues/214
1479 */
Darryl Greend49a4992018-06-18 17:27:26 +01001480 }
1481#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001482
Gilles Peskinefc762652019-07-22 19:30:34 +02001483#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1484 if( driver != NULL )
1485 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001486 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001487 if( overall_status == PSA_SUCCESS )
1488 overall_status = status;
1489 status = psa_crypto_stop_transaction( );
1490 if( overall_status == PSA_SUCCESS )
1491 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001492 }
1493#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1494
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001495#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1496exit:
1497#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001498 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001499 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1500 if( overall_status == PSA_SUCCESS )
1501 overall_status = status;
1502 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503}
1504
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001505void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001506{
Gilles Peskineb699f072019-04-26 16:06:02 +02001507 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001508 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001509}
1510
Gilles Peskineb699f072019-04-26 16:06:02 +02001511psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1512 psa_key_type_t type,
1513 const uint8_t *data,
1514 size_t data_length )
1515{
1516 uint8_t *copy = NULL;
1517
1518 if( data_length != 0 )
1519 {
1520 copy = mbedtls_calloc( 1, data_length );
1521 if( copy == NULL )
1522 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1523 memcpy( copy, data, data_length );
1524 }
1525 /* After this point, this function is guaranteed to succeed, so it
1526 * can start modifying `*attributes`. */
1527
1528 if( attributes->domain_parameters != NULL )
1529 {
1530 mbedtls_free( attributes->domain_parameters );
1531 attributes->domain_parameters = NULL;
1532 attributes->domain_parameters_size = 0;
1533 }
1534
1535 attributes->domain_parameters = copy;
1536 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001537 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001538 return( PSA_SUCCESS );
1539}
1540
1541psa_status_t psa_get_key_domain_parameters(
1542 const psa_key_attributes_t *attributes,
1543 uint8_t *data, size_t data_size, size_t *data_length )
1544{
1545 if( attributes->domain_parameters_size > data_size )
1546 return( PSA_ERROR_BUFFER_TOO_SMALL );
1547 *data_length = attributes->domain_parameters_size;
1548 if( attributes->domain_parameters_size != 0 )
1549 memcpy( data, attributes->domain_parameters,
1550 attributes->domain_parameters_size );
1551 return( PSA_SUCCESS );
1552}
1553
John Durkop07cc04a2020-11-16 22:08:34 -08001554#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001555 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001556static psa_status_t psa_get_rsa_public_exponent(
1557 const mbedtls_rsa_context *rsa,
1558 psa_key_attributes_t *attributes )
1559{
1560 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001562 uint8_t *buffer = NULL;
1563 size_t buflen;
1564 mbedtls_mpi_init( &mpi );
1565
1566 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1567 if( ret != 0 )
1568 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001569 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1570 {
1571 /* It's the default value, which is reported as an empty string,
1572 * so there's nothing to do. */
1573 goto exit;
1574 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001575
1576 buflen = mbedtls_mpi_size( &mpi );
1577 buffer = mbedtls_calloc( 1, buflen );
1578 if( buffer == NULL )
1579 {
1580 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1581 goto exit;
1582 }
1583 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1584 if( ret != 0 )
1585 goto exit;
1586 attributes->domain_parameters = buffer;
1587 attributes->domain_parameters_size = buflen;
1588
1589exit:
1590 mbedtls_mpi_free( &mpi );
1591 if( ret != 0 )
1592 mbedtls_free( buffer );
1593 return( mbedtls_to_psa_error( ret ) );
1594}
John Durkop07cc04a2020-11-16 22:08:34 -08001595#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001596 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001597
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001598/** Retrieve all the publicly-accessible attributes of a key.
1599 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001600psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001601 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001602{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001603 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001604 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001605 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001606
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001607 psa_reset_key_attributes( attributes );
1608
Ronald Cron5c522922020-11-14 16:35:34 +01001609 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001610 if( status != PSA_SUCCESS )
1611 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001612
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001613 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001614 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1615 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1616
1617#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1618 if( psa_key_slot_is_external( slot ) )
1619 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1620#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001621
Gilles Peskine8e338702019-07-30 20:06:31 +02001622 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001623 {
John Durkop07cc04a2020-11-16 22:08:34 -08001624#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001625 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001626 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001627 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001628#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001629 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001630 * is not yet implemented.
1631 * https://github.com/ARMmbed/mbed-crypto/issues/216
1632 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001633 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001634 break;
1635#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001636 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001637 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001638
Steven Cooreman4fed4552020-08-03 14:46:03 +02001639 status = psa_load_rsa_representation( slot->attr.type,
1640 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001641 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001642 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001643 if( status != PSA_SUCCESS )
1644 break;
1645
Steven Cooremana2371e52020-07-28 14:30:39 +02001646 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001647 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001648 mbedtls_rsa_free( rsa );
1649 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001650 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001651 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001652#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001653 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001654 default:
1655 /* Nothing else to do. */
1656 break;
1657 }
1658
1659 if( status != PSA_SUCCESS )
1660 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001661
Ronald Cron5c522922020-11-14 16:35:34 +01001662 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001663
Ronald Cron5c522922020-11-14 16:35:34 +01001664 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001665}
1666
Gilles Peskinec8000c02019-08-02 20:15:51 +02001667#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1668psa_status_t psa_get_key_slot_number(
1669 const psa_key_attributes_t *attributes,
1670 psa_key_slot_number_t *slot_number )
1671{
1672 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1673 {
1674 *slot_number = attributes->slot_number;
1675 return( PSA_SUCCESS );
1676 }
1677 else
1678 return( PSA_ERROR_INVALID_ARGUMENT );
1679}
1680#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1681
Steven Cooremana01795d2020-07-24 22:48:15 +02001682static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1683 uint8_t *data,
1684 size_t data_size,
1685 size_t *data_length )
1686{
1687 if( slot->data.key.bytes > data_size )
1688 return( PSA_ERROR_BUFFER_TOO_SMALL );
1689 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1690 memset( data + slot->data.key.bytes, 0,
1691 data_size - slot->data.key.bytes );
1692 *data_length = slot->data.key.bytes;
1693 return( PSA_SUCCESS );
1694}
1695
Gilles Peskinef603c712019-01-19 13:40:11 +01001696static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1697 uint8_t *data,
1698 size_t data_size,
1699 size_t *data_length,
1700 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001701{
Gilles Peskine5d309672019-07-12 23:47:28 +02001702#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1703 const psa_drv_se_t *drv;
1704 psa_drv_se_context_t *drv_context;
1705#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1706
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001707 *data_length = 0;
1708
Gilles Peskine8e338702019-07-30 20:06:31 +02001709 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001710 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001711
Gilles Peskinef9168942019-09-12 19:20:29 +02001712 /* Reject a zero-length output buffer now, since this can never be a
1713 * valid key representation. This way we know that data must be a valid
1714 * pointer and we can do things like memset(data, ..., data_size). */
1715 if( data_size == 0 )
1716 return( PSA_ERROR_BUFFER_TOO_SMALL );
1717
Gilles Peskine5d309672019-07-12 23:47:28 +02001718#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001719 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001720 {
1721 psa_drv_se_export_key_t method;
1722 if( drv->key_management == NULL )
1723 return( PSA_ERROR_NOT_SUPPORTED );
1724 method = ( export_public_key ?
1725 drv->key_management->p_export_public :
1726 drv->key_management->p_export );
1727 if( method == NULL )
1728 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001729 return( method( drv_context,
1730 slot->data.se.slot_number,
1731 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001732 }
1733#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1734
Gilles Peskine8e338702019-07-30 20:06:31 +02001735 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001736 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001737 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001738 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001739 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1740 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001741 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001742 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001743 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001744 /* Exporting public -> public */
1745 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1746 }
1747 else if( !export_public_key )
1748 {
1749 /* Exporting private -> private */
1750 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1751 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001752
Steven Cooreman560c28a2020-07-24 23:20:24 +02001753 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001754 * so conversion is needed. Try the accelerators first. */
1755 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1756 data,
1757 data_size,
1758 data_length );
1759
1760 if( status != PSA_ERROR_NOT_SUPPORTED ||
1761 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1762 return( status );
1763
Steven Cooreman560c28a2020-07-24 23:20:24 +02001764 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1765 {
John Durkop0e005192020-10-31 22:06:54 -07001766#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1767 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001768 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001769 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001770 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001771 slot->data.key.data,
1772 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001773 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001774 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001775 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001776
Steven Cooreman560c28a2020-07-24 23:20:24 +02001777 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001778 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001779 data,
1780 data_size,
1781 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001782
Steven Cooremana2371e52020-07-28 14:30:39 +02001783 mbedtls_rsa_free( rsa );
1784 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001785
Steven Cooreman560c28a2020-07-24 23:20:24 +02001786 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001787#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001788 /* We don't know how to convert a private RSA key to public. */
1789 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001790#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1791 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001792 }
1793 else
Moran Pekera998bc62018-04-16 18:16:20 +03001794 {
John Durkop6ba40d12020-11-10 08:50:04 -08001795#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1796 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001797 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001798 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001799 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001800 slot->data.key.data,
1801 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001802 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001803 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001804 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001805
1806 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1807 PSA_KEY_TYPE_ECC_GET_FAMILY(
1808 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001809 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001810 data,
1811 data_size,
1812 data_length );
1813
Steven Cooremana2371e52020-07-28 14:30:39 +02001814 mbedtls_ecp_keypair_free( ecp );
1815 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001816 return( status );
1817#else
1818 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001819 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001820#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1821 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001822 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001823 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001824 else
1825 {
1826 /* This shouldn't happen in the reference implementation, but
1827 it is valid for a special-purpose implementation to omit
1828 support for exporting certain key types. */
1829 return( PSA_ERROR_NOT_SUPPORTED );
1830 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001831}
1832
Ronald Croncf56a0a2020-08-04 09:51:30 +02001833psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001834 uint8_t *data,
1835 size_t data_size,
1836 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001837{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001838 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001839 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001840 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001841
1842 /* Set the key to empty now, so that even when there are errors, we always
1843 * set data_length to a value between 0 and data_size. On error, setting
1844 * the key to empty is a good choice because an empty key representation is
1845 * unlikely to be accepted anywhere. */
1846 *data_length = 0;
1847
1848 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001849 * which don't require any flag, but
1850 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1851 */
1852 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1853 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001854 if( status != PSA_SUCCESS )
1855 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001856
1857 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001858 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001859
Ronald Cron5c522922020-11-14 16:35:34 +01001860 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001861}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001862
Ronald Croncf56a0a2020-08-04 09:51:30 +02001863psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001864 uint8_t *data,
1865 size_t data_size,
1866 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001867{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001869 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001870 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001871
1872 /* Set the key to empty now, so that even when there are errors, we always
1873 * set data_length to a value between 0 and data_size. On error, setting
1874 * the key to empty is a good choice because an empty key representation is
1875 * unlikely to be accepted anywhere. */
1876 *data_length = 0;
1877
1878 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001879 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001880 if( status != PSA_SUCCESS )
1881 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001882
1883 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001884 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001885
Ronald Cron5c522922020-11-14 16:35:34 +01001886 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001887}
1888
Gilles Peskine91e8c332019-08-02 19:19:39 +02001889#if defined(static_assert)
1890static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1891 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001892static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001893 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001894static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001895 "One or more key attribute flag is listed as both internal-only and external-only" );
1896#endif
1897
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001898/** Validate that a key policy is internally well-formed.
1899 *
1900 * This function only rejects invalid policies. It does not validate the
1901 * consistency of the policy with respect to other attributes of the key
1902 * such as the key type.
1903 */
1904static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001905{
1906 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001907 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001908 PSA_KEY_USAGE_ENCRYPT |
1909 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001910 PSA_KEY_USAGE_SIGN_HASH |
1911 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001912 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1913 return( PSA_ERROR_INVALID_ARGUMENT );
1914
Gilles Peskine4747d192019-04-17 15:05:45 +02001915 return( PSA_SUCCESS );
1916}
1917
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001918/** Validate the internal consistency of key attributes.
1919 *
1920 * This function only rejects invalid attribute values. If does not
1921 * validate the consistency of the attributes with any key data that may
1922 * be involved in the creation of the key.
1923 *
1924 * Call this function early in the key creation process.
1925 *
1926 * \param[in] attributes Key attributes for the new key.
1927 * \param[out] p_drv On any return, the driver for the key, if any.
1928 * NULL for a transparent key.
1929 *
1930 */
1931static psa_status_t psa_validate_key_attributes(
1932 const psa_key_attributes_t *attributes,
1933 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001934{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001935 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001936 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001937 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001938
Ronald Cron54b90082020-10-29 15:26:43 +01001939 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001940 if( status != PSA_SUCCESS )
1941 return( status );
1942
Ronald Crond2ed4812020-07-17 16:11:30 +02001943 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001944 if( status != PSA_SUCCESS )
1945 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001946
Ronald Cron65f38a32020-10-23 17:11:13 +02001947 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1948 {
1949 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1950 return( PSA_ERROR_INVALID_ARGUMENT );
1951 }
1952 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001953 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001954 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001955 if( status != PSA_SUCCESS )
1956 return( status );
1957 }
1958
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001959 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001960 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001961 return( status );
1962
1963 /* Refuse to create overly large keys.
1964 * Note that this doesn't trigger on import if the attributes don't
1965 * explicitly specify a size (so psa_get_key_bits returns 0), so
1966 * psa_import_key() needs its own checks. */
1967 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1968 return( PSA_ERROR_NOT_SUPPORTED );
1969
Gilles Peskine91e8c332019-08-02 19:19:39 +02001970 /* Reject invalid flags. These should not be reachable through the API. */
1971 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1972 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1973 return( PSA_ERROR_INVALID_ARGUMENT );
1974
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001975 return( PSA_SUCCESS );
1976}
1977
Gilles Peskine4747d192019-04-17 15:05:45 +02001978/** Prepare a key slot to receive key material.
1979 *
1980 * This function allocates a key slot and sets its metadata.
1981 *
1982 * If this function fails, call psa_fail_key_creation().
1983 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001984 * This function is intended to be used as follows:
1985 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001986 * it with the specified attributes, and in case of a volatile key assign it
1987 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001988 * -# Populate the slot with the key material.
1989 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1990 * In case of failure at any step, stop the sequence and call
1991 * psa_fail_key_creation().
1992 *
Ronald Cron5c522922020-11-14 16:35:34 +01001993 * On success, the key slot is locked. It is the responsibility of the caller
1994 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001995 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001996 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001997 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001998 * \param[out] p_slot On success, a pointer to the prepared slot.
1999 * \param[out] p_drv On any return, the driver for the key, if any.
2000 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002001 *
2002 * \retval #PSA_SUCCESS
2003 * The key slot is ready to receive key material.
2004 * \return If this function fails, the key slot is an invalid state.
2005 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002006 */
2007static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02002008 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02002009 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002010 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002011 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02002012{
2013 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02002014 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02002015 psa_key_slot_t *slot;
2016
Gilles Peskinedf179142019-07-15 22:02:14 +02002017 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02002018 *p_drv = NULL;
2019
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002020 status = psa_validate_key_attributes( attributes, p_drv );
2021 if( status != PSA_SUCCESS )
2022 return( status );
2023
Ronald Cronc4d1b512020-07-31 11:26:37 +02002024 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02002025 if( status != PSA_SUCCESS )
2026 return( status );
2027 slot = *p_slot;
2028
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002029 /* We're storing the declared bit-size of the key. It's up to each
2030 * creation mechanism to verify that this information is correct.
2031 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002032 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002033 * is optional (import, copy). In case of a volatile key, assign it the
2034 * volatile key identifier associated to the slot returned to contain its
2035 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002036
2037 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002038 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2039 {
2040#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2041 slot->attr.id = volatile_key_id;
2042#else
2043 slot->attr.id.key_id = volatile_key_id;
2044#endif
2045 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002046
Gilles Peskine91e8c332019-08-02 19:19:39 +02002047 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002048 * external-only flags, query `attributes`. Thanks to the check
2049 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002050 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002051 * may have set. */
2052 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002053
Gilles Peskinecbaff462019-07-12 23:46:04 +02002054#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002055 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002056 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002057 * create the key file in internal storage, create the
2058 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002059 * persistent data. This is done by starting a transaction that will
2060 * encompass these three actions.
2061 * For registering a volatile key, we just need to find an appropriate
2062 * slot number inside the SE. Since the key is designated volatile, creating
2063 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002064 /* The first thing to do is to find a slot number for the new key.
2065 * We save the slot number in persistent storage as part of the
2066 * transaction data. It will be needed to recover if the power
2067 * fails during the key creation process, to clean up on the secure
2068 * element side after restarting. Obtaining a slot number from the
2069 * secure element driver updates its persistent state, but we do not yet
2070 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002071 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002072 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002073 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002074 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002075 &slot->data.se.slot_number );
2076 if( status != PSA_SUCCESS )
2077 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002078
2079 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002080 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002081 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2082 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2083 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2084 psa_crypto_transaction.key.id = slot->attr.id;
2085 status = psa_crypto_save_transaction( );
2086 if( status != PSA_SUCCESS )
2087 {
2088 (void) psa_crypto_stop_transaction( );
2089 return( status );
2090 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002091 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002092 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002093
2094 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2095 {
2096 /* Key registration only makes sense with a secure element. */
2097 return( PSA_ERROR_INVALID_ARGUMENT );
2098 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002099#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2100
Ronald Cronc4d1b512020-07-31 11:26:37 +02002101 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002102}
Gilles Peskine4747d192019-04-17 15:05:45 +02002103
2104/** Finalize the creation of a key once its key material has been set.
2105 *
2106 * This entails writing the key to persistent storage.
2107 *
2108 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002109 * See the documentation of psa_start_key_creation() for the intended use
2110 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002111 *
Ronald Cron5c522922020-11-14 16:35:34 +01002112 * If the finalization succeeds, the function unlocks the key slot (it was
2113 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2114 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002115 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002116 * \param[in,out] slot Pointer to the slot with key material.
2117 * \param[in] driver The secure element driver for the key,
2118 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002119 * \param[out] key On success, identifier of the key. Note that the
2120 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002121 *
2122 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002123 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002124 * \return If this function fails, the key slot is an invalid state.
2125 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002126 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002127static psa_status_t psa_finish_key_creation(
2128 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002129 psa_se_drv_table_entry_t *driver,
2130 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002131{
2132 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002133 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002134 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002135
2136#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002137 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002138 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002139#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2140 if( driver != NULL )
2141 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002142 psa_se_key_data_storage_t data;
2143#if defined(static_assert)
2144 static_assert( sizeof( slot->data.se.slot_number ) ==
2145 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
2148 memcpy( &data.slot_number, &slot->data.se.slot_number,
2149 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002150 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002151 (uint8_t*) &data,
2152 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002153 }
2154 else
2155#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2156 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002157 /* Key material is saved in export representation in the slot, so
2158 * just pass the slot buffer for storage. */
2159 status = psa_save_persistent_key( &slot->attr,
2160 slot->data.key.data,
2161 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002162 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002163 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002164#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2165
Gilles Peskinecbaff462019-07-12 23:46:04 +02002166#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002167 /* Finish the transaction for a key creation. This does not
2168 * happen when registering an existing key. Detect this case
2169 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002170 * creation of a persistent key in a secure element requires a transaction,
2171 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002172 if( driver != NULL &&
2173 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002174 {
2175 status = psa_save_se_persistent_data( driver );
2176 if( status != PSA_SUCCESS )
2177 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002178 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002179 return( status );
2180 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002181 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002182 }
2183#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2184
Ronald Cron50972942020-11-14 11:28:25 +01002185 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002186 {
2187 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002188 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002189 if( status != PSA_SUCCESS )
2190 *key = MBEDTLS_SVC_KEY_ID_INIT;
2191 }
Ronald Cron50972942020-11-14 11:28:25 +01002192
Gilles Peskine4747d192019-04-17 15:05:45 +02002193 return( status );
2194}
2195
2196/** Abort the creation of a key.
2197 *
2198 * You may call this function after calling psa_start_key_creation(),
2199 * or after psa_finish_key_creation() fails. In other circumstances, this
2200 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002201 * See the documentation of psa_start_key_creation() for the intended use
2202 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002203 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002204 * \param[in,out] slot Pointer to the slot with key material.
2205 * \param[in] driver The secure element driver for the key,
2206 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002207 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002208static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002209 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002210{
Gilles Peskine011e4282019-06-26 18:34:38 +02002211 (void) driver;
2212
Gilles Peskine4747d192019-04-17 15:05:45 +02002213 if( slot == NULL )
2214 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002215
2216#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002217 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002218 * element, and the failure happened later (when saving metadata
2219 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002220 * element.
2221 * https://github.com/ARMmbed/mbed-crypto/issues/217
2222 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002223
Gilles Peskined7729582019-08-05 15:55:54 +02002224 /* Abort the ongoing transaction if any (there may not be one if
2225 * the creation process failed before starting one, or if the
2226 * key creation is a registration of a key in a secure element).
2227 * Earlier functions must already have done what it takes to undo any
2228 * partial creation. All that's left is to update the transaction data
2229 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002230 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002231#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2232
Gilles Peskine4747d192019-04-17 15:05:45 +02002233 psa_wipe_key_slot( slot );
2234}
2235
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002236/** Validate optional attributes during key creation.
2237 *
2238 * Some key attributes are optional during key creation. If they are
2239 * specified in the attributes structure, check that they are consistent
2240 * with the data in the slot.
2241 *
2242 * This function should be called near the end of key creation, after
2243 * the slot in memory is fully populated but before saving persistent data.
2244 */
2245static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002246 const psa_key_slot_t *slot,
2247 const psa_key_attributes_t *attributes )
2248{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002249 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002250 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002251 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002252 return( PSA_ERROR_INVALID_ARGUMENT );
2253 }
2254
2255 if( attributes->domain_parameters_size != 0 )
2256 {
John Durkop0e005192020-10-31 22:06:54 -07002257#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2258 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002259 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002260 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002261 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002262 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002264
Steven Cooreman7f391872020-07-30 14:57:44 +02002265 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002266 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002267 slot->data.key.data,
2268 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002269 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002270 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002271 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002272
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002273 mbedtls_mpi_init( &actual );
2274 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002275 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002276 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002277 mbedtls_rsa_free( rsa );
2278 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002279 if( ret != 0 )
2280 goto rsa_exit;
2281 ret = mbedtls_mpi_read_binary( &required,
2282 attributes->domain_parameters,
2283 attributes->domain_parameters_size );
2284 if( ret != 0 )
2285 goto rsa_exit;
2286 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2287 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2288 rsa_exit:
2289 mbedtls_mpi_free( &actual );
2290 mbedtls_mpi_free( &required );
2291 if( ret != 0)
2292 return( mbedtls_to_psa_error( ret ) );
2293 }
2294 else
John Durkop9814fa22020-11-04 12:28:15 -08002295#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2296 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002297 {
2298 return( PSA_ERROR_INVALID_ARGUMENT );
2299 }
2300 }
2301
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002302 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002303 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002304 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002305 return( PSA_ERROR_INVALID_ARGUMENT );
2306 }
2307
2308 return( PSA_SUCCESS );
2309}
2310
Gilles Peskine4747d192019-04-17 15:05:45 +02002311psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002312 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002313 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002314 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002315{
2316 psa_status_t status;
2317 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002318 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002319
Ronald Cron81709fc2020-11-14 12:10:32 +01002320 *key = MBEDTLS_SVC_KEY_ID_INIT;
2321
Gilles Peskine0f84d622019-09-12 19:03:13 +02002322 /* Reject zero-length symmetric keys (including raw data key objects).
2323 * This also rejects any key which might be encoded as an empty string,
2324 * which is never valid. */
2325 if( data_length == 0 )
2326 return( PSA_ERROR_INVALID_ARGUMENT );
2327
Gilles Peskinedf179142019-07-15 22:02:14 +02002328 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002329 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002330 if( status != PSA_SUCCESS )
2331 goto exit;
2332
Gilles Peskine5d309672019-07-12 23:47:28 +02002333#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2334 if( driver != NULL )
2335 {
2336 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002337 /* The driver should set the number of key bits, however in
2338 * case it doesn't, we initialize bits to an invalid value. */
2339 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002340 if( drv->key_management == NULL ||
2341 drv->key_management->p_import == NULL )
2342 {
2343 status = PSA_ERROR_NOT_SUPPORTED;
2344 goto exit;
2345 }
2346 status = drv->key_management->p_import(
2347 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002348 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002349 &bits );
2350 if( status != PSA_SUCCESS )
2351 goto exit;
2352 if( bits > PSA_MAX_KEY_BITS )
2353 {
2354 status = PSA_ERROR_NOT_SUPPORTED;
2355 goto exit;
2356 }
2357 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002358 }
2359 else
2360#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002361 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2362 {
2363 /* Importing a key with external lifetime through the driver wrapper
2364 * interface is not yet supported. Return as if this was an invalid
2365 * lifetime. */
2366 status = PSA_ERROR_INVALID_ARGUMENT;
2367 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002368 }
2369 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002370 {
2371 status = psa_import_key_into_slot( slot, data, data_length );
2372 if( status != PSA_SUCCESS )
2373 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002374 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002375 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002376 if( status != PSA_SUCCESS )
2377 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002378
Ronald Cron81709fc2020-11-14 12:10:32 +01002379 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002380exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002381 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002382 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002383
Gilles Peskine4747d192019-04-17 15:05:45 +02002384 return( status );
2385}
2386
Gilles Peskined7729582019-08-05 15:55:54 +02002387#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2388psa_status_t mbedtls_psa_register_se_key(
2389 const psa_key_attributes_t *attributes )
2390{
2391 psa_status_t status;
2392 psa_key_slot_t *slot = NULL;
2393 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002394 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002395
2396 /* Leaving attributes unspecified is not currently supported.
2397 * It could make sense to query the key type and size from the
2398 * secure element, but not all secure elements support this
2399 * and the driver HAL doesn't currently support it. */
2400 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2401 return( PSA_ERROR_NOT_SUPPORTED );
2402 if( psa_get_key_bits( attributes ) == 0 )
2403 return( PSA_ERROR_NOT_SUPPORTED );
2404
2405 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002406 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002407 if( status != PSA_SUCCESS )
2408 goto exit;
2409
Ronald Cron81709fc2020-11-14 12:10:32 +01002410 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002411
2412exit:
2413 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002414 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002415
Gilles Peskined7729582019-08-05 15:55:54 +02002416 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002417 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002418 return( status );
2419}
2420#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2421
Gilles Peskinef603c712019-01-19 13:40:11 +01002422static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002423 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002424{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002425 psa_status_t status = psa_copy_key_material_into_slot( target,
2426 source->data.key.data,
2427 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002428 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002429 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002430
Steven Cooreman398aee52020-10-13 14:35:45 +02002431 target->attr.type = source->attr.type;
2432 target->attr.bits = source->attr.bits;
2433
2434 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002435}
2436
Ronald Croncf56a0a2020-08-04 09:51:30 +02002437psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002438 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002439 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002440{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002441 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002442 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002443 psa_key_slot_t *source_slot = NULL;
2444 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002445 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002446 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002447
Ronald Cron81709fc2020-11-14 12:10:32 +01002448 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2449
Ronald Cron5c522922020-11-14 16:35:34 +01002450 status = psa_get_and_lock_transparent_key_slot_with_policy(
2451 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002452 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002453 goto exit;
2454
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002455 status = psa_validate_optional_attributes( source_slot,
2456 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002457 if( status != PSA_SUCCESS )
2458 goto exit;
2459
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002460 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002461 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002462 if( status != PSA_SUCCESS )
2463 goto exit;
2464
Ronald Cron81709fc2020-11-14 12:10:32 +01002465 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2466 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002467 if( status != PSA_SUCCESS )
2468 goto exit;
2469
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002470#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2471 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002472 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002473 /* Copying to a secure element is not implemented yet. */
2474 status = PSA_ERROR_NOT_SUPPORTED;
2475 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002476 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002477#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002478
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002479 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002480 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002481 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002482
Ronald Cron81709fc2020-11-14 12:10:32 +01002483 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002484exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002485 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002486 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002487
Ronald Cron5c522922020-11-14 16:35:34 +01002488 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002489
Ronald Cron5c522922020-11-14 16:35:34 +01002490 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002491}
2492
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002493
2494
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002495/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002496/* Message digests */
2497/****************************************************************/
2498
John Durkop07cc04a2020-11-16 22:08:34 -08002499#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002500 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2501 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002502 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002503static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002504{
2505 switch( alg )
2506 {
John Durkopee4e6602020-11-27 08:48:46 -08002507#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002508 case PSA_ALG_MD2:
2509 return( &mbedtls_md2_info );
2510#endif
John Durkopee4e6602020-11-27 08:48:46 -08002511#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002512 case PSA_ALG_MD4:
2513 return( &mbedtls_md4_info );
2514#endif
John Durkopee4e6602020-11-27 08:48:46 -08002515#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002516 case PSA_ALG_MD5:
2517 return( &mbedtls_md5_info );
2518#endif
John Durkopee4e6602020-11-27 08:48:46 -08002519#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002520 case PSA_ALG_RIPEMD160:
2521 return( &mbedtls_ripemd160_info );
2522#endif
John Durkopee4e6602020-11-27 08:48:46 -08002523#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002524 case PSA_ALG_SHA_1:
2525 return( &mbedtls_sha1_info );
2526#endif
John Durkopee4e6602020-11-27 08:48:46 -08002527#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002528 case PSA_ALG_SHA_224:
2529 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002530#endif
2531#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002532 case PSA_ALG_SHA_256:
2533 return( &mbedtls_sha256_info );
2534#endif
John Durkopee4e6602020-11-27 08:48:46 -08002535#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002536 case PSA_ALG_SHA_384:
2537 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002538#endif
John Durkopee4e6602020-11-27 08:48:46 -08002539#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002540 case PSA_ALG_SHA_512:
2541 return( &mbedtls_sha512_info );
2542#endif
2543 default:
2544 return( NULL );
2545 }
2546}
John Durkop07cc04a2020-11-16 22:08:34 -08002547#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002548 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2549 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2550 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002551
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002552psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2553{
2554 switch( operation->alg )
2555 {
Gilles Peskine81736312018-06-26 15:04:31 +02002556 case 0:
2557 /* The object has (apparently) been initialized but it is not
2558 * in use. It's ok to call abort on such an object, and there's
2559 * nothing to do. */
2560 break;
John Durkopee4e6602020-11-27 08:48:46 -08002561#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002562 case PSA_ALG_MD2:
2563 mbedtls_md2_free( &operation->ctx.md2 );
2564 break;
2565#endif
John Durkopee4e6602020-11-27 08:48:46 -08002566#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002567 case PSA_ALG_MD4:
2568 mbedtls_md4_free( &operation->ctx.md4 );
2569 break;
2570#endif
John Durkopee4e6602020-11-27 08:48:46 -08002571#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002572 case PSA_ALG_MD5:
2573 mbedtls_md5_free( &operation->ctx.md5 );
2574 break;
2575#endif
John Durkopee4e6602020-11-27 08:48:46 -08002576#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002577 case PSA_ALG_RIPEMD160:
2578 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2579 break;
2580#endif
John Durkopee4e6602020-11-27 08:48:46 -08002581#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002582 case PSA_ALG_SHA_1:
2583 mbedtls_sha1_free( &operation->ctx.sha1 );
2584 break;
2585#endif
John Durkop6ca23272020-12-03 06:01:32 -08002586#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002587 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002588 mbedtls_sha256_free( &operation->ctx.sha256 );
2589 break;
2590#endif
2591#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002592 case PSA_ALG_SHA_256:
2593 mbedtls_sha256_free( &operation->ctx.sha256 );
2594 break;
2595#endif
John Durkop6ca23272020-12-03 06:01:32 -08002596#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002597 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002598 mbedtls_sha512_free( &operation->ctx.sha512 );
2599 break;
2600#endif
2601#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002602 case PSA_ALG_SHA_512:
2603 mbedtls_sha512_free( &operation->ctx.sha512 );
2604 break;
2605#endif
2606 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002607 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002608 }
2609 operation->alg = 0;
2610 return( PSA_SUCCESS );
2611}
2612
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002613psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002614 psa_algorithm_t alg )
2615{
Janos Follath24eed8d2019-11-22 13:21:35 +00002616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002617
2618 /* A context must be freshly initialized before it can be set up. */
2619 if( operation->alg != 0 )
2620 {
2621 return( PSA_ERROR_BAD_STATE );
2622 }
2623
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002624 switch( alg )
2625 {
John Durkopee4e6602020-11-27 08:48:46 -08002626#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002627 case PSA_ALG_MD2:
2628 mbedtls_md2_init( &operation->ctx.md2 );
2629 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2630 break;
2631#endif
John Durkopee4e6602020-11-27 08:48:46 -08002632#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002633 case PSA_ALG_MD4:
2634 mbedtls_md4_init( &operation->ctx.md4 );
2635 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2636 break;
2637#endif
John Durkopee4e6602020-11-27 08:48:46 -08002638#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002639 case PSA_ALG_MD5:
2640 mbedtls_md5_init( &operation->ctx.md5 );
2641 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2642 break;
2643#endif
John Durkopee4e6602020-11-27 08:48:46 -08002644#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002645 case PSA_ALG_RIPEMD160:
2646 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2647 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2648 break;
2649#endif
John Durkopee4e6602020-11-27 08:48:46 -08002650#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002651 case PSA_ALG_SHA_1:
2652 mbedtls_sha1_init( &operation->ctx.sha1 );
2653 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2654 break;
2655#endif
John Durkopee4e6602020-11-27 08:48:46 -08002656#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002657 case PSA_ALG_SHA_224:
2658 mbedtls_sha256_init( &operation->ctx.sha256 );
2659 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2660 break;
John Durkopee4e6602020-11-27 08:48:46 -08002661#endif
2662#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002663 case PSA_ALG_SHA_256:
2664 mbedtls_sha256_init( &operation->ctx.sha256 );
2665 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2666 break;
2667#endif
John Durkopee4e6602020-11-27 08:48:46 -08002668#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002669 case PSA_ALG_SHA_384:
2670 mbedtls_sha512_init( &operation->ctx.sha512 );
2671 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2672 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002673#endif
John Durkopee4e6602020-11-27 08:48:46 -08002674#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002675 case PSA_ALG_SHA_512:
2676 mbedtls_sha512_init( &operation->ctx.sha512 );
2677 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2678 break;
2679#endif
2680 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002681 return( PSA_ALG_IS_HASH( alg ) ?
2682 PSA_ERROR_NOT_SUPPORTED :
2683 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002684 }
2685 if( ret == 0 )
2686 operation->alg = alg;
2687 else
2688 psa_hash_abort( operation );
2689 return( mbedtls_to_psa_error( ret ) );
2690}
2691
2692psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2693 const uint8_t *input,
2694 size_t input_length )
2695{
Janos Follath24eed8d2019-11-22 13:21:35 +00002696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002697
2698 /* Don't require hash implementations to behave correctly on a
2699 * zero-length input, which may have an invalid pointer. */
2700 if( input_length == 0 )
2701 return( PSA_SUCCESS );
2702
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002703 switch( operation->alg )
2704 {
John Durkopee4e6602020-11-27 08:48:46 -08002705#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002706 case PSA_ALG_MD2:
2707 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2708 input, input_length );
2709 break;
2710#endif
John Durkopee4e6602020-11-27 08:48:46 -08002711#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002712 case PSA_ALG_MD4:
2713 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2714 input, input_length );
2715 break;
2716#endif
John Durkopee4e6602020-11-27 08:48:46 -08002717#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002718 case PSA_ALG_MD5:
2719 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2720 input, input_length );
2721 break;
2722#endif
John Durkopee4e6602020-11-27 08:48:46 -08002723#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002724 case PSA_ALG_RIPEMD160:
2725 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2726 input, input_length );
2727 break;
2728#endif
John Durkopee4e6602020-11-27 08:48:46 -08002729#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002730 case PSA_ALG_SHA_1:
2731 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2732 input, input_length );
2733 break;
2734#endif
John Durkop6ca23272020-12-03 06:01:32 -08002735#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002736 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002737 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2738 input, input_length );
2739 break;
2740#endif
2741#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002742 case PSA_ALG_SHA_256:
2743 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2744 input, input_length );
2745 break;
2746#endif
John Durkop6ca23272020-12-03 06:01:32 -08002747#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002748 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002749 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2750 input, input_length );
2751 break;
2752#endif
2753#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002754 case PSA_ALG_SHA_512:
2755 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2756 input, input_length );
2757 break;
2758#endif
2759 default:
John Durkopee4e6602020-11-27 08:48:46 -08002760 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002761 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002762 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002763
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002764 if( ret != 0 )
2765 psa_hash_abort( operation );
2766 return( mbedtls_to_psa_error( ret ) );
2767}
2768
2769psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2770 uint8_t *hash,
2771 size_t hash_size,
2772 size_t *hash_length )
2773{
itayzafrir40835d42018-08-02 13:14:17 +03002774 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002776 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002777
2778 /* Fill the output buffer with something that isn't a valid hash
2779 * (barring an attack on the hash and deliberately-crafted input),
2780 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002781 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002782 /* If hash_size is 0 then hash may be NULL and then the
2783 * call to memset would have undefined behavior. */
2784 if( hash_size != 0 )
2785 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002786
2787 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002788 {
2789 status = PSA_ERROR_BUFFER_TOO_SMALL;
2790 goto exit;
2791 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002792
2793 switch( operation->alg )
2794 {
John Durkopee4e6602020-11-27 08:48:46 -08002795#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002796 case PSA_ALG_MD2:
2797 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2798 break;
2799#endif
John Durkopee4e6602020-11-27 08:48:46 -08002800#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002801 case PSA_ALG_MD4:
2802 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2803 break;
2804#endif
John Durkopee4e6602020-11-27 08:48:46 -08002805#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002806 case PSA_ALG_MD5:
2807 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2808 break;
2809#endif
John Durkopee4e6602020-11-27 08:48:46 -08002810#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002811 case PSA_ALG_RIPEMD160:
2812 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2813 break;
2814#endif
John Durkopee4e6602020-11-27 08:48:46 -08002815#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002816 case PSA_ALG_SHA_1:
2817 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2818 break;
2819#endif
John Durkop6ca23272020-12-03 06:01:32 -08002820#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002821 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002822 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2823 break;
2824#endif
2825#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002826 case PSA_ALG_SHA_256:
2827 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2828 break;
2829#endif
John Durkop6ca23272020-12-03 06:01:32 -08002830#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002831 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002832 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2833 break;
2834#endif
2835#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002836 case PSA_ALG_SHA_512:
2837 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2838 break;
2839#endif
2840 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002841 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002842 }
itayzafrir40835d42018-08-02 13:14:17 +03002843 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002844
itayzafrir40835d42018-08-02 13:14:17 +03002845exit:
2846 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002847 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002848 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002849 return( psa_hash_abort( operation ) );
2850 }
2851 else
2852 {
2853 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002854 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002855 }
2856}
2857
Gilles Peskine2d277862018-06-18 15:41:12 +02002858psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2859 const uint8_t *hash,
2860 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002861{
2862 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2863 size_t actual_hash_length;
2864 psa_status_t status = psa_hash_finish( operation,
2865 actual_hash, sizeof( actual_hash ),
2866 &actual_hash_length );
2867 if( status != PSA_SUCCESS )
2868 return( status );
2869 if( actual_hash_length != hash_length )
2870 return( PSA_ERROR_INVALID_SIGNATURE );
2871 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2872 return( PSA_ERROR_INVALID_SIGNATURE );
2873 return( PSA_SUCCESS );
2874}
2875
Gilles Peskine0a749c82019-11-28 19:33:58 +01002876psa_status_t psa_hash_compute( psa_algorithm_t alg,
2877 const uint8_t *input, size_t input_length,
2878 uint8_t *hash, size_t hash_size,
2879 size_t *hash_length )
2880{
2881 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2882 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2883
2884 *hash_length = hash_size;
2885 status = psa_hash_setup( &operation, alg );
2886 if( status != PSA_SUCCESS )
2887 goto exit;
2888 status = psa_hash_update( &operation, input, input_length );
2889 if( status != PSA_SUCCESS )
2890 goto exit;
2891 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2892 if( status != PSA_SUCCESS )
2893 goto exit;
2894
2895exit:
2896 if( status == PSA_SUCCESS )
2897 status = psa_hash_abort( &operation );
2898 else
2899 psa_hash_abort( &operation );
2900 return( status );
2901}
2902
2903psa_status_t psa_hash_compare( psa_algorithm_t alg,
2904 const uint8_t *input, size_t input_length,
2905 const uint8_t *hash, size_t hash_length )
2906{
2907 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2908 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2909
2910 status = psa_hash_setup( &operation, alg );
2911 if( status != PSA_SUCCESS )
2912 goto exit;
2913 status = psa_hash_update( &operation, input, input_length );
2914 if( status != PSA_SUCCESS )
2915 goto exit;
2916 status = psa_hash_verify( &operation, hash, hash_length );
2917 if( status != PSA_SUCCESS )
2918 goto exit;
2919
2920exit:
2921 if( status == PSA_SUCCESS )
2922 status = psa_hash_abort( &operation );
2923 else
2924 psa_hash_abort( &operation );
2925 return( status );
2926}
2927
Gilles Peskineeb35d782019-01-22 17:56:16 +01002928psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2929 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002930{
2931 if( target_operation->alg != 0 )
2932 return( PSA_ERROR_BAD_STATE );
2933
2934 switch( source_operation->alg )
2935 {
2936 case 0:
2937 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002938#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002939 case PSA_ALG_MD2:
2940 mbedtls_md2_clone( &target_operation->ctx.md2,
2941 &source_operation->ctx.md2 );
2942 break;
2943#endif
John Durkopee4e6602020-11-27 08:48:46 -08002944#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002945 case PSA_ALG_MD4:
2946 mbedtls_md4_clone( &target_operation->ctx.md4,
2947 &source_operation->ctx.md4 );
2948 break;
2949#endif
John Durkopee4e6602020-11-27 08:48:46 -08002950#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002951 case PSA_ALG_MD5:
2952 mbedtls_md5_clone( &target_operation->ctx.md5,
2953 &source_operation->ctx.md5 );
2954 break;
2955#endif
John Durkopee4e6602020-11-27 08:48:46 -08002956#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002957 case PSA_ALG_RIPEMD160:
2958 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2959 &source_operation->ctx.ripemd160 );
2960 break;
2961#endif
John Durkopee4e6602020-11-27 08:48:46 -08002962#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002963 case PSA_ALG_SHA_1:
2964 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2965 &source_operation->ctx.sha1 );
2966 break;
2967#endif
John Durkop6ca23272020-12-03 06:01:32 -08002968#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002969 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002970 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2971 &source_operation->ctx.sha256 );
2972 break;
2973#endif
2974#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002975 case PSA_ALG_SHA_256:
2976 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2977 &source_operation->ctx.sha256 );
2978 break;
2979#endif
John Durkop6ca23272020-12-03 06:01:32 -08002980#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002981 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002982 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2983 &source_operation->ctx.sha512 );
2984 break;
2985#endif
2986#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002987 case PSA_ALG_SHA_512:
2988 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2989 &source_operation->ctx.sha512 );
2990 break;
2991#endif
2992 default:
2993 return( PSA_ERROR_NOT_SUPPORTED );
2994 }
2995
2996 target_operation->alg = source_operation->alg;
2997 return( PSA_SUCCESS );
2998}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002999
3000
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003001/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002/* MAC */
3003/****************************************************************/
3004
Gilles Peskinedc2fc842018-03-07 16:42:59 +01003005static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01003006 psa_algorithm_t alg,
3007 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02003008 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03003009 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03003012 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003014 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003015 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003016
Gilles Peskine8c9def32018-02-08 10:02:12 +01003017 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
3018 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03003019 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003020 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01003021 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022 mode = MBEDTLS_MODE_STREAM;
3023 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003024 case PSA_ALG_CTR:
3025 mode = MBEDTLS_MODE_CTR;
3026 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003027 case PSA_ALG_CFB:
3028 mode = MBEDTLS_MODE_CFB;
3029 break;
3030 case PSA_ALG_OFB:
3031 mode = MBEDTLS_MODE_OFB;
3032 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003033 case PSA_ALG_ECB_NO_PADDING:
3034 mode = MBEDTLS_MODE_ECB;
3035 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003036 case PSA_ALG_CBC_NO_PADDING:
3037 mode = MBEDTLS_MODE_CBC;
3038 break;
3039 case PSA_ALG_CBC_PKCS7:
3040 mode = MBEDTLS_MODE_CBC;
3041 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003042 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043 mode = MBEDTLS_MODE_CCM;
3044 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003045 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003046 mode = MBEDTLS_MODE_GCM;
3047 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003048 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3049 mode = MBEDTLS_MODE_CHACHAPOLY;
3050 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003051 default:
3052 return( NULL );
3053 }
3054 }
3055 else if( alg == PSA_ALG_CMAC )
3056 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057 else
3058 return( NULL );
3059
3060 switch( key_type )
3061 {
3062 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003063 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003064 break;
3065 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003066 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3067 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003069 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003070 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003071 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003072 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3073 * but two-key Triple-DES is functionally three-key Triple-DES
3074 * with K1=K3, so that's how we present it to mbedtls. */
3075 if( key_bits == 128 )
3076 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003077 break;
3078 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003079 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080 break;
3081 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003082 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003083 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003084 case PSA_KEY_TYPE_CHACHA20:
3085 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3086 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003087 default:
3088 return( NULL );
3089 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003090 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003091 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003092
Jaeden Amero23bbb752018-06-26 14:16:54 +01003093 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3094 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003095}
3096
John Durkop6ba40d12020-11-10 08:50:04 -08003097#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003098static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003099{
Gilles Peskine2d277862018-06-18 15:41:12 +02003100 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003101 {
3102 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003103 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003104 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003105 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003106 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003107 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003108 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003109 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003110 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003111 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003112 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003113 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003114 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003115 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003116 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003117 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003118 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003119 return( 128 );
3120 default:
3121 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003122 }
3123}
John Durkop07cc04a2020-11-16 22:08:34 -08003124#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003125
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003126/* Initialize the MAC operation structure. Once this function has been
3127 * called, psa_mac_abort can run and will do the right thing. */
3128static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3129 psa_algorithm_t alg )
3130{
3131 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3132
3133 operation->alg = alg;
3134 operation->key_set = 0;
3135 operation->iv_set = 0;
3136 operation->iv_required = 0;
3137 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003138 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003139
3140#if defined(MBEDTLS_CMAC_C)
3141 if( alg == PSA_ALG_CMAC )
3142 {
3143 operation->iv_required = 0;
3144 mbedtls_cipher_init( &operation->ctx.cmac );
3145 status = PSA_SUCCESS;
3146 }
3147 else
3148#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003149#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003150 if( PSA_ALG_IS_HMAC( operation->alg ) )
3151 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003152 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3153 operation->ctx.hmac.hash_ctx.alg = 0;
3154 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003155 }
3156 else
John Durkopd0321952020-10-29 21:37:36 -07003157#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003158 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003159 if( ! PSA_ALG_IS_MAC( alg ) )
3160 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003161 }
3162
3163 if( status != PSA_SUCCESS )
3164 memset( operation, 0, sizeof( *operation ) );
3165 return( status );
3166}
3167
John Durkop6ba40d12020-11-10 08:50:04 -08003168#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003169static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3170{
Gilles Peskine3f108122018-12-07 18:14:53 +01003171 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003172 return( psa_hash_abort( &hmac->hash_ctx ) );
3173}
John Durkop6ba40d12020-11-10 08:50:04 -08003174#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003175
Gilles Peskine8c9def32018-02-08 10:02:12 +01003176psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3177{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003178 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003179 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003180 /* The object has (apparently) been initialized but it is not
3181 * in use. It's ok to call abort on such an object, and there's
3182 * nothing to do. */
3183 return( PSA_SUCCESS );
3184 }
3185 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003186#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003187 if( operation->alg == PSA_ALG_CMAC )
3188 {
3189 mbedtls_cipher_free( &operation->ctx.cmac );
3190 }
3191 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003192#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003193#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003194 if( PSA_ALG_IS_HMAC( operation->alg ) )
3195 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003196 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003197 }
3198 else
John Durkopd0321952020-10-29 21:37:36 -07003199#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003200 {
3201 /* Sanity check (shouldn't happen: operation->alg should
3202 * always have been initialized to a valid value). */
3203 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003204 }
Moran Peker41deec42018-04-04 15:43:05 +03003205
Gilles Peskine8c9def32018-02-08 10:02:12 +01003206 operation->alg = 0;
3207 operation->key_set = 0;
3208 operation->iv_set = 0;
3209 operation->iv_required = 0;
3210 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003211 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003212
Gilles Peskine8c9def32018-02-08 10:02:12 +01003213 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003214
3215bad_state:
3216 /* If abort is called on an uninitialized object, we can't trust
3217 * anything. Wipe the object in case it contains confidential data.
3218 * This may result in a memory leak if a pointer gets overwritten,
3219 * but it's too late to do anything about this. */
3220 memset( operation, 0, sizeof( *operation ) );
3221 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003222}
3223
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003224#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003225static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003226 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003227 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003228 const mbedtls_cipher_info_t *cipher_info )
3229{
Janos Follath24eed8d2019-11-22 13:21:35 +00003230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003231
3232 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003233
3234 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3235 if( ret != 0 )
3236 return( ret );
3237
3238 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003239 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003240 key_bits );
3241 return( ret );
3242}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003243#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003244
John Durkop6ba40d12020-11-10 08:50:04 -08003245#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003246static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3247 const uint8_t *key,
3248 size_t key_length,
3249 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003250{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003251 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003252 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003253 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003254 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003255 psa_status_t status;
3256
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003257 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3258 * overflow below. This should never trigger if the hash algorithm
3259 * is implemented correctly. */
3260 /* The size checks against the ipad and opad buffers cannot be written
3261 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3262 * because that triggers -Wlogical-op on GCC 7.3. */
3263 if( block_size > sizeof( ipad ) )
3264 return( PSA_ERROR_NOT_SUPPORTED );
3265 if( block_size > sizeof( hmac->opad ) )
3266 return( PSA_ERROR_NOT_SUPPORTED );
3267 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003268 return( PSA_ERROR_NOT_SUPPORTED );
3269
Gilles Peskined223b522018-06-11 18:12:58 +02003270 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003271 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003272 status = psa_hash_compute( hash_alg, key, key_length,
3273 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003274 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003275 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003276 }
Gilles Peskine96889972018-07-12 17:07:03 +02003277 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3278 * but it is permitted. It is common when HMAC is used in HKDF, for
3279 * example. Don't call `memcpy` in the 0-length because `key` could be
3280 * an invalid pointer which would make the behavior undefined. */
3281 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003282 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003283
Gilles Peskined223b522018-06-11 18:12:58 +02003284 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3285 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003286 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003287 ipad[i] ^= 0x36;
3288 memset( ipad + key_length, 0x36, block_size - key_length );
3289
3290 /* Copy the key material from ipad to opad, flipping the requisite bits,
3291 * and filling the rest of opad with the requisite constant. */
3292 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003293 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3294 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003295
Gilles Peskine01126fa2018-07-12 17:04:55 +02003296 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003297 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003298 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003299
Gilles Peskine01126fa2018-07-12 17:04:55 +02003300 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003301
3302cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003303 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003304
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003305 return( status );
3306}
John Durkop6ba40d12020-11-10 08:50:04 -08003307#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003308
Gilles Peskine89167cb2018-07-08 20:12:23 +02003309static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003310 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003311 psa_algorithm_t alg,
3312 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003313{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003314 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003315 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003316 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003317 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003318 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003319 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003320 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003321 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003322
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003323 /* A context must be freshly initialized before it can be set up. */
3324 if( operation->alg != 0 )
3325 {
3326 return( PSA_ERROR_BAD_STATE );
3327 }
3328
Gilles Peskined911eb72018-08-14 15:18:45 +02003329 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003330 if( status != PSA_SUCCESS )
3331 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003332 if( is_sign )
3333 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003334
Ronald Cron5c522922020-11-14 16:35:34 +01003335 status = psa_get_and_lock_transparent_key_slot_with_policy(
3336 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003337 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003338 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003339 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003340
Gilles Peskine8c9def32018-02-08 10:02:12 +01003341#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003342 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003343 {
3344 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003345 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003346 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003348 if( cipher_info == NULL )
3349 {
3350 status = PSA_ERROR_NOT_SUPPORTED;
3351 goto exit;
3352 }
3353 operation->mac_size = cipher_info->block_size;
3354 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3355 status = mbedtls_to_psa_error( ret );
3356 }
3357 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003358#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003359#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003360 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003361 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003362 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003363 if( hash_alg == 0 )
3364 {
3365 status = PSA_ERROR_NOT_SUPPORTED;
3366 goto exit;
3367 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003368
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003369 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003370 /* Sanity check. This shouldn't fail on a valid configuration. */
3371 if( operation->mac_size == 0 ||
3372 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3373 {
3374 status = PSA_ERROR_NOT_SUPPORTED;
3375 goto exit;
3376 }
3377
Gilles Peskine8e338702019-07-30 20:06:31 +02003378 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003379 {
3380 status = PSA_ERROR_INVALID_ARGUMENT;
3381 goto exit;
3382 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003383
Gilles Peskine01126fa2018-07-12 17:04:55 +02003384 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003385 slot->data.key.data,
3386 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003387 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003388 }
3389 else
John Durkopd0321952020-10-29 21:37:36 -07003390#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003391 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003392 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003393 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003394 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003395
Gilles Peskined911eb72018-08-14 15:18:45 +02003396 if( truncated == 0 )
3397 {
3398 /* The "normal" case: untruncated algorithm. Nothing to do. */
3399 }
3400 else if( truncated < 4 )
3401 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003402 /* A very short MAC is too short for security since it can be
3403 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3404 * so we make this our minimum, even though 32 bits is still
3405 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003406 status = PSA_ERROR_NOT_SUPPORTED;
3407 }
3408 else if( truncated > operation->mac_size )
3409 {
3410 /* It's impossible to "truncate" to a larger length. */
3411 status = PSA_ERROR_INVALID_ARGUMENT;
3412 }
3413 else
3414 operation->mac_size = truncated;
3415
Gilles Peskinefbfac682018-07-08 20:51:54 +02003416exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003417 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003418 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003419 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003420 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003421 else
3422 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003423 operation->key_set = 1;
3424 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003425
Ronald Cron5c522922020-11-14 16:35:34 +01003426 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003427
Ronald Cron5c522922020-11-14 16:35:34 +01003428 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003429}
3430
Gilles Peskine89167cb2018-07-08 20:12:23 +02003431psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003432 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003433 psa_algorithm_t alg )
3434{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003435 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003436}
3437
3438psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003439 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003440 psa_algorithm_t alg )
3441{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003442 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003443}
3444
Gilles Peskine8c9def32018-02-08 10:02:12 +01003445psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3446 const uint8_t *input,
3447 size_t input_length )
3448{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003449 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003450 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003451 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003452 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003453 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003454 operation->has_input = 1;
3455
Gilles Peskine8c9def32018-02-08 10:02:12 +01003456#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003457 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003458 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003459 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3460 input, input_length );
3461 status = mbedtls_to_psa_error( ret );
3462 }
3463 else
3464#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003465#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003466 if( PSA_ALG_IS_HMAC( operation->alg ) )
3467 {
3468 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3469 input_length );
3470 }
3471 else
John Durkopd0321952020-10-29 21:37:36 -07003472#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003473 {
3474 /* This shouldn't happen if `operation` was initialized by
3475 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003476 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003477 }
3478
Gilles Peskinefbfac682018-07-08 20:51:54 +02003479 if( status != PSA_SUCCESS )
3480 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003481 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003482}
3483
John Durkop6ba40d12020-11-10 08:50:04 -08003484#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003485static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3486 uint8_t *mac,
3487 size_t mac_size )
3488{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003489 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003490 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3491 size_t hash_size = 0;
3492 size_t block_size = psa_get_hash_block_size( hash_alg );
3493 psa_status_t status;
3494
Gilles Peskine01126fa2018-07-12 17:04:55 +02003495 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3496 if( status != PSA_SUCCESS )
3497 return( status );
3498 /* From here on, tmp needs to be wiped. */
3499
3500 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3501 if( status != PSA_SUCCESS )
3502 goto exit;
3503
3504 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3505 if( status != PSA_SUCCESS )
3506 goto exit;
3507
3508 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3509 if( status != PSA_SUCCESS )
3510 goto exit;
3511
Gilles Peskined911eb72018-08-14 15:18:45 +02003512 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3513 if( status != PSA_SUCCESS )
3514 goto exit;
3515
3516 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003517
3518exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003519 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003520 return( status );
3521}
John Durkop6ba40d12020-11-10 08:50:04 -08003522#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003523
mohammad16036df908f2018-04-02 08:34:15 -07003524static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003525 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003526 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003527{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003528 if( ! operation->key_set )
3529 return( PSA_ERROR_BAD_STATE );
3530 if( operation->iv_required && ! operation->iv_set )
3531 return( PSA_ERROR_BAD_STATE );
3532
Gilles Peskine8c9def32018-02-08 10:02:12 +01003533 if( mac_size < operation->mac_size )
3534 return( PSA_ERROR_BUFFER_TOO_SMALL );
3535
Gilles Peskine8c9def32018-02-08 10:02:12 +01003536#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003537 if( operation->alg == PSA_ALG_CMAC )
3538 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003539 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003540 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3541 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003542 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003543 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003544 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003545 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003546 else
3547#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003548#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003549 if( PSA_ALG_IS_HMAC( operation->alg ) )
3550 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003551 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003552 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003553 }
3554 else
John Durkopd0321952020-10-29 21:37:36 -07003555#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003556 {
3557 /* This shouldn't happen if `operation` was initialized by
3558 * a setup function. */
3559 return( PSA_ERROR_BAD_STATE );
3560 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003561}
3562
Gilles Peskineacd4be32018-07-08 19:56:25 +02003563psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3564 uint8_t *mac,
3565 size_t mac_size,
3566 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003567{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003568 psa_status_t status;
3569
Jaeden Amero252ef282019-02-15 14:05:35 +00003570 if( operation->alg == 0 )
3571 {
3572 return( PSA_ERROR_BAD_STATE );
3573 }
3574
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003575 /* Fill the output buffer with something that isn't a valid mac
3576 * (barring an attack on the mac and deliberately-crafted input),
3577 * in case the caller doesn't check the return status properly. */
3578 *mac_length = mac_size;
3579 /* If mac_size is 0 then mac may be NULL and then the
3580 * call to memset would have undefined behavior. */
3581 if( mac_size != 0 )
3582 memset( mac, '!', mac_size );
3583
Gilles Peskine89167cb2018-07-08 20:12:23 +02003584 if( ! operation->is_sign )
3585 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003586 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003587 }
mohammad16036df908f2018-04-02 08:34:15 -07003588
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003589 status = psa_mac_finish_internal( operation, mac, mac_size );
3590
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003591 if( status == PSA_SUCCESS )
3592 {
3593 status = psa_mac_abort( operation );
3594 if( status == PSA_SUCCESS )
3595 *mac_length = operation->mac_size;
3596 else
3597 memset( mac, '!', mac_size );
3598 }
3599 else
3600 psa_mac_abort( operation );
3601 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003602}
3603
Gilles Peskineacd4be32018-07-08 19:56:25 +02003604psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3605 const uint8_t *mac,
3606 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003607{
Gilles Peskine828ed142018-06-18 23:25:51 +02003608 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003609 psa_status_t status;
3610
Jaeden Amero252ef282019-02-15 14:05:35 +00003611 if( operation->alg == 0 )
3612 {
3613 return( PSA_ERROR_BAD_STATE );
3614 }
3615
Gilles Peskine89167cb2018-07-08 20:12:23 +02003616 if( operation->is_sign )
3617 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003618 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003619 }
3620 if( operation->mac_size != mac_length )
3621 {
3622 status = PSA_ERROR_INVALID_SIGNATURE;
3623 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003624 }
mohammad16036df908f2018-04-02 08:34:15 -07003625
3626 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003627 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003628 if( status != PSA_SUCCESS )
3629 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003630
3631 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3632 status = PSA_ERROR_INVALID_SIGNATURE;
3633
3634cleanup:
3635 if( status == PSA_SUCCESS )
3636 status = psa_mac_abort( operation );
3637 else
3638 psa_mac_abort( operation );
3639
Gilles Peskine3f108122018-12-07 18:14:53 +01003640 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003641
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003642 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003643}
3644
3645
Gilles Peskine20035e32018-02-03 22:44:14 +01003646
Gilles Peskine20035e32018-02-03 22:44:14 +01003647/****************************************************************/
3648/* Asymmetric cryptography */
3649/****************************************************************/
3650
John Durkop6ba40d12020-11-10 08:50:04 -08003651#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3652 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003653/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003654 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003655static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3656 size_t hash_length,
3657 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003658{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003659 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003660 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003661 *md_alg = mbedtls_md_get_type( md_info );
3662
3663 /* The Mbed TLS RSA module uses an unsigned int for hash length
3664 * parameters. Validate that it fits so that we don't risk an
3665 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003666#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003667 if( hash_length > UINT_MAX )
3668 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003669#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003670
John Durkop0e005192020-10-31 22:06:54 -07003671#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003672 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3673 * must be correct. */
3674 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3675 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003676 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003677 if( md_info == NULL )
3678 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003679 if( mbedtls_md_get_size( md_info ) != hash_length )
3680 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003681 }
John Durkop0e005192020-10-31 22:06:54 -07003682#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003683
John Durkop0e005192020-10-31 22:06:54 -07003684#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003685 /* PSS requires a hash internally. */
3686 if( PSA_ALG_IS_RSA_PSS( alg ) )
3687 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003688 if( md_info == NULL )
3689 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003690 }
John Durkop0e005192020-10-31 22:06:54 -07003691#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003692
Gilles Peskine61b91d42018-06-08 16:09:36 +02003693 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003694}
3695
Gilles Peskine2b450e32018-06-27 15:42:46 +02003696static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3697 psa_algorithm_t alg,
3698 const uint8_t *hash,
3699 size_t hash_length,
3700 uint8_t *signature,
3701 size_t signature_size,
3702 size_t *signature_length )
3703{
3704 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003706 mbedtls_md_type_t md_alg;
3707
3708 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3709 if( status != PSA_SUCCESS )
3710 return( status );
3711
Gilles Peskine630a18a2018-06-29 17:49:35 +02003712 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003713 return( PSA_ERROR_BUFFER_TOO_SMALL );
3714
John Durkop0e005192020-10-31 22:06:54 -07003715#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003716 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3717 {
3718 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3719 MBEDTLS_MD_NONE );
3720 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003721 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003722 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003723 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003724 md_alg,
3725 (unsigned int) hash_length,
3726 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003727 signature );
3728 }
3729 else
John Durkop0e005192020-10-31 22:06:54 -07003730#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3731#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003732 if( PSA_ALG_IS_RSA_PSS( alg ) )
3733 {
3734 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3735 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003736 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003737 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003738 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003739 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003740 (unsigned int) hash_length,
3741 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003742 signature );
3743 }
3744 else
John Durkop0e005192020-10-31 22:06:54 -07003745#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003746 {
3747 return( PSA_ERROR_INVALID_ARGUMENT );
3748 }
3749
3750 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003751 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003752 return( mbedtls_to_psa_error( ret ) );
3753}
3754
3755static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3756 psa_algorithm_t alg,
3757 const uint8_t *hash,
3758 size_t hash_length,
3759 const uint8_t *signature,
3760 size_t signature_length )
3761{
3762 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003764 mbedtls_md_type_t md_alg;
3765
3766 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3767 if( status != PSA_SUCCESS )
3768 return( status );
3769
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003770 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3771 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003772
John Durkop0e005192020-10-31 22:06:54 -07003773#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003774 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3775 {
3776 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3777 MBEDTLS_MD_NONE );
3778 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003779 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003780 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003781 MBEDTLS_RSA_PUBLIC,
3782 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003783 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003784 hash,
3785 signature );
3786 }
3787 else
John Durkop0e005192020-10-31 22:06:54 -07003788#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3789#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003790 if( PSA_ALG_IS_RSA_PSS( alg ) )
3791 {
3792 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3793 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003794 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003795 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003796 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003797 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003798 (unsigned int) hash_length,
3799 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003800 signature );
3801 }
3802 else
John Durkop0e005192020-10-31 22:06:54 -07003803#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003804 {
3805 return( PSA_ERROR_INVALID_ARGUMENT );
3806 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003807
3808 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3809 * the rest of the signature is invalid". This has little use in
3810 * practice and PSA doesn't report this distinction. */
3811 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3812 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003813 return( mbedtls_to_psa_error( ret ) );
3814}
John Durkop6ba40d12020-11-10 08:50:04 -08003815#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3816 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003817
John Durkop6ba40d12020-11-10 08:50:04 -08003818#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3819 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003820/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3821 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3822 * (even though these functions don't modify it). */
3823static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3824 psa_algorithm_t alg,
3825 const uint8_t *hash,
3826 size_t hash_length,
3827 uint8_t *signature,
3828 size_t signature_size,
3829 size_t *signature_length )
3830{
Janos Follath24eed8d2019-11-22 13:21:35 +00003831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003832 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003833 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003834 mbedtls_mpi_init( &r );
3835 mbedtls_mpi_init( &s );
3836
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003837 if( signature_size < 2 * curve_bytes )
3838 {
3839 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3840 goto cleanup;
3841 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003842
John Durkop0ea39e02020-10-13 19:58:20 -07003843#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003844 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3845 {
3846 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3847 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3848 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003849 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3850 &ecp->d, hash,
3851 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003852 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003853 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003854 }
3855 else
John Durkop0ea39e02020-10-13 19:58:20 -07003856#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003857 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003858 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003859 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3860 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003861 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003862 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003863 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003864
3865 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3866 signature,
3867 curve_bytes ) );
3868 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3869 signature + curve_bytes,
3870 curve_bytes ) );
3871
3872cleanup:
3873 mbedtls_mpi_free( &r );
3874 mbedtls_mpi_free( &s );
3875 if( ret == 0 )
3876 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003877 return( mbedtls_to_psa_error( ret ) );
3878}
3879
3880static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3881 const uint8_t *hash,
3882 size_t hash_length,
3883 const uint8_t *signature,
3884 size_t signature_length )
3885{
Janos Follath24eed8d2019-11-22 13:21:35 +00003886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003887 mbedtls_mpi r, s;
3888 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3889 mbedtls_mpi_init( &r );
3890 mbedtls_mpi_init( &s );
3891
3892 if( signature_length != 2 * curve_bytes )
3893 return( PSA_ERROR_INVALID_SIGNATURE );
3894
3895 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3896 signature,
3897 curve_bytes ) );
3898 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3899 signature + curve_bytes,
3900 curve_bytes ) );
3901
Steven Cooremanacda8342020-07-24 23:09:52 +02003902 /* Check whether the public part is loaded. If not, load it. */
3903 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3904 {
3905 MBEDTLS_MPI_CHK(
3906 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003907 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003908 }
3909
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003910 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3911 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003912
3913cleanup:
3914 mbedtls_mpi_free( &r );
3915 mbedtls_mpi_free( &s );
3916 return( mbedtls_to_psa_error( ret ) );
3917}
John Durkop6ba40d12020-11-10 08:50:04 -08003918#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3919 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003920
Ronald Croncf56a0a2020-08-04 09:51:30 +02003921psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003922 psa_algorithm_t alg,
3923 const uint8_t *hash,
3924 size_t hash_length,
3925 uint8_t *signature,
3926 size_t signature_size,
3927 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003928{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003929 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003930 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003931 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003932
3933 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003934 /* Immediately reject a zero-length signature buffer. This guarantees
3935 * that signature must be a valid pointer. (On the other hand, the hash
3936 * buffer can in principle be empty since it doesn't actually have
3937 * to be a hash.) */
3938 if( signature_size == 0 )
3939 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003940
Ronald Cron5c522922020-11-14 16:35:34 +01003941 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3942 PSA_KEY_USAGE_SIGN_HASH,
3943 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003944 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003945 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003946 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003947 {
3948 status = PSA_ERROR_INVALID_ARGUMENT;
3949 goto exit;
3950 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003951
Steven Cooremancd84cb42020-07-16 20:28:36 +02003952 /* Try any of the available accelerators first */
3953 status = psa_driver_wrapper_sign_hash( slot,
3954 alg,
3955 hash,
3956 hash_length,
3957 signature,
3958 signature_size,
3959 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003960 if( status != PSA_ERROR_NOT_SUPPORTED ||
3961 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003962 goto exit;
3963
Steven Cooreman7a250572020-07-17 16:43:05 +02003964 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003965#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3966 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003967 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003968 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003969 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003970
Steven Cooreman4fed4552020-08-03 14:46:03 +02003971 status = psa_load_rsa_representation( slot->attr.type,
3972 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003973 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003974 &rsa );
3975 if( status != PSA_SUCCESS )
3976 goto exit;
3977
Steven Cooremana2371e52020-07-28 14:30:39 +02003978 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003979 alg,
3980 hash, hash_length,
3981 signature, signature_size,
3982 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003983
Steven Cooremana2371e52020-07-28 14:30:39 +02003984 mbedtls_rsa_free( rsa );
3985 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003986 }
3987 else
John Durkop6ba40d12020-11-10 08:50:04 -08003988#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3989 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003990 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003991 {
John Durkop9814fa22020-11-04 12:28:15 -08003992#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3993 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003994 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003995#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003996 PSA_ALG_IS_ECDSA( alg )
3997#else
3998 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3999#endif
4000 )
Steven Cooremanacda8342020-07-24 23:09:52 +02004001 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004002 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004003 status = psa_load_ecp_representation( slot->attr.type,
4004 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004005 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004006 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004007 if( status != PSA_SUCCESS )
4008 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004009 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004010 alg,
4011 hash, hash_length,
4012 signature, signature_size,
4013 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004014 mbedtls_ecp_keypair_free( ecp );
4015 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004016 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004017 else
John Durkop9814fa22020-11-04 12:28:15 -08004018#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4019 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004020 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004021 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004022 }
itayzafrir5c753392018-05-08 11:18:38 +03004023 }
4024 else
itayzafrir5c753392018-05-08 11:18:38 +03004025 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004026 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004027 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004028
4029exit:
4030 /* Fill the unused part of the output buffer (the whole buffer on error,
4031 * the trailing part on success) with something that isn't a valid mac
4032 * (barring an attack on the mac and deliberately-crafted input),
4033 * in case the caller doesn't check the return status properly. */
4034 if( status == PSA_SUCCESS )
4035 memset( signature + *signature_length, '!',
4036 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02004037 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004038 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004039 /* If signature_size is 0 then we have nothing to do. We must not call
4040 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02004041
Ronald Cron5c522922020-11-14 16:35:34 +01004042 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004043
Ronald Cron5c522922020-11-14 16:35:34 +01004044 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03004045}
4046
Ronald Croncf56a0a2020-08-04 09:51:30 +02004047psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004048 psa_algorithm_t alg,
4049 const uint8_t *hash,
4050 size_t hash_length,
4051 const uint8_t *signature,
4052 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004053{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004055 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004056 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004057
Ronald Cron5c522922020-11-14 16:35:34 +01004058 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4059 PSA_KEY_USAGE_VERIFY_HASH,
4060 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004061 if( status != PSA_SUCCESS )
4062 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004063
Steven Cooreman55ae2172020-07-17 19:46:15 +02004064 /* Try any of the available accelerators first */
4065 status = psa_driver_wrapper_verify_hash( slot,
4066 alg,
4067 hash,
4068 hash_length,
4069 signature,
4070 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004071 if( status != PSA_ERROR_NOT_SUPPORTED ||
4072 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004073 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004074
John Durkop6ba40d12020-11-10 08:50:04 -08004075#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4076 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004077 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004078 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004079 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004080
Steven Cooreman4fed4552020-08-03 14:46:03 +02004081 status = psa_load_rsa_representation( slot->attr.type,
4082 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004083 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004084 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004085 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004086 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004087
Steven Cooremana2371e52020-07-28 14:30:39 +02004088 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004089 alg,
4090 hash, hash_length,
4091 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004092 mbedtls_rsa_free( rsa );
4093 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004094 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004095 }
4096 else
John Durkop6ba40d12020-11-10 08:50:04 -08004097#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4098 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004099 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004100 {
John Durkop9814fa22020-11-04 12:28:15 -08004101#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4102 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004103 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004104 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004105 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004106 status = psa_load_ecp_representation( slot->attr.type,
4107 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004108 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004109 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004110 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004111 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004112 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004113 hash, hash_length,
4114 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004115 mbedtls_ecp_keypair_free( ecp );
4116 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004117 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004118 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004119 else
John Durkop9814fa22020-11-04 12:28:15 -08004120#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4121 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004122 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004123 status = PSA_ERROR_INVALID_ARGUMENT;
4124 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004125 }
itayzafrir5c753392018-05-08 11:18:38 +03004126 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004127 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004128 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004129 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004130 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004131
4132exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004133 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004134
Ronald Cron5c522922020-11-14 16:35:34 +01004135 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004136}
4137
John Durkop0e005192020-10-31 22:06:54 -07004138#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004139static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4140 mbedtls_rsa_context *rsa )
4141{
4142 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4143 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4144 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4145 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4146}
John Durkop0e005192020-10-31 22:06:54 -07004147#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004148
Ronald Croncf56a0a2020-08-04 09:51:30 +02004149psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004150 psa_algorithm_t alg,
4151 const uint8_t *input,
4152 size_t input_length,
4153 const uint8_t *salt,
4154 size_t salt_length,
4155 uint8_t *output,
4156 size_t output_size,
4157 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004158{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004159 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004160 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004161 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004162
Darryl Green5cc689a2018-07-24 15:34:10 +01004163 (void) input;
4164 (void) input_length;
4165 (void) salt;
4166 (void) output;
4167 (void) output_size;
4168
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004169 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004170
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004171 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4172 return( PSA_ERROR_INVALID_ARGUMENT );
4173
Ronald Cron5c522922020-11-14 16:35:34 +01004174 status = psa_get_and_lock_transparent_key_slot_with_policy(
4175 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004176 if( status != PSA_SUCCESS )
4177 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004178 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4179 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004180 {
4181 status = PSA_ERROR_INVALID_ARGUMENT;
4182 goto exit;
4183 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004184
John Durkop6ba40d12020-11-10 08:50:04 -08004185#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4186 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004187 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004188 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004189 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004190 status = psa_load_rsa_representation( slot->attr.type,
4191 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004192 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004193 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004194 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004195 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004196
4197 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004198 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004199 status = PSA_ERROR_BUFFER_TOO_SMALL;
4200 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004201 }
John Durkop0e005192020-10-31 22:06:54 -07004202#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004203 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004204 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004205 status = mbedtls_to_psa_error(
4206 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004207 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004208 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004209 MBEDTLS_RSA_PUBLIC,
4210 input_length,
4211 input,
4212 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004213 }
4214 else
John Durkop0e005192020-10-31 22:06:54 -07004215#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4216#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004217 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004218 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004219 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004220 status = mbedtls_to_psa_error(
4221 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004222 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004223 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004224 MBEDTLS_RSA_PUBLIC,
4225 salt, salt_length,
4226 input_length,
4227 input,
4228 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004229 }
4230 else
John Durkop0e005192020-10-31 22:06:54 -07004231#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004232 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004233 status = PSA_ERROR_INVALID_ARGUMENT;
4234 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004235 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004236rsa_exit:
4237 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004238 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004239
Steven Cooremana2371e52020-07-28 14:30:39 +02004240 mbedtls_rsa_free( rsa );
4241 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004242 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004243 else
John Durkop9814fa22020-11-04 12:28:15 -08004244#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004245 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004246 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004247 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004248 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004249
4250exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004251 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004252
Ronald Cron5c522922020-11-14 16:35:34 +01004253 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004254}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004255
Ronald Croncf56a0a2020-08-04 09:51:30 +02004256psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004257 psa_algorithm_t alg,
4258 const uint8_t *input,
4259 size_t input_length,
4260 const uint8_t *salt,
4261 size_t salt_length,
4262 uint8_t *output,
4263 size_t output_size,
4264 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004265{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004267 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004268 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004269
Darryl Green5cc689a2018-07-24 15:34:10 +01004270 (void) input;
4271 (void) input_length;
4272 (void) salt;
4273 (void) output;
4274 (void) output_size;
4275
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004276 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004277
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004278 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4279 return( PSA_ERROR_INVALID_ARGUMENT );
4280
Ronald Cron5c522922020-11-14 16:35:34 +01004281 status = psa_get_and_lock_transparent_key_slot_with_policy(
4282 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004283 if( status != PSA_SUCCESS )
4284 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004285 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004286 {
4287 status = PSA_ERROR_INVALID_ARGUMENT;
4288 goto exit;
4289 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004290
John Durkop6ba40d12020-11-10 08:50:04 -08004291#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4292 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004293 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004294 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004295 mbedtls_rsa_context *rsa = NULL;
4296 status = psa_load_rsa_representation( slot->attr.type,
4297 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004298 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004299 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004300 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004301 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004302
Steven Cooremana2371e52020-07-28 14:30:39 +02004303 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004304 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004305 status = PSA_ERROR_INVALID_ARGUMENT;
4306 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004307 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004308
John Durkop0e005192020-10-31 22:06:54 -07004309#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004310 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004311 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004312 status = mbedtls_to_psa_error(
4313 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004314 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004315 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004316 MBEDTLS_RSA_PRIVATE,
4317 output_length,
4318 input,
4319 output,
4320 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004321 }
4322 else
John Durkop0e005192020-10-31 22:06:54 -07004323#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4324#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004325 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004326 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004327 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004328 status = mbedtls_to_psa_error(
4329 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004330 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004331 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004332 MBEDTLS_RSA_PRIVATE,
4333 salt, salt_length,
4334 output_length,
4335 input,
4336 output,
4337 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004338 }
4339 else
John Durkop0e005192020-10-31 22:06:54 -07004340#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004341 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004342 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004343 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004344
Steven Cooreman4fed4552020-08-03 14:46:03 +02004345rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004346 mbedtls_rsa_free( rsa );
4347 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004348 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004349 else
John Durkop6ba40d12020-11-10 08:50:04 -08004350#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4351 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004352 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004353 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004354 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004355
4356exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004357 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004358
Ronald Cron5c522922020-11-14 16:35:34 +01004359 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004360}
Gilles Peskine20035e32018-02-03 22:44:14 +01004361
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004362
4363
mohammad1603503973b2018-03-12 15:59:30 +02004364/****************************************************************/
4365/* Symmetric cryptography */
4366/****************************************************************/
4367
Gilles Peskinee553c652018-06-04 16:22:46 +02004368static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004369 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004370 psa_algorithm_t alg,
4371 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004372{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004374 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004375 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004376 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004377 size_t key_bits;
4378 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004379 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4380 PSA_KEY_USAGE_ENCRYPT :
4381 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004382
Steven Cooremand3feccd2020-09-01 15:56:14 +02004383 /* A context must be freshly initialized before it can be set up. */
4384 if( operation->alg != 0 )
4385 return( PSA_ERROR_BAD_STATE );
4386
Steven Cooremana07b9972020-09-10 14:54:14 +02004387 /* The requested algorithm must be one that can be processed by cipher. */
4388 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004389 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004390
Steven Cooremanef8575e2020-09-11 11:44:50 +02004391 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004392 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004393 if( status != PSA_SUCCESS )
4394 goto exit;
4395
4396 /* Initialize the operation struct members, except for alg. The alg member
4397 * is used to indicate to psa_cipher_abort that there are resources to free,
4398 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004399 operation->key_set = 0;
4400 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004401 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004402 operation->iv_size = 0;
4403 operation->block_size = 0;
4404 if( alg == PSA_ALG_ECB_NO_PADDING )
4405 operation->iv_required = 0;
4406 else
4407 operation->iv_required = 1;
4408
Steven Cooremana07b9972020-09-10 14:54:14 +02004409 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004410 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004411 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004412 slot,
4413 alg );
4414 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004415 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004416 slot,
4417 alg );
4418
Steven Cooremanef8575e2020-09-11 11:44:50 +02004419 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004420 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004421 /* Once the driver context is initialised, it needs to be freed using
4422 * psa_cipher_abort. Indicate this through setting alg. */
4423 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004424 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004425
Steven Cooremand3feccd2020-09-01 15:56:14 +02004426 if( status != PSA_ERROR_NOT_SUPPORTED ||
4427 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4428 goto exit;
4429
Steven Cooremana07b9972020-09-10 14:54:14 +02004430 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004431 * available for the given algorithm & key. */
4432 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004433
Steven Cooremana07b9972020-09-10 14:54:14 +02004434 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004435 * psa_cipher_abort. Indicate there is something to be freed through setting
4436 * alg, and indicate the operation is being done using mbedtls crypto through
4437 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004438 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004439 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004440
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004441 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004442 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004443 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004444 {
4445 status = PSA_ERROR_NOT_SUPPORTED;
4446 goto exit;
4447 }
mohammad1603503973b2018-03-12 15:59:30 +02004448
mohammad1603503973b2018-03-12 15:59:30 +02004449 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004450 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004451 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004452
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004453#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004454 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004455 {
4456 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004457 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004458 memcpy( keys, slot->data.key.data, 16 );
4459 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004460 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4461 keys,
4462 192, cipher_operation );
4463 }
4464 else
4465#endif
4466 {
4467 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004468 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004469 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004470 }
Moran Peker41deec42018-04-04 15:43:05 +03004471 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004472 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004473
mohammad16038481e742018-03-18 13:57:31 +02004474#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004475 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004476 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004477 case PSA_ALG_CBC_NO_PADDING:
4478 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4479 MBEDTLS_PADDING_NONE );
4480 break;
4481 case PSA_ALG_CBC_PKCS7:
4482 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4483 MBEDTLS_PADDING_PKCS7 );
4484 break;
4485 default:
4486 /* The algorithm doesn't involve padding. */
4487 ret = 0;
4488 break;
4489 }
4490 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004491 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004492#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4493
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004494 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004495 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004496 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4497 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004498 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004499 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004500 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004501#if defined(MBEDTLS_CHACHA20_C)
4502 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004503 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004504 operation->iv_size = 12;
4505#endif
mohammad1603503973b2018-03-12 15:59:30 +02004506
Steven Cooreman7df02922020-09-09 15:28:49 +02004507 status = PSA_SUCCESS;
4508
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004509exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004510 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004511 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004512 if( status == PSA_SUCCESS )
4513 {
4514 /* Update operation flags for both driver and software implementations */
4515 operation->key_set = 1;
4516 }
4517 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004518 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004519
Ronald Cron5c522922020-11-14 16:35:34 +01004520 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004521
Ronald Cron5c522922020-11-14 16:35:34 +01004522 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004523}
4524
Gilles Peskinefe119512018-07-08 21:39:34 +02004525psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004526 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004527 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004528{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004529 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004530}
4531
Gilles Peskinefe119512018-07-08 21:39:34 +02004532psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004533 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004534 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004535{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004536 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004537}
4538
Gilles Peskinefe119512018-07-08 21:39:34 +02004539psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004540 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004541 size_t iv_size,
4542 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004543{
itayzafrir534bd7c2018-08-02 13:56:32 +03004544 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004546 if( operation->iv_set || ! operation->iv_required )
4547 {
4548 return( PSA_ERROR_BAD_STATE );
4549 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004550
Steven Cooremanef8575e2020-09-11 11:44:50 +02004551 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004552 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004553 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004554 iv,
4555 iv_size,
4556 iv_length );
4557 goto exit;
4558 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004559
Moran Peker41deec42018-04-04 15:43:05 +03004560 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004561 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004562 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004563 goto exit;
4564 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004565 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004566 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004567 if( ret != 0 )
4568 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004569 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004570 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004571 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004572
mohammad16038481e742018-03-18 13:57:31 +02004573 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004574 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004575
Moran Peker395db872018-05-31 14:07:14 +03004576exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004577 if( status == PSA_SUCCESS )
4578 operation->iv_set = 1;
4579 else
Moran Peker395db872018-05-31 14:07:14 +03004580 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004581 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004582}
4583
Gilles Peskinefe119512018-07-08 21:39:34 +02004584psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004585 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004586 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004587{
itayzafrir534bd7c2018-08-02 13:56:32 +03004588 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004590 if( operation->iv_set || ! operation->iv_required )
4591 {
4592 return( PSA_ERROR_BAD_STATE );
4593 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004594
Steven Cooremanef8575e2020-09-11 11:44:50 +02004595 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004596 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004597 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004598 iv,
4599 iv_length );
4600 goto exit;
4601 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004602
Moran Pekera28258c2018-05-29 16:25:04 +03004603 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004604 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004605 status = PSA_ERROR_INVALID_ARGUMENT;
4606 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004607 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004608 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4609 status = mbedtls_to_psa_error( ret );
4610exit:
4611 if( status == PSA_SUCCESS )
4612 operation->iv_set = 1;
4613 else
mohammad1603503973b2018-03-12 15:59:30 +02004614 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004615 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004616}
4617
Steven Cooreman177deba2020-09-07 17:14:14 +02004618/* Process input for which the algorithm is set to ECB mode. This requires
4619 * manual processing, since the PSA API is defined as being able to process
4620 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4621 * underlying mbedtls_cipher_update only takes full blocks. */
4622static psa_status_t psa_cipher_update_ecb_internal(
4623 mbedtls_cipher_context_t *ctx,
4624 const uint8_t *input,
4625 size_t input_length,
4626 uint8_t *output,
4627 size_t output_size,
4628 size_t *output_length )
4629{
4630 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4631 size_t block_size = ctx->cipher_info->block_size;
4632 size_t internal_output_length = 0;
4633 *output_length = 0;
4634
4635 if( input_length == 0 )
4636 {
4637 status = PSA_SUCCESS;
4638 goto exit;
4639 }
4640
4641 if( ctx->unprocessed_len > 0 )
4642 {
4643 /* Fill up to block size, and run the block if there's a full one. */
4644 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4645
4646 if( input_length < bytes_to_copy )
4647 bytes_to_copy = input_length;
4648
4649 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4650 input, bytes_to_copy );
4651 input_length -= bytes_to_copy;
4652 input += bytes_to_copy;
4653 ctx->unprocessed_len += bytes_to_copy;
4654
4655 if( ctx->unprocessed_len == block_size )
4656 {
4657 status = mbedtls_to_psa_error(
4658 mbedtls_cipher_update( ctx,
4659 ctx->unprocessed_data,
4660 block_size,
4661 output, &internal_output_length ) );
4662
4663 if( status != PSA_SUCCESS )
4664 goto exit;
4665
4666 output += internal_output_length;
4667 output_size -= internal_output_length;
4668 *output_length += internal_output_length;
4669 ctx->unprocessed_len = 0;
4670 }
4671 }
4672
4673 while( input_length >= block_size )
4674 {
4675 /* Run all full blocks we have, one by one */
4676 status = mbedtls_to_psa_error(
4677 mbedtls_cipher_update( ctx, input,
4678 block_size,
4679 output, &internal_output_length ) );
4680
4681 if( status != PSA_SUCCESS )
4682 goto exit;
4683
4684 input_length -= block_size;
4685 input += block_size;
4686
4687 output += internal_output_length;
4688 output_size -= internal_output_length;
4689 *output_length += internal_output_length;
4690 }
4691
4692 if( input_length > 0 )
4693 {
4694 /* Save unprocessed bytes for later processing */
4695 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4696 input, input_length );
4697 ctx->unprocessed_len += input_length;
4698 }
4699
4700 status = PSA_SUCCESS;
4701
4702exit:
4703 return( status );
4704}
4705
Gilles Peskinee553c652018-06-04 16:22:46 +02004706psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4707 const uint8_t *input,
4708 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004709 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004710 size_t output_size,
4711 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004712{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004713 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004714 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004715 if( operation->alg == 0 )
4716 {
4717 return( PSA_ERROR_BAD_STATE );
4718 }
4719 if( operation->iv_required && ! operation->iv_set )
4720 {
4721 return( PSA_ERROR_BAD_STATE );
4722 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004723
Steven Cooremanef8575e2020-09-11 11:44:50 +02004724 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004725 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004726 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004727 input,
4728 input_length,
4729 output,
4730 output_size,
4731 output_length );
4732 goto exit;
4733 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004734
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004735 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004736 {
4737 /* Take the unprocessed partial block left over from previous
4738 * update calls, if any, plus the input to this call. Remove
4739 * the last partial block, if any. You get the data that will be
4740 * output in this call. */
4741 expected_output_size =
4742 ( operation->ctx.cipher.unprocessed_len + input_length )
4743 / operation->block_size * operation->block_size;
4744 }
4745 else
4746 {
4747 expected_output_size = input_length;
4748 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004749
Gilles Peskine89d789c2018-06-04 17:17:16 +02004750 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004751 {
4752 status = PSA_ERROR_BUFFER_TOO_SMALL;
4753 goto exit;
4754 }
mohammad160382759612018-03-12 18:16:40 +02004755
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004756 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4757 {
4758 /* mbedtls_cipher_update has an API inconsistency: it will only
4759 * process a single block at a time in ECB mode. Abstract away that
4760 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004761 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4762 input,
4763 input_length,
4764 output,
4765 output_size,
4766 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004767 }
4768 else
4769 {
4770 status = mbedtls_to_psa_error(
4771 mbedtls_cipher_update( &operation->ctx.cipher, input,
4772 input_length, output, output_length ) );
4773 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004774exit:
4775 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004776 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004777 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004778}
4779
Gilles Peskinee553c652018-06-04 16:22:46 +02004780psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4781 uint8_t *output,
4782 size_t output_size,
4783 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004784{
David Saadab4ecc272019-02-14 13:48:10 +02004785 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004786 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004787 if( operation->alg == 0 )
4788 {
4789 return( PSA_ERROR_BAD_STATE );
4790 }
4791 if( operation->iv_required && ! operation->iv_set )
4792 {
4793 return( PSA_ERROR_BAD_STATE );
4794 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004795
Steven Cooremanef8575e2020-09-11 11:44:50 +02004796 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004797 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004798 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004799 output,
4800 output_size,
4801 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004802 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004803 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004804
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004805 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004806 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004807 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004808 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004809 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004810 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004811 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004812 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004813 }
4814
Steven Cooremanef8575e2020-09-11 11:44:50 +02004815 status = mbedtls_to_psa_error(
4816 mbedtls_cipher_finish( &operation->ctx.cipher,
4817 temp_output_buffer,
4818 output_length ) );
4819 if( status != PSA_SUCCESS )
4820 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004821
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004822 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004823 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004824 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004825 memcpy( output, temp_output_buffer, *output_length );
4826 else
Janos Follath315b51c2018-07-09 16:04:51 +01004827 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004828
Steven Cooremanef8575e2020-09-11 11:44:50 +02004829exit:
4830 if( operation->mbedtls_in_use == 1 )
4831 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004832
Steven Cooremanef8575e2020-09-11 11:44:50 +02004833 if( status == PSA_SUCCESS )
4834 return( psa_cipher_abort( operation ) );
4835 else
4836 {
4837 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004838 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004839
Steven Cooremanef8575e2020-09-11 11:44:50 +02004840 return( status );
4841 }
mohammad1603503973b2018-03-12 15:59:30 +02004842}
4843
Gilles Peskinee553c652018-06-04 16:22:46 +02004844psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4845{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004846 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004847 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004848 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004849 * in use. It's ok to call abort on such an object, and there's
4850 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004851 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004852 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004853
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004854 /* Sanity check (shouldn't happen: operation->alg should
4855 * always have been initialized to a valid value). */
4856 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4857 return( PSA_ERROR_BAD_STATE );
4858
Steven Cooremanef8575e2020-09-11 11:44:50 +02004859 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004860 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004861 else
4862 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004863
Moran Peker41deec42018-04-04 15:43:05 +03004864 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004865 operation->key_set = 0;
4866 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004867 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004868 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004869 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004870 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004871
Moran Peker395db872018-05-31 14:07:14 +03004872 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004873}
4874
Gilles Peskinea0655c32018-04-30 17:06:50 +02004875
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004876
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004877
mohammad16035955c982018-04-26 00:53:03 +03004878/****************************************************************/
4879/* AEAD */
4880/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004881
Gilles Peskineedf9a652018-08-17 18:11:56 +02004882typedef struct
4883{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004884 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004885 const mbedtls_cipher_info_t *cipher_info;
4886 union
4887 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004888 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004889#if defined(MBEDTLS_CCM_C)
4890 mbedtls_ccm_context ccm;
4891#endif /* MBEDTLS_CCM_C */
4892#if defined(MBEDTLS_GCM_C)
4893 mbedtls_gcm_context gcm;
4894#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004895#if defined(MBEDTLS_CHACHAPOLY_C)
4896 mbedtls_chachapoly_context chachapoly;
4897#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004898 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004899 psa_algorithm_t core_alg;
4900 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004901 uint8_t tag_length;
4902} aead_operation_t;
4903
Ronald Cronf95a2b12020-10-22 15:24:49 +02004904#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4905
Gilles Peskine30a9e412019-01-14 18:36:12 +01004906static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004907{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004908 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004909 {
4910#if defined(MBEDTLS_CCM_C)
4911 case PSA_ALG_CCM:
4912 mbedtls_ccm_free( &operation->ctx.ccm );
4913 break;
4914#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004915#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004916 case PSA_ALG_GCM:
4917 mbedtls_gcm_free( &operation->ctx.gcm );
4918 break;
4919#endif /* MBEDTLS_GCM_C */
4920 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004921
Ronald Cron5c522922020-11-14 16:35:34 +01004922 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004923}
4924
4925static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004926 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004927 psa_key_usage_t usage,
4928 psa_algorithm_t alg )
4929{
4930 psa_status_t status;
4931 size_t key_bits;
4932 mbedtls_cipher_id_t cipher_id;
4933
Ronald Cron5c522922020-11-14 16:35:34 +01004934 status = psa_get_and_lock_transparent_key_slot_with_policy(
4935 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004936 if( status != PSA_SUCCESS )
4937 return( status );
4938
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004939 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004940
4941 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004942 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004943 &cipher_id );
4944 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004945 {
4946 status = PSA_ERROR_NOT_SUPPORTED;
4947 goto cleanup;
4948 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004949
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004950 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004951 {
4952#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004953 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4954 operation->core_alg = PSA_ALG_CCM;
4955 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004956 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4957 * The call to mbedtls_ccm_encrypt_and_tag or
4958 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004959 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004960 {
4961 status = PSA_ERROR_INVALID_ARGUMENT;
4962 goto cleanup;
4963 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004964 mbedtls_ccm_init( &operation->ctx.ccm );
4965 status = mbedtls_to_psa_error(
4966 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004967 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004968 (unsigned int) key_bits ) );
4969 if( status != 0 )
4970 goto cleanup;
4971 break;
4972#endif /* MBEDTLS_CCM_C */
4973
4974#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004975 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4976 operation->core_alg = PSA_ALG_GCM;
4977 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004978 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4979 * The call to mbedtls_gcm_crypt_and_tag or
4980 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004981 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004982 {
4983 status = PSA_ERROR_INVALID_ARGUMENT;
4984 goto cleanup;
4985 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004986 mbedtls_gcm_init( &operation->ctx.gcm );
4987 status = mbedtls_to_psa_error(
4988 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004989 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004990 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004991 if( status != 0 )
4992 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004993 break;
4994#endif /* MBEDTLS_GCM_C */
4995
Gilles Peskine26869f22019-05-06 15:25:00 +02004996#if defined(MBEDTLS_CHACHAPOLY_C)
4997 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4998 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4999 operation->full_tag_length = 16;
5000 /* We only support the default tag length. */
5001 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02005002 {
5003 status = PSA_ERROR_NOT_SUPPORTED;
5004 goto cleanup;
5005 }
Gilles Peskine26869f22019-05-06 15:25:00 +02005006 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
5007 status = mbedtls_to_psa_error(
5008 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005009 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02005010 if( status != 0 )
5011 goto cleanup;
5012 break;
5013#endif /* MBEDTLS_CHACHAPOLY_C */
5014
Gilles Peskineedf9a652018-08-17 18:11:56 +02005015 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02005016 status = PSA_ERROR_NOT_SUPPORTED;
5017 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005018 }
5019
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005020 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
5021 {
5022 status = PSA_ERROR_INVALID_ARGUMENT;
5023 goto cleanup;
5024 }
5025 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005026
Gilles Peskineedf9a652018-08-17 18:11:56 +02005027 return( PSA_SUCCESS );
5028
5029cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005030 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005031 return( status );
5032}
5033
Ronald Croncf56a0a2020-08-04 09:51:30 +02005034psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005035 psa_algorithm_t alg,
5036 const uint8_t *nonce,
5037 size_t nonce_length,
5038 const uint8_t *additional_data,
5039 size_t additional_data_length,
5040 const uint8_t *plaintext,
5041 size_t plaintext_length,
5042 uint8_t *ciphertext,
5043 size_t ciphertext_size,
5044 size_t *ciphertext_length )
5045{
mohammad16035955c982018-04-26 00:53:03 +03005046 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005047 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03005048 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005049
mohammad1603f08a5502018-06-03 15:05:47 +03005050 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005051
Ronald Croncf56a0a2020-08-04 09:51:30 +02005052 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005053 if( status != PSA_SUCCESS )
5054 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005055
Gilles Peskineedf9a652018-08-17 18:11:56 +02005056 /* For all currently supported modes, the tag is at the end of the
5057 * ciphertext. */
5058 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5059 {
5060 status = PSA_ERROR_BUFFER_TOO_SMALL;
5061 goto exit;
5062 }
5063 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005064
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005065#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005066 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005067 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005068 status = mbedtls_to_psa_error(
5069 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5070 MBEDTLS_GCM_ENCRYPT,
5071 plaintext_length,
5072 nonce, nonce_length,
5073 additional_data, additional_data_length,
5074 plaintext, ciphertext,
5075 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005076 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005077 else
5078#endif /* MBEDTLS_GCM_C */
5079#if defined(MBEDTLS_CCM_C)
5080 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005081 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005082 status = mbedtls_to_psa_error(
5083 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5084 plaintext_length,
5085 nonce, nonce_length,
5086 additional_data,
5087 additional_data_length,
5088 plaintext, ciphertext,
5089 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005090 }
mohammad16035c8845f2018-05-09 05:40:09 -07005091 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005092#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005093#if defined(MBEDTLS_CHACHAPOLY_C)
5094 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5095 {
5096 if( nonce_length != 12 || operation.tag_length != 16 )
5097 {
5098 status = PSA_ERROR_NOT_SUPPORTED;
5099 goto exit;
5100 }
5101 status = mbedtls_to_psa_error(
5102 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5103 plaintext_length,
5104 nonce,
5105 additional_data,
5106 additional_data_length,
5107 plaintext,
5108 ciphertext,
5109 tag ) );
5110 }
5111 else
5112#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005113 {
mohammad1603554faad2018-06-03 15:07:38 +03005114 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005115 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005116
Gilles Peskineedf9a652018-08-17 18:11:56 +02005117 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5118 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005119
Gilles Peskineedf9a652018-08-17 18:11:56 +02005120exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005121 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005122 if( status == PSA_SUCCESS )
5123 *ciphertext_length = plaintext_length + operation.tag_length;
5124 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005125}
5126
Gilles Peskineee652a32018-06-01 19:23:52 +02005127/* Locate the tag in a ciphertext buffer containing the encrypted data
5128 * followed by the tag. Return the length of the part preceding the tag in
5129 * *plaintext_length. This is the size of the plaintext in modes where
5130 * the encrypted data has the same size as the plaintext, such as
5131 * CCM and GCM. */
5132static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5133 const uint8_t *ciphertext,
5134 size_t ciphertext_length,
5135 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005136 const uint8_t **p_tag )
5137{
5138 size_t payload_length;
5139 if( tag_length > ciphertext_length )
5140 return( PSA_ERROR_INVALID_ARGUMENT );
5141 payload_length = ciphertext_length - tag_length;
5142 if( payload_length > plaintext_size )
5143 return( PSA_ERROR_BUFFER_TOO_SMALL );
5144 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005145 return( PSA_SUCCESS );
5146}
5147
Ronald Croncf56a0a2020-08-04 09:51:30 +02005148psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005149 psa_algorithm_t alg,
5150 const uint8_t *nonce,
5151 size_t nonce_length,
5152 const uint8_t *additional_data,
5153 size_t additional_data_length,
5154 const uint8_t *ciphertext,
5155 size_t ciphertext_length,
5156 uint8_t *plaintext,
5157 size_t plaintext_size,
5158 size_t *plaintext_length )
5159{
mohammad16035955c982018-04-26 00:53:03 +03005160 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005161 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005162 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005163
Gilles Peskineee652a32018-06-01 19:23:52 +02005164 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005165
Ronald Croncf56a0a2020-08-04 09:51:30 +02005166 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005167 if( status != PSA_SUCCESS )
5168 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005169
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005170 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5171 ciphertext, ciphertext_length,
5172 plaintext_size, &tag );
5173 if( status != PSA_SUCCESS )
5174 goto exit;
5175
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005176#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005177 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005178 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005179 status = mbedtls_to_psa_error(
5180 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5181 ciphertext_length - operation.tag_length,
5182 nonce, nonce_length,
5183 additional_data,
5184 additional_data_length,
5185 tag, operation.tag_length,
5186 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005187 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005188 else
5189#endif /* MBEDTLS_GCM_C */
5190#if defined(MBEDTLS_CCM_C)
5191 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005192 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005193 status = mbedtls_to_psa_error(
5194 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5195 ciphertext_length - operation.tag_length,
5196 nonce, nonce_length,
5197 additional_data,
5198 additional_data_length,
5199 ciphertext, plaintext,
5200 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005201 }
mohammad160339574652018-06-01 04:39:53 -07005202 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005203#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005204#if defined(MBEDTLS_CHACHAPOLY_C)
5205 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5206 {
5207 if( nonce_length != 12 || operation.tag_length != 16 )
5208 {
5209 status = PSA_ERROR_NOT_SUPPORTED;
5210 goto exit;
5211 }
5212 status = mbedtls_to_psa_error(
5213 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5214 ciphertext_length - operation.tag_length,
5215 nonce,
5216 additional_data,
5217 additional_data_length,
5218 tag,
5219 ciphertext,
5220 plaintext ) );
5221 }
5222 else
5223#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005224 {
mohammad1603554faad2018-06-03 15:07:38 +03005225 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005226 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005227
Gilles Peskineedf9a652018-08-17 18:11:56 +02005228 if( status != PSA_SUCCESS && plaintext_size != 0 )
5229 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005230
Gilles Peskineedf9a652018-08-17 18:11:56 +02005231exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005232 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005233 if( status == PSA_SUCCESS )
5234 *plaintext_length = ciphertext_length - operation.tag_length;
5235 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005236}
5237
Gilles Peskinea0655c32018-04-30 17:06:50 +02005238
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005239
Gilles Peskine20035e32018-02-03 22:44:14 +01005240/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005241/* Generators */
5242/****************************************************************/
5243
John Durkop07cc04a2020-11-16 22:08:34 -08005244#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5245 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5246 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5247#define AT_LEAST_ONE_BUILTIN_KDF
5248#endif
5249
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250#define HKDF_STATE_INIT 0 /* no input yet */
5251#define HKDF_STATE_STARTED 1 /* got salt */
5252#define HKDF_STATE_KEYED 2 /* got key */
5253#define HKDF_STATE_OUTPUT 3 /* output started */
5254
Gilles Peskinecbe66502019-05-16 16:59:18 +02005255static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005256 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005257{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005258 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5259 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005260 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005262}
5263
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005265{
5266 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005269 {
5270 /* The object has (apparently) been initialized but it is not
5271 * in use. It's ok to call abort on such an object, and there's
5272 * nothing to do. */
5273 }
5274 else
John Durkopd0321952020-10-29 21:37:36 -07005275#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005276 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005277 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278 mbedtls_free( operation->ctx.hkdf.info );
5279 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005280 }
John Durkop07cc04a2020-11-16 22:08:34 -08005281 else
5282#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5283#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5284 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5285 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005287 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005288 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005289 if( operation->ctx.tls12_prf.seed != NULL )
5290 {
5291 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5292 operation->ctx.tls12_prf.seed_length );
5293 mbedtls_free( operation->ctx.tls12_prf.seed );
5294 }
5295
5296 if( operation->ctx.tls12_prf.label != NULL )
5297 {
5298 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5299 operation->ctx.tls12_prf.label_length );
5300 mbedtls_free( operation->ctx.tls12_prf.label );
5301 }
5302
5303 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5304
5305 /* We leave the fields Ai and output_block to be erased safely by the
5306 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005307 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005308 else
John Durkop07cc04a2020-11-16 22:08:34 -08005309#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5310 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005311 {
5312 status = PSA_ERROR_BAD_STATE;
5313 }
Janos Follath083036a2019-06-11 10:22:26 +01005314 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005315 return( status );
5316}
5317
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005319 size_t *capacity)
5320{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005321 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005322 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005323 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005324 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005325 }
5326
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005327 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005328 return( PSA_SUCCESS );
5329}
5330
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005332 size_t capacity )
5333{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005335 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005336 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005337 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005339 return( PSA_SUCCESS );
5340}
5341
John Durkopd0321952020-10-29 21:37:36 -07005342#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005343/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005344 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005345static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005346 psa_algorithm_t hash_alg,
5347 uint8_t *output,
5348 size_t output_length )
5349{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005350 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005351 psa_status_t status;
5352
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005353 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5354 return( PSA_ERROR_BAD_STATE );
5355 hkdf->state = HKDF_STATE_OUTPUT;
5356
Gilles Peskinebef7f142018-07-12 17:22:21 +02005357 while( output_length != 0 )
5358 {
5359 /* Copy what remains of the current block */
5360 uint8_t n = hash_length - hkdf->offset_in_block;
5361 if( n > output_length )
5362 n = (uint8_t) output_length;
5363 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5364 output += n;
5365 output_length -= n;
5366 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005367 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005368 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005369 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005370 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005371 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005372 * object was corrupted or if this function is called directly
5373 * inside the library. */
5374 if( hkdf->block_number == 0xff )
5375 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005376
5377 /* We need a new block */
5378 ++hkdf->block_number;
5379 hkdf->offset_in_block = 0;
5380 status = psa_hmac_setup_internal( &hkdf->hmac,
5381 hkdf->prk, hash_length,
5382 hash_alg );
5383 if( status != PSA_SUCCESS )
5384 return( status );
5385 if( hkdf->block_number != 1 )
5386 {
5387 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5388 hkdf->output_block,
5389 hash_length );
5390 if( status != PSA_SUCCESS )
5391 return( status );
5392 }
5393 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5394 hkdf->info,
5395 hkdf->info_length );
5396 if( status != PSA_SUCCESS )
5397 return( status );
5398 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5399 &hkdf->block_number, 1 );
5400 if( status != PSA_SUCCESS )
5401 return( status );
5402 status = psa_hmac_finish_internal( &hkdf->hmac,
5403 hkdf->output_block,
5404 sizeof( hkdf->output_block ) );
5405 if( status != PSA_SUCCESS )
5406 return( status );
5407 }
5408
5409 return( PSA_SUCCESS );
5410}
John Durkop07cc04a2020-11-16 22:08:34 -08005411#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005412
John Durkop07cc04a2020-11-16 22:08:34 -08005413#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5414 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005415static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5416 psa_tls12_prf_key_derivation_t *tls12_prf,
5417 psa_algorithm_t alg )
5418{
5419 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005420 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005421 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5422 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005423
Janos Follath7742fee2019-06-17 12:58:10 +01005424 /* We can't be wanting more output after block 0xff, otherwise
5425 * the capacity check in psa_key_derivation_output_bytes() would have
5426 * prevented this call. It could happen only if the operation
5427 * object was corrupted or if this function is called directly
5428 * inside the library. */
5429 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005430 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005431
5432 /* We need a new block */
5433 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005434 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005435
5436 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5437 *
5438 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5439 *
5440 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5441 * HMAC_hash(secret, A(2) + seed) +
5442 * HMAC_hash(secret, A(3) + seed) + ...
5443 *
5444 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005445 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005446 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005447 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005448 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005449 * is currently extracted as `output_block` and where i is
5450 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005451 */
5452
Janos Follathea29bfb2019-06-19 12:21:20 +01005453 /* Save the hash context before using it, to preserve the hash state with
5454 * only the inner padding in it. We need this, because inner padding depends
5455 * on the key (secret in the RFC's terminology). */
5456 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5457 if( status != PSA_SUCCESS )
5458 goto cleanup;
5459
5460 /* Calculate A(i) where i = tls12_prf->block_number. */
5461 if( tls12_prf->block_number == 1 )
5462 {
5463 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5464 * the variable seed and in this instance means it in the context of the
5465 * P_hash function, where seed = label + seed.) */
5466 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5467 tls12_prf->label, tls12_prf->label_length );
5468 if( status != PSA_SUCCESS )
5469 goto cleanup;
5470 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5471 tls12_prf->seed, tls12_prf->seed_length );
5472 if( status != PSA_SUCCESS )
5473 goto cleanup;
5474 }
5475 else
5476 {
5477 /* A(i) = HMAC_hash(secret, A(i-1)) */
5478 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5479 tls12_prf->Ai, hash_length );
5480 if( status != PSA_SUCCESS )
5481 goto cleanup;
5482 }
5483
5484 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5485 tls12_prf->Ai, hash_length );
5486 if( status != PSA_SUCCESS )
5487 goto cleanup;
5488 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5489 if( status != PSA_SUCCESS )
5490 goto cleanup;
5491
5492 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5493 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5494 tls12_prf->Ai, hash_length );
5495 if( status != PSA_SUCCESS )
5496 goto cleanup;
5497 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5498 tls12_prf->label, tls12_prf->label_length );
5499 if( status != PSA_SUCCESS )
5500 goto cleanup;
5501 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5502 tls12_prf->seed, tls12_prf->seed_length );
5503 if( status != PSA_SUCCESS )
5504 goto cleanup;
5505 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5506 tls12_prf->output_block, hash_length );
5507 if( status != PSA_SUCCESS )
5508 goto cleanup;
5509 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5510 if( status != PSA_SUCCESS )
5511 goto cleanup;
5512
Janos Follath7742fee2019-06-17 12:58:10 +01005513
5514cleanup:
5515
Janos Follathea29bfb2019-06-19 12:21:20 +01005516 cleanup_status = psa_hash_abort( &backup );
5517 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5518 status = cleanup_status;
5519
5520 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005521}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005522
Janos Follath844eb0e2019-06-19 12:10:49 +01005523static psa_status_t psa_key_derivation_tls12_prf_read(
5524 psa_tls12_prf_key_derivation_t *tls12_prf,
5525 psa_algorithm_t alg,
5526 uint8_t *output,
5527 size_t output_length )
5528{
5529 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005530 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005531 psa_status_t status;
5532 uint8_t offset, length;
5533
5534 while( output_length != 0 )
5535 {
5536 /* Check if we have fully processed the current block. */
5537 if( tls12_prf->left_in_block == 0 )
5538 {
5539 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5540 alg );
5541 if( status != PSA_SUCCESS )
5542 return( status );
5543
5544 continue;
5545 }
5546
5547 if( tls12_prf->left_in_block > output_length )
5548 length = (uint8_t) output_length;
5549 else
5550 length = tls12_prf->left_in_block;
5551
5552 offset = hash_length - tls12_prf->left_in_block;
5553 memcpy( output, tls12_prf->output_block + offset, length );
5554 output += length;
5555 output_length -= length;
5556 tls12_prf->left_in_block -= length;
5557 }
5558
5559 return( PSA_SUCCESS );
5560}
John Durkop07cc04a2020-11-16 22:08:34 -08005561#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5562 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005563
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005564psa_status_t psa_key_derivation_output_bytes(
5565 psa_key_derivation_operation_t *operation,
5566 uint8_t *output,
5567 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005568{
5569 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005570 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005571
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005572 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005573 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005574 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005575 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005576 }
5577
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005578 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005579 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005580 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005581 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005582 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005583 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005584 goto exit;
5585 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005586 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005587 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005588 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005589 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005590 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5591 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005592 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005593 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005594 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005595 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005596 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005597
John Durkopd0321952020-10-29 21:37:36 -07005598#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005599 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005600 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005601 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005602 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005603 output, output_length );
5604 }
Janos Follathadbec812019-06-14 11:05:39 +01005605 else
John Durkop07cc04a2020-11-16 22:08:34 -08005606#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5607#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5608 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005609 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005610 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005611 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005612 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005613 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005614 output_length );
5615 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005616 else
John Durkop07cc04a2020-11-16 22:08:34 -08005617#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5618 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005619 {
5620 return( PSA_ERROR_BAD_STATE );
5621 }
5622
5623exit:
5624 if( status != PSA_SUCCESS )
5625 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005626 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005627 * This allows us to differentiate between exhausted operations and
5628 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5629 * operations. */
5630 psa_algorithm_t alg = operation->alg;
5631 psa_key_derivation_abort( operation );
5632 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005633 memset( output, '!', output_length );
5634 }
5635 return( status );
5636}
5637
Gilles Peskine08542d82018-07-19 17:05:42 +02005638#if defined(MBEDTLS_DES_C)
5639static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5640{
5641 if( data_size >= 8 )
5642 mbedtls_des_key_set_parity( data );
5643 if( data_size >= 16 )
5644 mbedtls_des_key_set_parity( data + 8 );
5645 if( data_size >= 24 )
5646 mbedtls_des_key_set_parity( data + 16 );
5647}
5648#endif /* MBEDTLS_DES_C */
5649
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005650static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005651 psa_key_slot_t *slot,
5652 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005653 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005654{
5655 uint8_t *data = NULL;
5656 size_t bytes = PSA_BITS_TO_BYTES( bits );
5657 psa_status_t status;
5658
Gilles Peskine8e338702019-07-30 20:06:31 +02005659 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005660 return( PSA_ERROR_INVALID_ARGUMENT );
5661 if( bits % 8 != 0 )
5662 return( PSA_ERROR_INVALID_ARGUMENT );
5663 data = mbedtls_calloc( 1, bytes );
5664 if( data == NULL )
5665 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5666
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005667 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005668 if( status != PSA_SUCCESS )
5669 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005670#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005671 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005672 psa_des_set_key_parity( data, bytes );
5673#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005674 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005675
5676exit:
5677 mbedtls_free( data );
5678 return( status );
5679}
5680
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005681psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005683 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005684{
5685 psa_status_t status;
5686 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005687 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005688
Ronald Cron81709fc2020-11-14 12:10:32 +01005689 *key = MBEDTLS_SVC_KEY_ID_INIT;
5690
Gilles Peskine0f84d622019-09-12 19:03:13 +02005691 /* Reject any attempt to create a zero-length key so that we don't
5692 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5693 if( psa_get_key_bits( attributes ) == 0 )
5694 return( PSA_ERROR_INVALID_ARGUMENT );
5695
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005696 if( ! operation->can_output_key )
5697 return( PSA_ERROR_NOT_PERMITTED );
5698
Ronald Cron81709fc2020-11-14 12:10:32 +01005699 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5700 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005701#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5702 if( driver != NULL )
5703 {
5704 /* Deriving a key in a secure element is not implemented yet. */
5705 status = PSA_ERROR_NOT_SUPPORTED;
5706 }
5707#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005708 if( status == PSA_SUCCESS )
5709 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005710 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005711 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005712 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005713 }
5714 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005715 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005716 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005717 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005718
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005719 return( status );
5720}
5721
Gilles Peskineeab56e42018-07-12 17:12:33 +02005722
5723
5724/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005725/* Key derivation */
5726/****************************************************************/
5727
John Durkop07cc04a2020-11-16 22:08:34 -08005728#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005729static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005731 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005732{
John Durkop07cc04a2020-11-16 22:08:34 -08005733 int is_kdf_alg_supported;
5734
Janos Follath5fe19732019-06-20 15:09:30 +01005735 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5736 * initialisers for this union leave some bytes unspecified.) */
5737 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5738
Gilles Peskine969c5d62019-01-16 15:53:06 +01005739 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005740#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005741 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5742 is_kdf_alg_supported = 1;
5743 else
5744#endif
5745#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5746 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5747 is_kdf_alg_supported = 1;
5748 else
5749#endif
5750#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5751 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5752 is_kdf_alg_supported = 1;
5753 else
5754#endif
5755 is_kdf_alg_supported = 0;
5756
5757 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005758 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005759 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005760 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005761 if( hash_size == 0 )
5762 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005763 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5764 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005765 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005766 {
5767 return( PSA_ERROR_NOT_SUPPORTED );
5768 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005769 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005770 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005771 }
John Durkop07cc04a2020-11-16 22:08:34 -08005772
5773 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005774}
John Durkop07cc04a2020-11-16 22:08:34 -08005775#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005776
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005777psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005778 psa_algorithm_t alg )
5779{
5780 psa_status_t status;
5781
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005782 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005783 return( PSA_ERROR_BAD_STATE );
5784
Gilles Peskine6843c292019-01-18 16:44:49 +01005785 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5786 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005787#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005788 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005789 {
5790 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005791 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005792 }
5793 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5794 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005795 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005796 }
John Durkop07cc04a2020-11-16 22:08:34 -08005797#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005798 else
5799 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005800
5801 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005802 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005803 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005804}
5805
John Durkopd0321952020-10-29 21:37:36 -07005806#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005807static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005808 psa_algorithm_t hash_alg,
5809 psa_key_derivation_step_t step,
5810 const uint8_t *data,
5811 size_t data_length )
5812{
5813 psa_status_t status;
5814 switch( step )
5815 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005816 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005817 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005818 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005819 status = psa_hmac_setup_internal( &hkdf->hmac,
5820 data, data_length,
5821 hash_alg );
5822 if( status != PSA_SUCCESS )
5823 return( status );
5824 hkdf->state = HKDF_STATE_STARTED;
5825 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005826 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005827 /* If no salt was provided, use an empty salt. */
5828 if( hkdf->state == HKDF_STATE_INIT )
5829 {
5830 status = psa_hmac_setup_internal( &hkdf->hmac,
5831 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005832 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005833 if( status != PSA_SUCCESS )
5834 return( status );
5835 hkdf->state = HKDF_STATE_STARTED;
5836 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005837 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005838 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005839 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5840 data, data_length );
5841 if( status != PSA_SUCCESS )
5842 return( status );
5843 status = psa_hmac_finish_internal( &hkdf->hmac,
5844 hkdf->prk,
5845 sizeof( hkdf->prk ) );
5846 if( status != PSA_SUCCESS )
5847 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005848 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005849 hkdf->block_number = 0;
5850 hkdf->state = HKDF_STATE_KEYED;
5851 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005852 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005853 if( hkdf->state == HKDF_STATE_OUTPUT )
5854 return( PSA_ERROR_BAD_STATE );
5855 if( hkdf->info_set )
5856 return( PSA_ERROR_BAD_STATE );
5857 hkdf->info_length = data_length;
5858 if( data_length != 0 )
5859 {
5860 hkdf->info = mbedtls_calloc( 1, data_length );
5861 if( hkdf->info == NULL )
5862 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5863 memcpy( hkdf->info, data, data_length );
5864 }
5865 hkdf->info_set = 1;
5866 return( PSA_SUCCESS );
5867 default:
5868 return( PSA_ERROR_INVALID_ARGUMENT );
5869 }
5870}
John Durkop07cc04a2020-11-16 22:08:34 -08005871#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005872
John Durkop07cc04a2020-11-16 22:08:34 -08005873#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5874 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005875static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5876 const uint8_t *data,
5877 size_t data_length )
5878{
5879 if( prf->state != TLS12_PRF_STATE_INIT )
5880 return( PSA_ERROR_BAD_STATE );
5881
Janos Follathd6dce9f2019-07-04 09:11:38 +01005882 if( data_length != 0 )
5883 {
5884 prf->seed = mbedtls_calloc( 1, data_length );
5885 if( prf->seed == NULL )
5886 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005887
Janos Follathd6dce9f2019-07-04 09:11:38 +01005888 memcpy( prf->seed, data, data_length );
5889 prf->seed_length = data_length;
5890 }
Janos Follathf08e2652019-06-13 09:05:41 +01005891
5892 prf->state = TLS12_PRF_STATE_SEED_SET;
5893
5894 return( PSA_SUCCESS );
5895}
5896
Janos Follath81550542019-06-13 14:26:34 +01005897static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5898 psa_algorithm_t hash_alg,
5899 const uint8_t *data,
5900 size_t data_length )
5901{
5902 psa_status_t status;
5903 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5904 return( PSA_ERROR_BAD_STATE );
5905
5906 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5907 if( status != PSA_SUCCESS )
5908 return( status );
5909
5910 prf->state = TLS12_PRF_STATE_KEY_SET;
5911
5912 return( PSA_SUCCESS );
5913}
5914
Janos Follath63028dd2019-06-13 09:15:47 +01005915static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5916 const uint8_t *data,
5917 size_t data_length )
5918{
5919 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5920 return( PSA_ERROR_BAD_STATE );
5921
Janos Follathd6dce9f2019-07-04 09:11:38 +01005922 if( data_length != 0 )
5923 {
5924 prf->label = mbedtls_calloc( 1, data_length );
5925 if( prf->label == NULL )
5926 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005927
Janos Follathd6dce9f2019-07-04 09:11:38 +01005928 memcpy( prf->label, data, data_length );
5929 prf->label_length = data_length;
5930 }
Janos Follath63028dd2019-06-13 09:15:47 +01005931
5932 prf->state = TLS12_PRF_STATE_LABEL_SET;
5933
5934 return( PSA_SUCCESS );
5935}
5936
Janos Follathb03233e2019-06-11 15:30:30 +01005937static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5938 psa_algorithm_t hash_alg,
5939 psa_key_derivation_step_t step,
5940 const uint8_t *data,
5941 size_t data_length )
5942{
Janos Follathb03233e2019-06-11 15:30:30 +01005943 switch( step )
5944 {
Janos Follathf08e2652019-06-13 09:05:41 +01005945 case PSA_KEY_DERIVATION_INPUT_SEED:
5946 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005947 case PSA_KEY_DERIVATION_INPUT_SECRET:
5948 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005949 case PSA_KEY_DERIVATION_INPUT_LABEL:
5950 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005951 default:
5952 return( PSA_ERROR_INVALID_ARGUMENT );
5953 }
5954}
John Durkop07cc04a2020-11-16 22:08:34 -08005955#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5956 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5957
5958#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5959static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5960 psa_tls12_prf_key_derivation_t *prf,
5961 psa_algorithm_t hash_alg,
5962 const uint8_t *data,
5963 size_t data_length )
5964{
5965 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005966 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005967 uint8_t *cur = pms;
5968
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005969 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005970 return( PSA_ERROR_INVALID_ARGUMENT );
5971
5972 /* Quoting RFC 4279, Section 2:
5973 *
5974 * The premaster secret is formed as follows: if the PSK is N octets
5975 * long, concatenate a uint16 with the value N, N zero octets, a second
5976 * uint16 with the value N, and the PSK itself.
5977 */
5978
5979 *cur++ = ( data_length >> 8 ) & 0xff;
5980 *cur++ = ( data_length >> 0 ) & 0xff;
5981 memset( cur, 0, data_length );
5982 cur += data_length;
5983 *cur++ = pms[0];
5984 *cur++ = pms[1];
5985 memcpy( cur, data, data_length );
5986 cur += data_length;
5987
5988 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5989
5990 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5991 return( status );
5992}
Janos Follath6660f0e2019-06-17 08:44:03 +01005993
5994static psa_status_t psa_tls12_prf_psk_to_ms_input(
5995 psa_tls12_prf_key_derivation_t *prf,
5996 psa_algorithm_t hash_alg,
5997 psa_key_derivation_step_t step,
5998 const uint8_t *data,
5999 size_t data_length )
6000{
6001 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01006002 {
Janos Follath6660f0e2019-06-17 08:44:03 +01006003 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
6004 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01006005 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006006
6007 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
6008}
John Durkop07cc04a2020-11-16 22:08:34 -08006009#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006010
Gilles Peskineb8965192019-09-24 16:21:10 +02006011/** Check whether the given key type is acceptable for the given
6012 * input step of a key derivation.
6013 *
6014 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6015 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6016 * Both secret and non-secret inputs can alternatively have the type
6017 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6018 * that the input was passed as a buffer rather than via a key object.
6019 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006020static int psa_key_derivation_check_input_type(
6021 psa_key_derivation_step_t step,
6022 psa_key_type_t key_type )
6023{
6024 switch( step )
6025 {
6026 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02006027 if( key_type == PSA_KEY_TYPE_DERIVE )
6028 return( PSA_SUCCESS );
6029 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006030 return( PSA_SUCCESS );
6031 break;
6032 case PSA_KEY_DERIVATION_INPUT_LABEL:
6033 case PSA_KEY_DERIVATION_INPUT_SALT:
6034 case PSA_KEY_DERIVATION_INPUT_INFO:
6035 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02006036 if( key_type == PSA_KEY_TYPE_RAW_DATA )
6037 return( PSA_SUCCESS );
6038 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006039 return( PSA_SUCCESS );
6040 break;
6041 }
6042 return( PSA_ERROR_INVALID_ARGUMENT );
6043}
6044
Janos Follathb80a94e2019-06-12 15:54:46 +01006045static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006046 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006047 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006048 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006049 const uint8_t *data,
6050 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006051{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006052 psa_status_t status;
6053 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6054
6055 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006056 if( status != PSA_SUCCESS )
6057 goto exit;
6058
John Durkopd0321952020-10-29 21:37:36 -07006059#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006060 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006061 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006062 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006063 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006064 step, data, data_length );
6065 }
John Durkop07cc04a2020-11-16 22:08:34 -08006066 else
6067#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6068#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6069 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006070 {
Janos Follathb03233e2019-06-11 15:30:30 +01006071 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6072 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6073 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006074 }
John Durkop07cc04a2020-11-16 22:08:34 -08006075 else
6076#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6077#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6078 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006079 {
6080 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6081 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6082 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006083 }
6084 else
John Durkop07cc04a2020-11-16 22:08:34 -08006085#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006086 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006087 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006088 return( PSA_ERROR_BAD_STATE );
6089 }
6090
Gilles Peskine224b0d62019-09-23 18:13:17 +02006091exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006092 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006093 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006094 return( status );
6095}
6096
Janos Follath51f4a0f2019-06-14 11:35:55 +01006097psa_status_t psa_key_derivation_input_bytes(
6098 psa_key_derivation_operation_t *operation,
6099 psa_key_derivation_step_t step,
6100 const uint8_t *data,
6101 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006102{
Gilles Peskineb8965192019-09-24 16:21:10 +02006103 return( psa_key_derivation_input_internal( operation, step,
6104 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006105 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006106}
6107
Janos Follath51f4a0f2019-06-14 11:35:55 +01006108psa_status_t psa_key_derivation_input_key(
6109 psa_key_derivation_operation_t *operation,
6110 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006111 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006112{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006114 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006115 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006116
Ronald Cron5c522922020-11-14 16:35:34 +01006117 status = psa_get_and_lock_transparent_key_slot_with_policy(
6118 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006119 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006120 {
6121 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006122 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006123 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006124
6125 /* Passing a key object as a SECRET input unlocks the permission
6126 * to output to a key object. */
6127 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6128 operation->can_output_key = 1;
6129
Ronald Cronf95a2b12020-10-22 15:24:49 +02006130 status = psa_key_derivation_input_internal( operation,
6131 step, slot->attr.type,
6132 slot->data.key.data,
6133 slot->data.key.bytes );
6134
Ronald Cron5c522922020-11-14 16:35:34 +01006135 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006136
Ronald Cron5c522922020-11-14 16:35:34 +01006137 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006138}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006139
6140
6141
6142/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006143/* Key agreement */
6144/****************************************************************/
6145
John Durkop6ba40d12020-11-10 08:50:04 -08006146#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006147static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6148 size_t peer_key_length,
6149 const mbedtls_ecp_keypair *our_key,
6150 uint8_t *shared_secret,
6151 size_t shared_secret_size,
6152 size_t *shared_secret_length )
6153{
Steven Cooremand4867872020-08-05 16:31:39 +02006154 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006155 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006156 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006157 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006158 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006159 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006160
Steven Cooreman4fed4552020-08-03 14:46:03 +02006161 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6162 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006163 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006164 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006165 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006166 goto exit;
6167
Jaeden Amero97271b32019-01-10 19:38:51 +00006168 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006169 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006170 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006171 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006172 status = mbedtls_to_psa_error(
6173 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6174 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006175 goto exit;
6176
Jaeden Amero97271b32019-01-10 19:38:51 +00006177 status = mbedtls_to_psa_error(
6178 mbedtls_ecdh_calc_secret( &ecdh,
6179 shared_secret_length,
6180 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006181 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006182 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006183 if( status != PSA_SUCCESS )
6184 goto exit;
6185 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6186 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006187
6188exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006189 if( status != PSA_SUCCESS )
6190 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006191 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006192 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006193 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006194
Jaeden Amero97271b32019-01-10 19:38:51 +00006195 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006196}
John Durkop6ba40d12020-11-10 08:50:04 -08006197#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006198
Gilles Peskine01d718c2018-09-18 12:01:02 +02006199#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6200
Gilles Peskine0216fe12019-04-11 21:23:21 +02006201static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6202 psa_key_slot_t *private_key,
6203 const uint8_t *peer_key,
6204 size_t peer_key_length,
6205 uint8_t *shared_secret,
6206 size_t shared_secret_size,
6207 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006208{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006209 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006210 {
John Durkop6ba40d12020-11-10 08:50:04 -08006211#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006212 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006213 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006214 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006215 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006216 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006217 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006218 private_key->data.key.data,
6219 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006220 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006221 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006222 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006223 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006224 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006225 shared_secret, shared_secret_size,
6226 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006227 mbedtls_ecp_keypair_free( ecp );
6228 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006229 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006230#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006231 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006232 (void) private_key;
6233 (void) peer_key;
6234 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006235 (void) shared_secret;
6236 (void) shared_secret_size;
6237 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006238 return( PSA_ERROR_NOT_SUPPORTED );
6239 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006240}
6241
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006242/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006243 * to potentially free embedded data structures and wipe confidential data.
6244 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006245static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006246 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006247 psa_key_slot_t *private_key,
6248 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006249 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006250{
6251 psa_status_t status;
6252 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6253 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006254 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006255
6256 /* Step 1: run the secret agreement algorithm to generate the shared
6257 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006258 status = psa_key_agreement_raw_internal( ka_alg,
6259 private_key,
6260 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006261 shared_secret,
6262 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006263 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006264 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006265 goto exit;
6266
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006267 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006268 * the shared secret. A shared secret is permitted wherever a key
6269 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006270 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006271 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006272 shared_secret,
6273 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006274exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006275 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006276 return( status );
6277}
6278
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006279psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006280 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006281 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006282 const uint8_t *peer_key,
6283 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006284{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006285 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006286 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006287 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006288
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006289 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006290 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006291 status = psa_get_and_lock_transparent_key_slot_with_policy(
6292 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006293 if( status != PSA_SUCCESS )
6294 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006295 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006296 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006297 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006298 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006299 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006300 else
6301 {
6302 /* If a private key has been added as SECRET, we allow the derived
6303 * key material to be used as a key in PSA Crypto. */
6304 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6305 operation->can_output_key = 1;
6306 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006307
Ronald Cron5c522922020-11-14 16:35:34 +01006308 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006309
Ronald Cron5c522922020-11-14 16:35:34 +01006310 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006311}
6312
Gilles Peskinebe697d82019-05-16 18:00:41 +02006313psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006314 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006315 const uint8_t *peer_key,
6316 size_t peer_key_length,
6317 uint8_t *output,
6318 size_t output_size,
6319 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006320{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006321 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006322 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006323 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006324
6325 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6326 {
6327 status = PSA_ERROR_INVALID_ARGUMENT;
6328 goto exit;
6329 }
Ronald Cron5c522922020-11-14 16:35:34 +01006330 status = psa_get_and_lock_transparent_key_slot_with_policy(
6331 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006332 if( status != PSA_SUCCESS )
6333 goto exit;
6334
6335 status = psa_key_agreement_raw_internal( alg, slot,
6336 peer_key, peer_key_length,
6337 output, output_size,
6338 output_length );
6339
6340exit:
6341 if( status != PSA_SUCCESS )
6342 {
6343 /* If an error happens and is not handled properly, the output
6344 * may be used as a key to protect sensitive data. Arrange for such
6345 * a key to be random, which is likely to result in decryption or
6346 * verification errors. This is better than filling the buffer with
6347 * some constant data such as zeros, which would result in the data
6348 * being protected with a reproducible, easily knowable key.
6349 */
6350 psa_generate_random( output, output_size );
6351 *output_length = output_size;
6352 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006353
Ronald Cron5c522922020-11-14 16:35:34 +01006354 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006355
Ronald Cron5c522922020-11-14 16:35:34 +01006356 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006357}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006358
6359
Gilles Peskine30524eb2020-11-13 17:02:26 +01006360
Gilles Peskine86a440b2018-11-12 18:39:40 +01006361/****************************************************************/
6362/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006363/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006364
Gilles Peskine30524eb2020-11-13 17:02:26 +01006365/** Initialize the PSA random generator.
6366 */
6367static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6368{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006369#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6370 memset( rng, 0, sizeof( *rng ) );
6371#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6372
Gilles Peskine30524eb2020-11-13 17:02:26 +01006373 /* Set default configuration if
6374 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6375 if( rng->entropy_init == NULL )
6376 rng->entropy_init = mbedtls_entropy_init;
6377 if( rng->entropy_free == NULL )
6378 rng->entropy_free = mbedtls_entropy_free;
6379
6380 rng->entropy_init( &rng->entropy );
6381#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6382 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6383 /* The PSA entropy injection feature depends on using NV seed as an entropy
6384 * source. Add NV seed as an entropy source for PSA entropy injection. */
6385 mbedtls_entropy_add_source( &rng->entropy,
6386 mbedtls_nv_seed_poll, NULL,
6387 MBEDTLS_ENTROPY_BLOCK_SIZE,
6388 MBEDTLS_ENTROPY_SOURCE_STRONG );
6389#endif
6390
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006391 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006392#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006393}
6394
6395/** Deinitialize the PSA random generator.
6396 */
6397static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6398{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006399#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6400 memset( rng, 0, sizeof( *rng ) );
6401#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006402 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006403 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006404#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006405}
6406
6407/** Seed the PSA random generator.
6408 */
6409static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6410{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006411#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6412 /* Do nothing: the external RNG seeds itself. */
6413 (void) rng;
6414 return( PSA_SUCCESS );
6415#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006416 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006417 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6418 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006419 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006420#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006421}
6422
Gilles Peskinee59236f2018-01-27 23:32:46 +01006423psa_status_t psa_generate_random( uint8_t *output,
6424 size_t output_size )
6425{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006426 GUARD_MODULE_INITIALIZED;
6427
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006428#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6429
6430 size_t output_length = 0;
6431 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6432 output, output_size,
6433 &output_length );
6434 if( status != PSA_SUCCESS )
6435 return( status );
6436 /* Breaking up a request into smaller chunks is currently not supported
6437 * for the extrernal RNG interface. */
6438 if( output_length != output_size )
6439 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6440 return( PSA_SUCCESS );
6441
6442#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6443
Gilles Peskine71ddab92021-01-04 21:01:07 +01006444 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006445 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006446 size_t request_size =
6447 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6448 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6449 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006450 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6451 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006452 if( ret != 0 )
6453 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006454 output_size -= request_size;
6455 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006456 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006457 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006458#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006459}
6460
Gilles Peskine8814fc42020-12-14 15:33:44 +01006461/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006462 *
6463 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6464 * `psa_generate_random(...)`. The state parameter is ignored since the
6465 * PSA API doesn't support passing an explicit state.
6466 *
6467 * In the non-external case, psa_generate_random() calls an
6468 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6469 * and semantics as mbedtls_psa_get_random(). As an optimization,
6470 * instead of doing this back-and-forth between the PSA API and the
6471 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6472 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006473 */
6474#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6475int mbedtls_psa_get_random( void *p_rng,
6476 unsigned char *output,
6477 size_t output_size )
6478{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006479 /* This function takes a pointer to the RNG state because that's what
6480 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6481 * its own state internally and doesn't let the caller access that state.
6482 * So we just ignore the state parameter, and in practice we'll pass
6483 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006484 (void) p_rng;
6485 psa_status_t status = psa_generate_random( output, output_size );
6486 if( status == PSA_SUCCESS )
6487 return( 0 );
6488 else
6489 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6490}
6491#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6492
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006493#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6494#include "mbedtls/entropy_poll.h"
6495
Gilles Peskine7228da22019-07-15 11:06:15 +02006496psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006497 size_t seed_size )
6498{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006499 if( global_data.initialized )
6500 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006501
6502 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6503 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6504 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6505 return( PSA_ERROR_INVALID_ARGUMENT );
6506
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006507 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006508}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006509#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006510
John Durkop07cc04a2020-11-16 22:08:34 -08006511#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006512static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6513 size_t domain_parameters_size,
6514 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006515{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006516 size_t i;
6517 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006518
Gilles Peskinee56e8782019-04-26 17:34:02 +02006519 if( domain_parameters_size == 0 )
6520 {
6521 *exponent = 65537;
6522 return( PSA_SUCCESS );
6523 }
6524
6525 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6526 * support values that fit in a 32-bit integer, which is larger than
6527 * int on just about every platform anyway. */
6528 if( domain_parameters_size > sizeof( acc ) )
6529 return( PSA_ERROR_NOT_SUPPORTED );
6530 for( i = 0; i < domain_parameters_size; i++ )
6531 acc = ( acc << 8 ) | domain_parameters[i];
6532 if( acc > INT_MAX )
6533 return( PSA_ERROR_NOT_SUPPORTED );
6534 *exponent = acc;
6535 return( PSA_SUCCESS );
6536}
John Durkop07cc04a2020-11-16 22:08:34 -08006537#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006538
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006539static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006540 psa_key_slot_t *slot, size_t bits,
6541 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006542{
Gilles Peskine8e338702019-07-30 20:06:31 +02006543 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006544
Gilles Peskinee56e8782019-04-26 17:34:02 +02006545 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006546 return( PSA_ERROR_INVALID_ARGUMENT );
6547
Gilles Peskinee59236f2018-01-27 23:32:46 +01006548 if( key_type_is_raw_bytes( type ) )
6549 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006550 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006551
6552 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006553 if( status != PSA_SUCCESS )
6554 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006555
6556 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006557 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6558 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006559 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006560
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006561 status = psa_generate_random( slot->data.key.data,
6562 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006563 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006564 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006565
6566 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006567#if defined(MBEDTLS_DES_C)
6568 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006569 psa_des_set_key_parity( slot->data.key.data,
6570 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006571#endif /* MBEDTLS_DES_C */
6572 }
6573 else
6574
John Durkop07cc04a2020-11-16 22:08:34 -08006575#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006576 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006577 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006578 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006580 int exponent;
6581 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006582 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6583 return( PSA_ERROR_NOT_SUPPORTED );
6584 /* Accept only byte-aligned keys, for the same reasons as
6585 * in psa_import_rsa_key(). */
6586 if( bits % 8 != 0 )
6587 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006588 status = psa_read_rsa_exponent( domain_parameters,
6589 domain_parameters_size,
6590 &exponent );
6591 if( status != PSA_SUCCESS )
6592 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006593 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6594 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006595 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006596 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006597 (unsigned int) bits,
6598 exponent );
6599 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006600 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006601
6602 /* Make sure to always have an export representation available */
6603 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6604
Steven Cooreman75b74362020-07-28 14:30:13 +02006605 status = psa_allocate_buffer_to_slot( slot, bytes );
6606 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006607 {
6608 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006609 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006610 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006611
6612 status = psa_export_rsa_key( type,
6613 &rsa,
6614 slot->data.key.data,
6615 bytes,
6616 &slot->data.key.bytes );
6617 mbedtls_rsa_free( &rsa );
6618 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006619 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006620 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006621 }
6622 else
John Durkop07cc04a2020-11-16 22:08:34 -08006623#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006624
John Durkop9814fa22020-11-04 12:28:15 -08006625#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006626 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006627 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006628 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006629 mbedtls_ecp_group_id grp_id =
6630 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006631 const mbedtls_ecp_curve_info *curve_info =
6632 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006633 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006634 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006635 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006636 return( PSA_ERROR_NOT_SUPPORTED );
6637 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6638 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006639 mbedtls_ecp_keypair_init( &ecp );
6640 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006641 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006642 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006643 if( ret != 0 )
6644 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006645 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006646 return( mbedtls_to_psa_error( ret ) );
6647 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006648
6649
6650 /* Make sure to always have an export representation available */
6651 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006652 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6653 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006654 {
6655 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006656 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006657 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006658
6659 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006660 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6661
6662 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006663 if( status != PSA_SUCCESS ) {
6664 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006665 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006666 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006667 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006668 }
6669 else
John Durkop9814fa22020-11-04 12:28:15 -08006670#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006671 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006672 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006673 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006674
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006675 return( PSA_SUCCESS );
6676}
Darryl Green0c6575a2018-11-07 16:05:30 +00006677
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006678psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006679 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006680{
6681 psa_status_t status;
6682 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006683 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006684
Ronald Cron81709fc2020-11-14 12:10:32 +01006685 *key = MBEDTLS_SVC_KEY_ID_INIT;
6686
Gilles Peskine0f84d622019-09-12 19:03:13 +02006687 /* Reject any attempt to create a zero-length key so that we don't
6688 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6689 if( psa_get_key_bits( attributes ) == 0 )
6690 return( PSA_ERROR_INVALID_ARGUMENT );
6691
Ronald Cron81709fc2020-11-14 12:10:32 +01006692 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6693 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006694 if( status != PSA_SUCCESS )
6695 goto exit;
6696
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006697 status = psa_driver_wrapper_generate_key( attributes,
6698 slot );
6699 if( status != PSA_ERROR_NOT_SUPPORTED ||
6700 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6701 goto exit;
6702
6703 status = psa_generate_key_internal(
6704 slot, attributes->core.bits,
6705 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006706
6707exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006708 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006709 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006710 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006711 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006712
Darryl Green0c6575a2018-11-07 16:05:30 +00006713 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006714}
6715
6716
Gilles Peskinee59236f2018-01-27 23:32:46 +01006717
6718/****************************************************************/
6719/* Module setup */
6720/****************************************************************/
6721
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006722#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006723psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6724 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6725 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6726{
6727 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6728 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006729 global_data.rng.entropy_init = entropy_init;
6730 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006731 return( PSA_SUCCESS );
6732}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006733#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006734
Gilles Peskinee59236f2018-01-27 23:32:46 +01006735void mbedtls_psa_crypto_free( void )
6736{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006737 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006738 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6739 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006740 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006741 }
6742 /* Wipe all remaining data, including configuration.
6743 * In particular, this sets all state indicator to the value
6744 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006745 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006746#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006747 /* Unregister all secure element drivers, so that we restart from
6748 * a pristine state. */
6749 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006750#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006751}
6752
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006753#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6754/** Recover a transaction that was interrupted by a power failure.
6755 *
6756 * This function is called during initialization, before psa_crypto_init()
6757 * returns. If this function returns a failure status, the initialization
6758 * fails.
6759 */
6760static psa_status_t psa_crypto_recover_transaction(
6761 const psa_crypto_transaction_t *transaction )
6762{
6763 switch( transaction->unknown.type )
6764 {
6765 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6766 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006767 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006768 * is implemented.
6769 * https://github.com/ARMmbed/mbed-crypto/issues/218
6770 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006771 default:
6772 /* We found an unsupported transaction in the storage.
6773 * We don't know what state the storage is in. Give up. */
6774 return( PSA_ERROR_STORAGE_FAILURE );
6775 }
6776}
6777#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6778
Gilles Peskinee59236f2018-01-27 23:32:46 +01006779psa_status_t psa_crypto_init( void )
6780{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006781 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006782
Gilles Peskinec6b69072018-11-20 21:42:52 +01006783 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006784 if( global_data.initialized != 0 )
6785 return( PSA_SUCCESS );
6786
Gilles Peskine30524eb2020-11-13 17:02:26 +01006787 /* Initialize and seed the random generator. */
6788 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006789 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006790 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006791 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006792 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006793 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006794
Gilles Peskine66fb1262018-12-10 16:29:04 +01006795 status = psa_initialize_key_slots( );
6796 if( status != PSA_SUCCESS )
6797 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006798
Gilles Peskined9348f22019-10-01 15:22:29 +02006799#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6800 status = psa_init_all_se_drivers( );
6801 if( status != PSA_SUCCESS )
6802 goto exit;
6803#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6804
Gilles Peskine4b734222019-07-24 15:56:31 +02006805#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006806 status = psa_crypto_load_transaction( );
6807 if( status == PSA_SUCCESS )
6808 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006809 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6810 if( status != PSA_SUCCESS )
6811 goto exit;
6812 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006813 }
6814 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6815 {
6816 /* There's no transaction to complete. It's all good. */
6817 status = PSA_SUCCESS;
6818 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006819#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006820
Gilles Peskinec6b69072018-11-20 21:42:52 +01006821 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006822 global_data.initialized = 1;
6823
6824exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006825 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006826 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006827 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006828}
6829
6830#endif /* MBEDTLS_PSA_CRYPTO_C */