blob: 2213657ff58f6f8ab83be29ff22780d272ecea71 [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"
Chris Jonese2191cd2021-02-19 16:04:15 +000076#include "md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077#include "mbedtls/pk.h"
Chris Jonese2191cd2021-02-19 16:04:15 +000078#include "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 */
2361 {
2362 status = psa_import_key_into_slot( slot, data, data_length );
2363 if( status != PSA_SUCCESS )
2364 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002365 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002366 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002367 if( status != PSA_SUCCESS )
2368 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002369
Ronald Cron81709fc2020-11-14 12:10:32 +01002370 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002371exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002372 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002373 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002374
Gilles Peskine4747d192019-04-17 15:05:45 +02002375 return( status );
2376}
2377
Gilles Peskined7729582019-08-05 15:55:54 +02002378#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2379psa_status_t mbedtls_psa_register_se_key(
2380 const psa_key_attributes_t *attributes )
2381{
2382 psa_status_t status;
2383 psa_key_slot_t *slot = NULL;
2384 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002385 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002386
2387 /* Leaving attributes unspecified is not currently supported.
2388 * It could make sense to query the key type and size from the
2389 * secure element, but not all secure elements support this
2390 * and the driver HAL doesn't currently support it. */
2391 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2392 return( PSA_ERROR_NOT_SUPPORTED );
2393 if( psa_get_key_bits( attributes ) == 0 )
2394 return( PSA_ERROR_NOT_SUPPORTED );
2395
2396 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002397 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002398 if( status != PSA_SUCCESS )
2399 goto exit;
2400
Ronald Cron81709fc2020-11-14 12:10:32 +01002401 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002402
2403exit:
2404 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002405 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002406
Gilles Peskined7729582019-08-05 15:55:54 +02002407 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002408 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002409 return( status );
2410}
2411#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2412
Gilles Peskinef603c712019-01-19 13:40:11 +01002413static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002414 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002415{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002416 psa_status_t status = psa_copy_key_material_into_slot( target,
2417 source->data.key.data,
2418 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002419 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002420 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002421
Steven Cooreman398aee52020-10-13 14:35:45 +02002422 target->attr.type = source->attr.type;
2423 target->attr.bits = source->attr.bits;
2424
2425 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002426}
2427
Ronald Croncf56a0a2020-08-04 09:51:30 +02002428psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002429 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002430 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002431{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002433 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002434 psa_key_slot_t *source_slot = NULL;
2435 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002436 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002437 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002438
Ronald Cron81709fc2020-11-14 12:10:32 +01002439 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2440
Ronald Cron5c522922020-11-14 16:35:34 +01002441 status = psa_get_and_lock_transparent_key_slot_with_policy(
2442 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002443 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002444 goto exit;
2445
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002446 status = psa_validate_optional_attributes( source_slot,
2447 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002448 if( status != PSA_SUCCESS )
2449 goto exit;
2450
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002451 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002452 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002453 if( status != PSA_SUCCESS )
2454 goto exit;
2455
Ronald Cron81709fc2020-11-14 12:10:32 +01002456 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2457 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002458 if( status != PSA_SUCCESS )
2459 goto exit;
2460
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002461#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2462 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002463 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002464 /* Copying to a secure element is not implemented yet. */
2465 status = PSA_ERROR_NOT_SUPPORTED;
2466 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002467 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002468#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002469
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002470 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002471 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002472 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002473
Ronald Cron81709fc2020-11-14 12:10:32 +01002474 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002475exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002476 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002477 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002478
Ronald Cron5c522922020-11-14 16:35:34 +01002479 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002480
Ronald Cron5c522922020-11-14 16:35:34 +01002481 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002482}
2483
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002484
2485
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002486/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002487/* Message digests */
2488/****************************************************************/
2489
John Durkop07cc04a2020-11-16 22:08:34 -08002490#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002491 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2492 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002493 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002494static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002495{
2496 switch( alg )
2497 {
John Durkopee4e6602020-11-27 08:48:46 -08002498#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002499 case PSA_ALG_MD2:
2500 return( &mbedtls_md2_info );
2501#endif
John Durkopee4e6602020-11-27 08:48:46 -08002502#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002503 case PSA_ALG_MD4:
2504 return( &mbedtls_md4_info );
2505#endif
John Durkopee4e6602020-11-27 08:48:46 -08002506#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002507 case PSA_ALG_MD5:
2508 return( &mbedtls_md5_info );
2509#endif
John Durkopee4e6602020-11-27 08:48:46 -08002510#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002511 case PSA_ALG_RIPEMD160:
2512 return( &mbedtls_ripemd160_info );
2513#endif
John Durkopee4e6602020-11-27 08:48:46 -08002514#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002515 case PSA_ALG_SHA_1:
2516 return( &mbedtls_sha1_info );
2517#endif
John Durkopee4e6602020-11-27 08:48:46 -08002518#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002519 case PSA_ALG_SHA_224:
2520 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002521#endif
2522#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002523 case PSA_ALG_SHA_256:
2524 return( &mbedtls_sha256_info );
2525#endif
John Durkopee4e6602020-11-27 08:48:46 -08002526#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002527 case PSA_ALG_SHA_384:
2528 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002529#endif
John Durkopee4e6602020-11-27 08:48:46 -08002530#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002531 case PSA_ALG_SHA_512:
2532 return( &mbedtls_sha512_info );
2533#endif
2534 default:
2535 return( NULL );
2536 }
2537}
John Durkop07cc04a2020-11-16 22:08:34 -08002538#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002539 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2540 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2541 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002542
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002543psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2544{
2545 switch( operation->alg )
2546 {
Gilles Peskine81736312018-06-26 15:04:31 +02002547 case 0:
2548 /* The object has (apparently) been initialized but it is not
2549 * in use. It's ok to call abort on such an object, and there's
2550 * nothing to do. */
2551 break;
John Durkopee4e6602020-11-27 08:48:46 -08002552#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002553 case PSA_ALG_MD2:
2554 mbedtls_md2_free( &operation->ctx.md2 );
2555 break;
2556#endif
John Durkopee4e6602020-11-27 08:48:46 -08002557#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002558 case PSA_ALG_MD4:
2559 mbedtls_md4_free( &operation->ctx.md4 );
2560 break;
2561#endif
John Durkopee4e6602020-11-27 08:48:46 -08002562#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002563 case PSA_ALG_MD5:
2564 mbedtls_md5_free( &operation->ctx.md5 );
2565 break;
2566#endif
John Durkopee4e6602020-11-27 08:48:46 -08002567#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002568 case PSA_ALG_RIPEMD160:
2569 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2570 break;
2571#endif
John Durkopee4e6602020-11-27 08:48:46 -08002572#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 case PSA_ALG_SHA_1:
2574 mbedtls_sha1_free( &operation->ctx.sha1 );
2575 break;
2576#endif
John Durkop6ca23272020-12-03 06:01:32 -08002577#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002578 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002579 mbedtls_sha256_free( &operation->ctx.sha256 );
2580 break;
2581#endif
2582#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002583 case PSA_ALG_SHA_256:
2584 mbedtls_sha256_free( &operation->ctx.sha256 );
2585 break;
2586#endif
John Durkop6ca23272020-12-03 06:01:32 -08002587#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002588 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002589 mbedtls_sha512_free( &operation->ctx.sha512 );
2590 break;
2591#endif
2592#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002593 case PSA_ALG_SHA_512:
2594 mbedtls_sha512_free( &operation->ctx.sha512 );
2595 break;
2596#endif
2597 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002598 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002599 }
2600 operation->alg = 0;
2601 return( PSA_SUCCESS );
2602}
2603
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002604psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002605 psa_algorithm_t alg )
2606{
Janos Follath24eed8d2019-11-22 13:21:35 +00002607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002608
2609 /* A context must be freshly initialized before it can be set up. */
2610 if( operation->alg != 0 )
2611 {
2612 return( PSA_ERROR_BAD_STATE );
2613 }
2614
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002615 switch( alg )
2616 {
John Durkopee4e6602020-11-27 08:48:46 -08002617#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002618 case PSA_ALG_MD2:
2619 mbedtls_md2_init( &operation->ctx.md2 );
2620 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2621 break;
2622#endif
John Durkopee4e6602020-11-27 08:48:46 -08002623#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002624 case PSA_ALG_MD4:
2625 mbedtls_md4_init( &operation->ctx.md4 );
2626 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2627 break;
2628#endif
John Durkopee4e6602020-11-27 08:48:46 -08002629#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002630 case PSA_ALG_MD5:
2631 mbedtls_md5_init( &operation->ctx.md5 );
2632 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2633 break;
2634#endif
John Durkopee4e6602020-11-27 08:48:46 -08002635#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002636 case PSA_ALG_RIPEMD160:
2637 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2638 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2639 break;
2640#endif
John Durkopee4e6602020-11-27 08:48:46 -08002641#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002642 case PSA_ALG_SHA_1:
2643 mbedtls_sha1_init( &operation->ctx.sha1 );
2644 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2645 break;
2646#endif
John Durkopee4e6602020-11-27 08:48:46 -08002647#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002648 case PSA_ALG_SHA_224:
2649 mbedtls_sha256_init( &operation->ctx.sha256 );
2650 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2651 break;
John Durkopee4e6602020-11-27 08:48:46 -08002652#endif
2653#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002654 case PSA_ALG_SHA_256:
2655 mbedtls_sha256_init( &operation->ctx.sha256 );
2656 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2657 break;
2658#endif
John Durkopee4e6602020-11-27 08:48:46 -08002659#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002660 case PSA_ALG_SHA_384:
2661 mbedtls_sha512_init( &operation->ctx.sha512 );
2662 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2663 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002664#endif
John Durkopee4e6602020-11-27 08:48:46 -08002665#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002666 case PSA_ALG_SHA_512:
2667 mbedtls_sha512_init( &operation->ctx.sha512 );
2668 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2669 break;
2670#endif
2671 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002672 return( PSA_ALG_IS_HASH( alg ) ?
2673 PSA_ERROR_NOT_SUPPORTED :
2674 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002675 }
2676 if( ret == 0 )
2677 operation->alg = alg;
2678 else
2679 psa_hash_abort( operation );
2680 return( mbedtls_to_psa_error( ret ) );
2681}
2682
2683psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2684 const uint8_t *input,
2685 size_t input_length )
2686{
Janos Follath24eed8d2019-11-22 13:21:35 +00002687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002688
2689 /* Don't require hash implementations to behave correctly on a
2690 * zero-length input, which may have an invalid pointer. */
2691 if( input_length == 0 )
2692 return( PSA_SUCCESS );
2693
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002694 switch( operation->alg )
2695 {
John Durkopee4e6602020-11-27 08:48:46 -08002696#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002697 case PSA_ALG_MD2:
2698 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2699 input, input_length );
2700 break;
2701#endif
John Durkopee4e6602020-11-27 08:48:46 -08002702#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002703 case PSA_ALG_MD4:
2704 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2705 input, input_length );
2706 break;
2707#endif
John Durkopee4e6602020-11-27 08:48:46 -08002708#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002709 case PSA_ALG_MD5:
2710 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2711 input, input_length );
2712 break;
2713#endif
John Durkopee4e6602020-11-27 08:48:46 -08002714#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002715 case PSA_ALG_RIPEMD160:
2716 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2717 input, input_length );
2718 break;
2719#endif
John Durkopee4e6602020-11-27 08:48:46 -08002720#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002721 case PSA_ALG_SHA_1:
2722 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2723 input, input_length );
2724 break;
2725#endif
John Durkop6ca23272020-12-03 06:01:32 -08002726#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002727 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002728 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2729 input, input_length );
2730 break;
2731#endif
2732#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002733 case PSA_ALG_SHA_256:
2734 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2735 input, input_length );
2736 break;
2737#endif
John Durkop6ca23272020-12-03 06:01:32 -08002738#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002739 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002740 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2741 input, input_length );
2742 break;
2743#endif
2744#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002745 case PSA_ALG_SHA_512:
2746 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2747 input, input_length );
2748 break;
2749#endif
2750 default:
John Durkopee4e6602020-11-27 08:48:46 -08002751 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002752 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002753 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002754
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002755 if( ret != 0 )
2756 psa_hash_abort( operation );
2757 return( mbedtls_to_psa_error( ret ) );
2758}
2759
2760psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2761 uint8_t *hash,
2762 size_t hash_size,
2763 size_t *hash_length )
2764{
itayzafrir40835d42018-08-02 13:14:17 +03002765 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002767 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002768
2769 /* Fill the output buffer with something that isn't a valid hash
2770 * (barring an attack on the hash and deliberately-crafted input),
2771 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002772 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002773 /* If hash_size is 0 then hash may be NULL and then the
2774 * call to memset would have undefined behavior. */
2775 if( hash_size != 0 )
2776 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002777
2778 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002779 {
2780 status = PSA_ERROR_BUFFER_TOO_SMALL;
2781 goto exit;
2782 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002783
2784 switch( operation->alg )
2785 {
John Durkopee4e6602020-11-27 08:48:46 -08002786#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002787 case PSA_ALG_MD2:
2788 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2789 break;
2790#endif
John Durkopee4e6602020-11-27 08:48:46 -08002791#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002792 case PSA_ALG_MD4:
2793 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2794 break;
2795#endif
John Durkopee4e6602020-11-27 08:48:46 -08002796#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002797 case PSA_ALG_MD5:
2798 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2799 break;
2800#endif
John Durkopee4e6602020-11-27 08:48:46 -08002801#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002802 case PSA_ALG_RIPEMD160:
2803 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2804 break;
2805#endif
John Durkopee4e6602020-11-27 08:48:46 -08002806#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002807 case PSA_ALG_SHA_1:
2808 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2809 break;
2810#endif
John Durkop6ca23272020-12-03 06:01:32 -08002811#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002812 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002813 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2814 break;
2815#endif
2816#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002817 case PSA_ALG_SHA_256:
2818 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2819 break;
2820#endif
John Durkop6ca23272020-12-03 06:01:32 -08002821#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002822 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002823 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2824 break;
2825#endif
2826#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002827 case PSA_ALG_SHA_512:
2828 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2829 break;
2830#endif
2831 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002832 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002833 }
itayzafrir40835d42018-08-02 13:14:17 +03002834 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002835
itayzafrir40835d42018-08-02 13:14:17 +03002836exit:
2837 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002838 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002839 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002840 return( psa_hash_abort( operation ) );
2841 }
2842 else
2843 {
2844 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002845 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002846 }
2847}
2848
Gilles Peskine2d277862018-06-18 15:41:12 +02002849psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2850 const uint8_t *hash,
2851 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002852{
2853 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2854 size_t actual_hash_length;
2855 psa_status_t status = psa_hash_finish( operation,
2856 actual_hash, sizeof( actual_hash ),
2857 &actual_hash_length );
2858 if( status != PSA_SUCCESS )
2859 return( status );
2860 if( actual_hash_length != hash_length )
2861 return( PSA_ERROR_INVALID_SIGNATURE );
2862 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2863 return( PSA_ERROR_INVALID_SIGNATURE );
2864 return( PSA_SUCCESS );
2865}
2866
Gilles Peskine0a749c82019-11-28 19:33:58 +01002867psa_status_t psa_hash_compute( psa_algorithm_t alg,
2868 const uint8_t *input, size_t input_length,
2869 uint8_t *hash, size_t hash_size,
2870 size_t *hash_length )
2871{
2872 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2874
2875 *hash_length = hash_size;
2876 status = psa_hash_setup( &operation, alg );
2877 if( status != PSA_SUCCESS )
2878 goto exit;
2879 status = psa_hash_update( &operation, input, input_length );
2880 if( status != PSA_SUCCESS )
2881 goto exit;
2882 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2883 if( status != PSA_SUCCESS )
2884 goto exit;
2885
2886exit:
2887 if( status == PSA_SUCCESS )
2888 status = psa_hash_abort( &operation );
2889 else
2890 psa_hash_abort( &operation );
2891 return( status );
2892}
2893
2894psa_status_t psa_hash_compare( psa_algorithm_t alg,
2895 const uint8_t *input, size_t input_length,
2896 const uint8_t *hash, size_t hash_length )
2897{
2898 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2900
2901 status = psa_hash_setup( &operation, alg );
2902 if( status != PSA_SUCCESS )
2903 goto exit;
2904 status = psa_hash_update( &operation, input, input_length );
2905 if( status != PSA_SUCCESS )
2906 goto exit;
2907 status = psa_hash_verify( &operation, hash, hash_length );
2908 if( status != PSA_SUCCESS )
2909 goto exit;
2910
2911exit:
2912 if( status == PSA_SUCCESS )
2913 status = psa_hash_abort( &operation );
2914 else
2915 psa_hash_abort( &operation );
2916 return( status );
2917}
2918
Gilles Peskineeb35d782019-01-22 17:56:16 +01002919psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2920 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002921{
2922 if( target_operation->alg != 0 )
2923 return( PSA_ERROR_BAD_STATE );
2924
2925 switch( source_operation->alg )
2926 {
2927 case 0:
2928 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002929#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002930 case PSA_ALG_MD2:
2931 mbedtls_md2_clone( &target_operation->ctx.md2,
2932 &source_operation->ctx.md2 );
2933 break;
2934#endif
John Durkopee4e6602020-11-27 08:48:46 -08002935#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002936 case PSA_ALG_MD4:
2937 mbedtls_md4_clone( &target_operation->ctx.md4,
2938 &source_operation->ctx.md4 );
2939 break;
2940#endif
John Durkopee4e6602020-11-27 08:48:46 -08002941#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002942 case PSA_ALG_MD5:
2943 mbedtls_md5_clone( &target_operation->ctx.md5,
2944 &source_operation->ctx.md5 );
2945 break;
2946#endif
John Durkopee4e6602020-11-27 08:48:46 -08002947#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002948 case PSA_ALG_RIPEMD160:
2949 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2950 &source_operation->ctx.ripemd160 );
2951 break;
2952#endif
John Durkopee4e6602020-11-27 08:48:46 -08002953#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002954 case PSA_ALG_SHA_1:
2955 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2956 &source_operation->ctx.sha1 );
2957 break;
2958#endif
John Durkop6ca23272020-12-03 06:01:32 -08002959#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002960 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002961 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2962 &source_operation->ctx.sha256 );
2963 break;
2964#endif
2965#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002966 case PSA_ALG_SHA_256:
2967 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2968 &source_operation->ctx.sha256 );
2969 break;
2970#endif
John Durkop6ca23272020-12-03 06:01:32 -08002971#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002972 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002973 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2974 &source_operation->ctx.sha512 );
2975 break;
2976#endif
2977#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002978 case PSA_ALG_SHA_512:
2979 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2980 &source_operation->ctx.sha512 );
2981 break;
2982#endif
2983 default:
2984 return( PSA_ERROR_NOT_SUPPORTED );
2985 }
2986
2987 target_operation->alg = source_operation->alg;
2988 return( PSA_SUCCESS );
2989}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002990
2991
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002992/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002993/* MAC */
2994/****************************************************************/
2995
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002996static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997 psa_algorithm_t alg,
2998 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002999 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03003000 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003001{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03003003 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003004
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003005 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003006 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003007
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
3009 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03003010 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01003012 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013 mode = MBEDTLS_MODE_STREAM;
3014 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015 case PSA_ALG_CTR:
3016 mode = MBEDTLS_MODE_CTR;
3017 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003018 case PSA_ALG_CFB:
3019 mode = MBEDTLS_MODE_CFB;
3020 break;
3021 case PSA_ALG_OFB:
3022 mode = MBEDTLS_MODE_OFB;
3023 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003024 case PSA_ALG_ECB_NO_PADDING:
3025 mode = MBEDTLS_MODE_ECB;
3026 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003027 case PSA_ALG_CBC_NO_PADDING:
3028 mode = MBEDTLS_MODE_CBC;
3029 break;
3030 case PSA_ALG_CBC_PKCS7:
3031 mode = MBEDTLS_MODE_CBC;
3032 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003033 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034 mode = MBEDTLS_MODE_CCM;
3035 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003036 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003037 mode = MBEDTLS_MODE_GCM;
3038 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003039 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3040 mode = MBEDTLS_MODE_CHACHAPOLY;
3041 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042 default:
3043 return( NULL );
3044 }
3045 }
3046 else if( alg == PSA_ALG_CMAC )
3047 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003048 else
3049 return( NULL );
3050
3051 switch( key_type )
3052 {
3053 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003054 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003055 break;
3056 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003057 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3058 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003059 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003060 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003061 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003062 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003063 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3064 * but two-key Triple-DES is functionally three-key Triple-DES
3065 * with K1=K3, so that's how we present it to mbedtls. */
3066 if( key_bits == 128 )
3067 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068 break;
3069 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003070 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003071 break;
3072 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003073 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003074 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003075 case PSA_KEY_TYPE_CHACHA20:
3076 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3077 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003078 default:
3079 return( NULL );
3080 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003081 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003082 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003083
Jaeden Amero23bbb752018-06-26 14:16:54 +01003084 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3085 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003086}
3087
John Durkop6ba40d12020-11-10 08:50:04 -08003088#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003089static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003090{
Gilles Peskine2d277862018-06-18 15:41:12 +02003091 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003092 {
3093 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003094 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003095 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003096 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003097 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003098 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003099 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003100 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003101 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003102 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003103 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003104 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003105 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003106 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003107 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003108 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003109 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003110 return( 128 );
3111 default:
3112 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003113 }
3114}
John Durkop07cc04a2020-11-16 22:08:34 -08003115#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003116
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003117/* Initialize the MAC operation structure. Once this function has been
3118 * called, psa_mac_abort can run and will do the right thing. */
3119static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3120 psa_algorithm_t alg )
3121{
3122 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3123
3124 operation->alg = alg;
3125 operation->key_set = 0;
3126 operation->iv_set = 0;
3127 operation->iv_required = 0;
3128 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003129 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003130
3131#if defined(MBEDTLS_CMAC_C)
3132 if( alg == PSA_ALG_CMAC )
3133 {
3134 operation->iv_required = 0;
3135 mbedtls_cipher_init( &operation->ctx.cmac );
3136 status = PSA_SUCCESS;
3137 }
3138 else
3139#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003140#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003141 if( PSA_ALG_IS_HMAC( operation->alg ) )
3142 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003143 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3144 operation->ctx.hmac.hash_ctx.alg = 0;
3145 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003146 }
3147 else
John Durkopd0321952020-10-29 21:37:36 -07003148#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003149 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003150 if( ! PSA_ALG_IS_MAC( alg ) )
3151 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003152 }
3153
3154 if( status != PSA_SUCCESS )
3155 memset( operation, 0, sizeof( *operation ) );
3156 return( status );
3157}
3158
John Durkop6ba40d12020-11-10 08:50:04 -08003159#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003160static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3161{
Gilles Peskine3f108122018-12-07 18:14:53 +01003162 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003163 return( psa_hash_abort( &hmac->hash_ctx ) );
3164}
John Durkop6ba40d12020-11-10 08:50:04 -08003165#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003166
Gilles Peskine8c9def32018-02-08 10:02:12 +01003167psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3168{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003169 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003170 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003171 /* The object has (apparently) been initialized but it is not
3172 * in use. It's ok to call abort on such an object, and there's
3173 * nothing to do. */
3174 return( PSA_SUCCESS );
3175 }
3176 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003177#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003178 if( operation->alg == PSA_ALG_CMAC )
3179 {
3180 mbedtls_cipher_free( &operation->ctx.cmac );
3181 }
3182 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003183#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003184#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003185 if( PSA_ALG_IS_HMAC( operation->alg ) )
3186 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003187 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003188 }
3189 else
John Durkopd0321952020-10-29 21:37:36 -07003190#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003191 {
3192 /* Sanity check (shouldn't happen: operation->alg should
3193 * always have been initialized to a valid value). */
3194 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003195 }
Moran Peker41deec42018-04-04 15:43:05 +03003196
Gilles Peskine8c9def32018-02-08 10:02:12 +01003197 operation->alg = 0;
3198 operation->key_set = 0;
3199 operation->iv_set = 0;
3200 operation->iv_required = 0;
3201 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003202 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003203
Gilles Peskine8c9def32018-02-08 10:02:12 +01003204 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003205
3206bad_state:
3207 /* If abort is called on an uninitialized object, we can't trust
3208 * anything. Wipe the object in case it contains confidential data.
3209 * This may result in a memory leak if a pointer gets overwritten,
3210 * but it's too late to do anything about this. */
3211 memset( operation, 0, sizeof( *operation ) );
3212 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003213}
3214
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003215#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003216static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003217 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003218 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003219 const mbedtls_cipher_info_t *cipher_info )
3220{
Janos Follath24eed8d2019-11-22 13:21:35 +00003221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003222
3223 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003224
3225 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3226 if( ret != 0 )
3227 return( ret );
3228
3229 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003230 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003231 key_bits );
3232 return( ret );
3233}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003234#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003235
John Durkop6ba40d12020-11-10 08:50:04 -08003236#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003237static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3238 const uint8_t *key,
3239 size_t key_length,
3240 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003241{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003242 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003243 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003244 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003245 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003246 psa_status_t status;
3247
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003248 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3249 * overflow below. This should never trigger if the hash algorithm
3250 * is implemented correctly. */
3251 /* The size checks against the ipad and opad buffers cannot be written
3252 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3253 * because that triggers -Wlogical-op on GCC 7.3. */
3254 if( block_size > sizeof( ipad ) )
3255 return( PSA_ERROR_NOT_SUPPORTED );
3256 if( block_size > sizeof( hmac->opad ) )
3257 return( PSA_ERROR_NOT_SUPPORTED );
3258 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003259 return( PSA_ERROR_NOT_SUPPORTED );
3260
Gilles Peskined223b522018-06-11 18:12:58 +02003261 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003262 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003263 status = psa_hash_compute( hash_alg, key, key_length,
3264 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003265 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003266 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003267 }
Gilles Peskine96889972018-07-12 17:07:03 +02003268 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3269 * but it is permitted. It is common when HMAC is used in HKDF, for
3270 * example. Don't call `memcpy` in the 0-length because `key` could be
3271 * an invalid pointer which would make the behavior undefined. */
3272 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003273 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003274
Gilles Peskined223b522018-06-11 18:12:58 +02003275 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3276 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003277 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003278 ipad[i] ^= 0x36;
3279 memset( ipad + key_length, 0x36, block_size - key_length );
3280
3281 /* Copy the key material from ipad to opad, flipping the requisite bits,
3282 * and filling the rest of opad with the requisite constant. */
3283 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003284 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3285 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003286
Gilles Peskine01126fa2018-07-12 17:04:55 +02003287 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003288 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003289 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003290
Gilles Peskine01126fa2018-07-12 17:04:55 +02003291 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003292
3293cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003294 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003295
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003296 return( status );
3297}
John Durkop6ba40d12020-11-10 08:50:04 -08003298#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003299
Gilles Peskine89167cb2018-07-08 20:12:23 +02003300static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003301 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003302 psa_algorithm_t alg,
3303 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003304{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003305 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003306 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003307 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003308 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003309 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003310 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003311 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003312 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003313
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003314 /* A context must be freshly initialized before it can be set up. */
3315 if( operation->alg != 0 )
3316 {
3317 return( PSA_ERROR_BAD_STATE );
3318 }
3319
Gilles Peskined911eb72018-08-14 15:18:45 +02003320 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003321 if( status != PSA_SUCCESS )
3322 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003323 if( is_sign )
3324 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003325
Ronald Cron5c522922020-11-14 16:35:34 +01003326 status = psa_get_and_lock_transparent_key_slot_with_policy(
3327 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003328 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003329 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003330 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003331
Gilles Peskine8c9def32018-02-08 10:02:12 +01003332#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003333 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003334 {
3335 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003336 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003337 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003339 if( cipher_info == NULL )
3340 {
3341 status = PSA_ERROR_NOT_SUPPORTED;
3342 goto exit;
3343 }
3344 operation->mac_size = cipher_info->block_size;
3345 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3346 status = mbedtls_to_psa_error( ret );
3347 }
3348 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003349#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003350#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003351 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003352 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003353 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003354 if( hash_alg == 0 )
3355 {
3356 status = PSA_ERROR_NOT_SUPPORTED;
3357 goto exit;
3358 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003359
3360 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3361 /* Sanity check. This shouldn't fail on a valid configuration. */
3362 if( operation->mac_size == 0 ||
3363 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3364 {
3365 status = PSA_ERROR_NOT_SUPPORTED;
3366 goto exit;
3367 }
3368
Gilles Peskine8e338702019-07-30 20:06:31 +02003369 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003370 {
3371 status = PSA_ERROR_INVALID_ARGUMENT;
3372 goto exit;
3373 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003374
Gilles Peskine01126fa2018-07-12 17:04:55 +02003375 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003376 slot->data.key.data,
3377 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003378 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003379 }
3380 else
John Durkopd0321952020-10-29 21:37:36 -07003381#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003382 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003383 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003384 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003385 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003386
Gilles Peskined911eb72018-08-14 15:18:45 +02003387 if( truncated == 0 )
3388 {
3389 /* The "normal" case: untruncated algorithm. Nothing to do. */
3390 }
3391 else if( truncated < 4 )
3392 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003393 /* A very short MAC is too short for security since it can be
3394 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3395 * so we make this our minimum, even though 32 bits is still
3396 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003397 status = PSA_ERROR_NOT_SUPPORTED;
3398 }
3399 else if( truncated > operation->mac_size )
3400 {
3401 /* It's impossible to "truncate" to a larger length. */
3402 status = PSA_ERROR_INVALID_ARGUMENT;
3403 }
3404 else
3405 operation->mac_size = truncated;
3406
Gilles Peskinefbfac682018-07-08 20:51:54 +02003407exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003408 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003409 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003410 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003411 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003412 else
3413 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003414 operation->key_set = 1;
3415 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003416
Ronald Cron5c522922020-11-14 16:35:34 +01003417 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003418
Ronald Cron5c522922020-11-14 16:35:34 +01003419 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003420}
3421
Gilles Peskine89167cb2018-07-08 20:12:23 +02003422psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003423 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003424 psa_algorithm_t alg )
3425{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003426 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003427}
3428
3429psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003430 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003431 psa_algorithm_t alg )
3432{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003433 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003434}
3435
Gilles Peskine8c9def32018-02-08 10:02:12 +01003436psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3437 const uint8_t *input,
3438 size_t input_length )
3439{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003440 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003441 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003442 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003443 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003444 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003445 operation->has_input = 1;
3446
Gilles Peskine8c9def32018-02-08 10:02:12 +01003447#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003448 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003449 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003450 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3451 input, input_length );
3452 status = mbedtls_to_psa_error( ret );
3453 }
3454 else
3455#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003456#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003457 if( PSA_ALG_IS_HMAC( operation->alg ) )
3458 {
3459 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3460 input_length );
3461 }
3462 else
John Durkopd0321952020-10-29 21:37:36 -07003463#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003464 {
3465 /* This shouldn't happen if `operation` was initialized by
3466 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003467 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003468 }
3469
Gilles Peskinefbfac682018-07-08 20:51:54 +02003470 if( status != PSA_SUCCESS )
3471 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003472 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003473}
3474
John Durkop6ba40d12020-11-10 08:50:04 -08003475#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003476static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3477 uint8_t *mac,
3478 size_t mac_size )
3479{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003480 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003481 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3482 size_t hash_size = 0;
3483 size_t block_size = psa_get_hash_block_size( hash_alg );
3484 psa_status_t status;
3485
Gilles Peskine01126fa2018-07-12 17:04:55 +02003486 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3487 if( status != PSA_SUCCESS )
3488 return( status );
3489 /* From here on, tmp needs to be wiped. */
3490
3491 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3492 if( status != PSA_SUCCESS )
3493 goto exit;
3494
3495 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3496 if( status != PSA_SUCCESS )
3497 goto exit;
3498
3499 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3500 if( status != PSA_SUCCESS )
3501 goto exit;
3502
Gilles Peskined911eb72018-08-14 15:18:45 +02003503 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3504 if( status != PSA_SUCCESS )
3505 goto exit;
3506
3507 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003508
3509exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003510 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003511 return( status );
3512}
John Durkop6ba40d12020-11-10 08:50:04 -08003513#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003514
mohammad16036df908f2018-04-02 08:34:15 -07003515static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003516 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003517 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003518{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003519 if( ! operation->key_set )
3520 return( PSA_ERROR_BAD_STATE );
3521 if( operation->iv_required && ! operation->iv_set )
3522 return( PSA_ERROR_BAD_STATE );
3523
Gilles Peskine8c9def32018-02-08 10:02:12 +01003524 if( mac_size < operation->mac_size )
3525 return( PSA_ERROR_BUFFER_TOO_SMALL );
3526
Gilles Peskine8c9def32018-02-08 10:02:12 +01003527#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003528 if( operation->alg == PSA_ALG_CMAC )
3529 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003530 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3531 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3532 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003533 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003534 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003535 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003536 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003537 else
3538#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003539#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003540 if( PSA_ALG_IS_HMAC( operation->alg ) )
3541 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003542 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003543 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003544 }
3545 else
John Durkopd0321952020-10-29 21:37:36 -07003546#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003547 {
3548 /* This shouldn't happen if `operation` was initialized by
3549 * a setup function. */
3550 return( PSA_ERROR_BAD_STATE );
3551 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003552}
3553
Gilles Peskineacd4be32018-07-08 19:56:25 +02003554psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3555 uint8_t *mac,
3556 size_t mac_size,
3557 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003558{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003559 psa_status_t status;
3560
Jaeden Amero252ef282019-02-15 14:05:35 +00003561 if( operation->alg == 0 )
3562 {
3563 return( PSA_ERROR_BAD_STATE );
3564 }
3565
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003566 /* Fill the output buffer with something that isn't a valid mac
3567 * (barring an attack on the mac and deliberately-crafted input),
3568 * in case the caller doesn't check the return status properly. */
3569 *mac_length = mac_size;
3570 /* If mac_size is 0 then mac may be NULL and then the
3571 * call to memset would have undefined behavior. */
3572 if( mac_size != 0 )
3573 memset( mac, '!', mac_size );
3574
Gilles Peskine89167cb2018-07-08 20:12:23 +02003575 if( ! operation->is_sign )
3576 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003577 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003578 }
mohammad16036df908f2018-04-02 08:34:15 -07003579
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003580 status = psa_mac_finish_internal( operation, mac, mac_size );
3581
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003582 if( status == PSA_SUCCESS )
3583 {
3584 status = psa_mac_abort( operation );
3585 if( status == PSA_SUCCESS )
3586 *mac_length = operation->mac_size;
3587 else
3588 memset( mac, '!', mac_size );
3589 }
3590 else
3591 psa_mac_abort( operation );
3592 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003593}
3594
Gilles Peskineacd4be32018-07-08 19:56:25 +02003595psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3596 const uint8_t *mac,
3597 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003598{
Gilles Peskine828ed142018-06-18 23:25:51 +02003599 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003600 psa_status_t status;
3601
Jaeden Amero252ef282019-02-15 14:05:35 +00003602 if( operation->alg == 0 )
3603 {
3604 return( PSA_ERROR_BAD_STATE );
3605 }
3606
Gilles Peskine89167cb2018-07-08 20:12:23 +02003607 if( operation->is_sign )
3608 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003609 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003610 }
3611 if( operation->mac_size != mac_length )
3612 {
3613 status = PSA_ERROR_INVALID_SIGNATURE;
3614 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003615 }
mohammad16036df908f2018-04-02 08:34:15 -07003616
3617 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003618 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003619 if( status != PSA_SUCCESS )
3620 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003621
3622 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3623 status = PSA_ERROR_INVALID_SIGNATURE;
3624
3625cleanup:
3626 if( status == PSA_SUCCESS )
3627 status = psa_mac_abort( operation );
3628 else
3629 psa_mac_abort( operation );
3630
Gilles Peskine3f108122018-12-07 18:14:53 +01003631 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003632
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003633 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003634}
3635
3636
Gilles Peskine20035e32018-02-03 22:44:14 +01003637
Gilles Peskine20035e32018-02-03 22:44:14 +01003638/****************************************************************/
3639/* Asymmetric cryptography */
3640/****************************************************************/
3641
John Durkop6ba40d12020-11-10 08:50:04 -08003642#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3643 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003644/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003645 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003646static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3647 size_t hash_length,
3648 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003649{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003650 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003651 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003652 *md_alg = mbedtls_md_get_type( md_info );
3653
3654 /* The Mbed TLS RSA module uses an unsigned int for hash length
3655 * parameters. Validate that it fits so that we don't risk an
3656 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003657#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003658 if( hash_length > UINT_MAX )
3659 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003660#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003661
John Durkop0e005192020-10-31 22:06:54 -07003662#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003663 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3664 * must be correct. */
3665 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3666 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003667 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003668 if( md_info == NULL )
3669 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003670 if( mbedtls_md_get_size( md_info ) != hash_length )
3671 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003672 }
John Durkop0e005192020-10-31 22:06:54 -07003673#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003674
John Durkop0e005192020-10-31 22:06:54 -07003675#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003676 /* PSS requires a hash internally. */
3677 if( PSA_ALG_IS_RSA_PSS( alg ) )
3678 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003679 if( md_info == NULL )
3680 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003681 }
John Durkop0e005192020-10-31 22:06:54 -07003682#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003683
Gilles Peskine61b91d42018-06-08 16:09:36 +02003684 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003685}
3686
Gilles Peskine2b450e32018-06-27 15:42:46 +02003687static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3688 psa_algorithm_t alg,
3689 const uint8_t *hash,
3690 size_t hash_length,
3691 uint8_t *signature,
3692 size_t signature_size,
3693 size_t *signature_length )
3694{
3695 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003697 mbedtls_md_type_t md_alg;
3698
3699 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3700 if( status != PSA_SUCCESS )
3701 return( status );
3702
Gilles Peskine630a18a2018-06-29 17:49:35 +02003703 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003704 return( PSA_ERROR_BUFFER_TOO_SMALL );
3705
John Durkop0e005192020-10-31 22:06:54 -07003706#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003707 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3708 {
3709 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3710 MBEDTLS_MD_NONE );
3711 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003712 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003713 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003714 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003715 md_alg,
3716 (unsigned int) hash_length,
3717 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003718 signature );
3719 }
3720 else
John Durkop0e005192020-10-31 22:06:54 -07003721#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3722#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003723 if( PSA_ALG_IS_RSA_PSS( alg ) )
3724 {
3725 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3726 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003727 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003728 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003729 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003730 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003731 (unsigned int) hash_length,
3732 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003733 signature );
3734 }
3735 else
John Durkop0e005192020-10-31 22:06:54 -07003736#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003737 {
3738 return( PSA_ERROR_INVALID_ARGUMENT );
3739 }
3740
3741 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003742 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003743 return( mbedtls_to_psa_error( ret ) );
3744}
3745
3746static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3747 psa_algorithm_t alg,
3748 const uint8_t *hash,
3749 size_t hash_length,
3750 const uint8_t *signature,
3751 size_t signature_length )
3752{
3753 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003755 mbedtls_md_type_t md_alg;
3756
3757 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3758 if( status != PSA_SUCCESS )
3759 return( status );
3760
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003761 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3762 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003763
John Durkop0e005192020-10-31 22:06:54 -07003764#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003765 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3766 {
3767 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3768 MBEDTLS_MD_NONE );
3769 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003770 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003771 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003772 MBEDTLS_RSA_PUBLIC,
3773 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003774 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003775 hash,
3776 signature );
3777 }
3778 else
John Durkop0e005192020-10-31 22:06:54 -07003779#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3780#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003781 if( PSA_ALG_IS_RSA_PSS( alg ) )
3782 {
3783 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3784 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003785 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003786 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003787 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003788 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003789 (unsigned int) hash_length,
3790 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003791 signature );
3792 }
3793 else
John Durkop0e005192020-10-31 22:06:54 -07003794#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003795 {
3796 return( PSA_ERROR_INVALID_ARGUMENT );
3797 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003798
3799 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3800 * the rest of the signature is invalid". This has little use in
3801 * practice and PSA doesn't report this distinction. */
3802 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3803 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003804 return( mbedtls_to_psa_error( ret ) );
3805}
John Durkop6ba40d12020-11-10 08:50:04 -08003806#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3807 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003808
John Durkop6ba40d12020-11-10 08:50:04 -08003809#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3810 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003811/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3812 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3813 * (even though these functions don't modify it). */
3814static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3815 psa_algorithm_t alg,
3816 const uint8_t *hash,
3817 size_t hash_length,
3818 uint8_t *signature,
3819 size_t signature_size,
3820 size_t *signature_length )
3821{
Janos Follath24eed8d2019-11-22 13:21:35 +00003822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003823 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003824 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003825 mbedtls_mpi_init( &r );
3826 mbedtls_mpi_init( &s );
3827
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003828 if( signature_size < 2 * curve_bytes )
3829 {
3830 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3831 goto cleanup;
3832 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003833
John Durkop0ea39e02020-10-13 19:58:20 -07003834#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003835 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3836 {
3837 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3838 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3839 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003840 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3841 &ecp->d, hash,
3842 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003843 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003844 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003845 }
3846 else
John Durkop0ea39e02020-10-13 19:58:20 -07003847#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003848 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003849 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003850 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3851 hash, hash_length,
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 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003855
3856 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3857 signature,
3858 curve_bytes ) );
3859 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3860 signature + curve_bytes,
3861 curve_bytes ) );
3862
3863cleanup:
3864 mbedtls_mpi_free( &r );
3865 mbedtls_mpi_free( &s );
3866 if( ret == 0 )
3867 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003868 return( mbedtls_to_psa_error( ret ) );
3869}
3870
3871static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3872 const uint8_t *hash,
3873 size_t hash_length,
3874 const uint8_t *signature,
3875 size_t signature_length )
3876{
Janos Follath24eed8d2019-11-22 13:21:35 +00003877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003878 mbedtls_mpi r, s;
3879 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3880 mbedtls_mpi_init( &r );
3881 mbedtls_mpi_init( &s );
3882
3883 if( signature_length != 2 * curve_bytes )
3884 return( PSA_ERROR_INVALID_SIGNATURE );
3885
3886 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3887 signature,
3888 curve_bytes ) );
3889 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3890 signature + curve_bytes,
3891 curve_bytes ) );
3892
Steven Cooremanacda8342020-07-24 23:09:52 +02003893 /* Check whether the public part is loaded. If not, load it. */
3894 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3895 {
3896 MBEDTLS_MPI_CHK(
3897 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003898 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003899 }
3900
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003901 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3902 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003903
3904cleanup:
3905 mbedtls_mpi_free( &r );
3906 mbedtls_mpi_free( &s );
3907 return( mbedtls_to_psa_error( ret ) );
3908}
John Durkop6ba40d12020-11-10 08:50:04 -08003909#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3910 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003911
Ronald Croncf56a0a2020-08-04 09:51:30 +02003912psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003913 psa_algorithm_t alg,
3914 const uint8_t *hash,
3915 size_t hash_length,
3916 uint8_t *signature,
3917 size_t signature_size,
3918 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003919{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003920 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003921 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003922 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003923
3924 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003925 /* Immediately reject a zero-length signature buffer. This guarantees
3926 * that signature must be a valid pointer. (On the other hand, the hash
3927 * buffer can in principle be empty since it doesn't actually have
3928 * to be a hash.) */
3929 if( signature_size == 0 )
3930 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003931
Ronald Cron5c522922020-11-14 16:35:34 +01003932 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3933 PSA_KEY_USAGE_SIGN_HASH,
3934 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003935 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003936 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003937 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003938 {
3939 status = PSA_ERROR_INVALID_ARGUMENT;
3940 goto exit;
3941 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003942
Steven Cooremancd84cb42020-07-16 20:28:36 +02003943 /* Try any of the available accelerators first */
3944 status = psa_driver_wrapper_sign_hash( slot,
3945 alg,
3946 hash,
3947 hash_length,
3948 signature,
3949 signature_size,
3950 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003951 if( status != PSA_ERROR_NOT_SUPPORTED ||
3952 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003953 goto exit;
3954
Steven Cooreman7a250572020-07-17 16:43:05 +02003955 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003956#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3957 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003958 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003959 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003960 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003961
Steven Cooreman4fed4552020-08-03 14:46:03 +02003962 status = psa_load_rsa_representation( slot->attr.type,
3963 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003964 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003965 &rsa );
3966 if( status != PSA_SUCCESS )
3967 goto exit;
3968
Steven Cooremana2371e52020-07-28 14:30:39 +02003969 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003970 alg,
3971 hash, hash_length,
3972 signature, signature_size,
3973 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003974
Steven Cooremana2371e52020-07-28 14:30:39 +02003975 mbedtls_rsa_free( rsa );
3976 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003977 }
3978 else
John Durkop6ba40d12020-11-10 08:50:04 -08003979#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3980 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003981 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003982 {
John Durkop9814fa22020-11-04 12:28:15 -08003983#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3984 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003985 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003986#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003987 PSA_ALG_IS_ECDSA( alg )
3988#else
3989 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3990#endif
3991 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003992 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003993 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003994 status = psa_load_ecp_representation( slot->attr.type,
3995 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003996 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003997 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003998 if( status != PSA_SUCCESS )
3999 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004000 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004001 alg,
4002 hash, hash_length,
4003 signature, signature_size,
4004 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004005 mbedtls_ecp_keypair_free( ecp );
4006 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004007 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004008 else
John Durkop9814fa22020-11-04 12:28:15 -08004009#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4010 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004011 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004012 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004013 }
itayzafrir5c753392018-05-08 11:18:38 +03004014 }
4015 else
itayzafrir5c753392018-05-08 11:18:38 +03004016 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004017 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004018 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004019
4020exit:
4021 /* Fill the unused part of the output buffer (the whole buffer on error,
4022 * the trailing part on success) with something that isn't a valid mac
4023 * (barring an attack on the mac and deliberately-crafted input),
4024 * in case the caller doesn't check the return status properly. */
4025 if( status == PSA_SUCCESS )
4026 memset( signature + *signature_length, '!',
4027 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02004028 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004029 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004030 /* If signature_size is 0 then we have nothing to do. We must not call
4031 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02004032
Ronald Cron5c522922020-11-14 16:35:34 +01004033 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004034
Ronald Cron5c522922020-11-14 16:35:34 +01004035 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03004036}
4037
Ronald Croncf56a0a2020-08-04 09:51:30 +02004038psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004039 psa_algorithm_t alg,
4040 const uint8_t *hash,
4041 size_t hash_length,
4042 const uint8_t *signature,
4043 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004044{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004045 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004046 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004047 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004048
Ronald Cron5c522922020-11-14 16:35:34 +01004049 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4050 PSA_KEY_USAGE_VERIFY_HASH,
4051 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004052 if( status != PSA_SUCCESS )
4053 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004054
Steven Cooreman55ae2172020-07-17 19:46:15 +02004055 /* Try any of the available accelerators first */
4056 status = psa_driver_wrapper_verify_hash( slot,
4057 alg,
4058 hash,
4059 hash_length,
4060 signature,
4061 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004062 if( status != PSA_ERROR_NOT_SUPPORTED ||
4063 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004064 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004065
John Durkop6ba40d12020-11-10 08:50:04 -08004066#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4067 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004068 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004069 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004070 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004071
Steven Cooreman4fed4552020-08-03 14:46:03 +02004072 status = psa_load_rsa_representation( slot->attr.type,
4073 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004074 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004075 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004076 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004077 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004078
Steven Cooremana2371e52020-07-28 14:30:39 +02004079 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004080 alg,
4081 hash, hash_length,
4082 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004083 mbedtls_rsa_free( rsa );
4084 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004085 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004086 }
4087 else
John Durkop6ba40d12020-11-10 08:50:04 -08004088#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4089 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004090 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004091 {
John Durkop9814fa22020-11-04 12:28:15 -08004092#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4093 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004094 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004095 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004096 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004097 status = psa_load_ecp_representation( slot->attr.type,
4098 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004099 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004100 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004101 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004102 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004103 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004104 hash, hash_length,
4105 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004106 mbedtls_ecp_keypair_free( ecp );
4107 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004108 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004109 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004110 else
John Durkop9814fa22020-11-04 12:28:15 -08004111#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4112 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004113 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004114 status = PSA_ERROR_INVALID_ARGUMENT;
4115 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004116 }
itayzafrir5c753392018-05-08 11:18:38 +03004117 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004118 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004119 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004120 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004121 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004122
4123exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004124 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004125
Ronald Cron5c522922020-11-14 16:35:34 +01004126 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004127}
4128
John Durkop0e005192020-10-31 22:06:54 -07004129#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004130static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4131 mbedtls_rsa_context *rsa )
4132{
4133 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4134 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4135 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4136 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4137}
John Durkop0e005192020-10-31 22:06:54 -07004138#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004139
Ronald Croncf56a0a2020-08-04 09:51:30 +02004140psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004141 psa_algorithm_t alg,
4142 const uint8_t *input,
4143 size_t input_length,
4144 const uint8_t *salt,
4145 size_t salt_length,
4146 uint8_t *output,
4147 size_t output_size,
4148 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004149{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004151 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004152 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004153
Darryl Green5cc689a2018-07-24 15:34:10 +01004154 (void) input;
4155 (void) input_length;
4156 (void) salt;
4157 (void) output;
4158 (void) output_size;
4159
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004160 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004162 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4163 return( PSA_ERROR_INVALID_ARGUMENT );
4164
Ronald Cron5c522922020-11-14 16:35:34 +01004165 status = psa_get_and_lock_transparent_key_slot_with_policy(
4166 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004167 if( status != PSA_SUCCESS )
4168 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004169 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4170 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004171 {
4172 status = PSA_ERROR_INVALID_ARGUMENT;
4173 goto exit;
4174 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004175
John Durkop6ba40d12020-11-10 08:50:04 -08004176#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4177 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004178 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004180 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004181 status = psa_load_rsa_representation( slot->attr.type,
4182 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004183 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004184 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004185 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004186 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004187
4188 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004189 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004190 status = PSA_ERROR_BUFFER_TOO_SMALL;
4191 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004192 }
John Durkop0e005192020-10-31 22:06:54 -07004193#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004194 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004196 status = mbedtls_to_psa_error(
4197 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004198 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004199 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004200 MBEDTLS_RSA_PUBLIC,
4201 input_length,
4202 input,
4203 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004204 }
4205 else
John Durkop0e005192020-10-31 22:06:54 -07004206#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4207#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004208 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004209 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004210 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004211 status = mbedtls_to_psa_error(
4212 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004213 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004214 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004215 MBEDTLS_RSA_PUBLIC,
4216 salt, salt_length,
4217 input_length,
4218 input,
4219 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004220 }
4221 else
John Durkop0e005192020-10-31 22:06:54 -07004222#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004223 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004224 status = PSA_ERROR_INVALID_ARGUMENT;
4225 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004226 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004227rsa_exit:
4228 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004229 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004230
Steven Cooremana2371e52020-07-28 14:30:39 +02004231 mbedtls_rsa_free( rsa );
4232 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004233 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004234 else
John Durkop9814fa22020-11-04 12:28:15 -08004235#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004236 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004237 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004238 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004239 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004240
4241exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004242 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004243
Ronald Cron5c522922020-11-14 16:35:34 +01004244 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004245}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004246
Ronald Croncf56a0a2020-08-04 09:51:30 +02004247psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004248 psa_algorithm_t alg,
4249 const uint8_t *input,
4250 size_t input_length,
4251 const uint8_t *salt,
4252 size_t salt_length,
4253 uint8_t *output,
4254 size_t output_size,
4255 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004256{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004257 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004258 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004259 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004260
Darryl Green5cc689a2018-07-24 15:34:10 +01004261 (void) input;
4262 (void) input_length;
4263 (void) salt;
4264 (void) output;
4265 (void) output_size;
4266
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004267 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004268
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004269 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4270 return( PSA_ERROR_INVALID_ARGUMENT );
4271
Ronald Cron5c522922020-11-14 16:35:34 +01004272 status = psa_get_and_lock_transparent_key_slot_with_policy(
4273 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004274 if( status != PSA_SUCCESS )
4275 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004276 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004277 {
4278 status = PSA_ERROR_INVALID_ARGUMENT;
4279 goto exit;
4280 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004281
John Durkop6ba40d12020-11-10 08:50:04 -08004282#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4283 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004284 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004285 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004286 mbedtls_rsa_context *rsa = NULL;
4287 status = psa_load_rsa_representation( slot->attr.type,
4288 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004289 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004290 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004291 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004292 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004293
Steven Cooremana2371e52020-07-28 14:30:39 +02004294 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004295 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004296 status = PSA_ERROR_INVALID_ARGUMENT;
4297 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004298 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004299
John Durkop0e005192020-10-31 22:06:54 -07004300#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004301 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004302 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004303 status = mbedtls_to_psa_error(
4304 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004305 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004306 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004307 MBEDTLS_RSA_PRIVATE,
4308 output_length,
4309 input,
4310 output,
4311 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004312 }
4313 else
John Durkop0e005192020-10-31 22:06:54 -07004314#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4315#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004316 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004317 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004318 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004319 status = mbedtls_to_psa_error(
4320 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004321 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004322 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004323 MBEDTLS_RSA_PRIVATE,
4324 salt, salt_length,
4325 output_length,
4326 input,
4327 output,
4328 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004329 }
4330 else
John Durkop0e005192020-10-31 22:06:54 -07004331#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004332 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004333 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004334 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004335
Steven Cooreman4fed4552020-08-03 14:46:03 +02004336rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004337 mbedtls_rsa_free( rsa );
4338 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004339 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340 else
John Durkop6ba40d12020-11-10 08:50:04 -08004341#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4342 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004343 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004344 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004345 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004346
4347exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004348 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004349
Ronald Cron5c522922020-11-14 16:35:34 +01004350 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004351}
Gilles Peskine20035e32018-02-03 22:44:14 +01004352
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004353
4354
mohammad1603503973b2018-03-12 15:59:30 +02004355/****************************************************************/
4356/* Symmetric cryptography */
4357/****************************************************************/
4358
Gilles Peskinee553c652018-06-04 16:22:46 +02004359static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004360 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004361 psa_algorithm_t alg,
4362 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004363{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004365 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004366 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004367 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004368 size_t key_bits;
4369 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004370 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4371 PSA_KEY_USAGE_ENCRYPT :
4372 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004373
Steven Cooremand3feccd2020-09-01 15:56:14 +02004374 /* A context must be freshly initialized before it can be set up. */
4375 if( operation->alg != 0 )
4376 return( PSA_ERROR_BAD_STATE );
4377
Steven Cooremana07b9972020-09-10 14:54:14 +02004378 /* The requested algorithm must be one that can be processed by cipher. */
4379 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004380 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004381
Steven Cooremanef8575e2020-09-11 11:44:50 +02004382 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004383 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004384 if( status != PSA_SUCCESS )
4385 goto exit;
4386
4387 /* Initialize the operation struct members, except for alg. The alg member
4388 * is used to indicate to psa_cipher_abort that there are resources to free,
4389 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004390 operation->key_set = 0;
4391 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004392 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004393 operation->iv_size = 0;
4394 operation->block_size = 0;
4395 if( alg == PSA_ALG_ECB_NO_PADDING )
4396 operation->iv_required = 0;
4397 else
4398 operation->iv_required = 1;
4399
Steven Cooremana07b9972020-09-10 14:54:14 +02004400 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004401 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004402 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004403 slot,
4404 alg );
4405 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004406 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004407 slot,
4408 alg );
4409
Steven Cooremanef8575e2020-09-11 11:44:50 +02004410 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004411 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004412 /* Once the driver context is initialised, it needs to be freed using
4413 * psa_cipher_abort. Indicate this through setting alg. */
4414 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004415 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004416
Steven Cooremand3feccd2020-09-01 15:56:14 +02004417 if( status != PSA_ERROR_NOT_SUPPORTED ||
4418 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4419 goto exit;
4420
Steven Cooremana07b9972020-09-10 14:54:14 +02004421 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004422 * available for the given algorithm & key. */
4423 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004424
Steven Cooremana07b9972020-09-10 14:54:14 +02004425 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004426 * psa_cipher_abort. Indicate there is something to be freed through setting
4427 * alg, and indicate the operation is being done using mbedtls crypto through
4428 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004429 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004430 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004431
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004432 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004433 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004434 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004435 {
4436 status = PSA_ERROR_NOT_SUPPORTED;
4437 goto exit;
4438 }
mohammad1603503973b2018-03-12 15:59:30 +02004439
mohammad1603503973b2018-03-12 15:59:30 +02004440 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004441 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004442 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004443
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004444#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004445 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004446 {
4447 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004448 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004449 memcpy( keys, slot->data.key.data, 16 );
4450 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004451 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4452 keys,
4453 192, cipher_operation );
4454 }
4455 else
4456#endif
4457 {
4458 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004459 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004460 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004461 }
Moran Peker41deec42018-04-04 15:43:05 +03004462 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004463 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004464
mohammad16038481e742018-03-18 13:57:31 +02004465#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004466 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004467 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004468 case PSA_ALG_CBC_NO_PADDING:
4469 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4470 MBEDTLS_PADDING_NONE );
4471 break;
4472 case PSA_ALG_CBC_PKCS7:
4473 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4474 MBEDTLS_PADDING_PKCS7 );
4475 break;
4476 default:
4477 /* The algorithm doesn't involve padding. */
4478 ret = 0;
4479 break;
4480 }
4481 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004482 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004483#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4484
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004485 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004486 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004487 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4488 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004489 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004490 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004491 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004492#if defined(MBEDTLS_CHACHA20_C)
4493 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004494 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004495 operation->iv_size = 12;
4496#endif
mohammad1603503973b2018-03-12 15:59:30 +02004497
Steven Cooreman7df02922020-09-09 15:28:49 +02004498 status = PSA_SUCCESS;
4499
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004500exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004501 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004502 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004503 if( status == PSA_SUCCESS )
4504 {
4505 /* Update operation flags for both driver and software implementations */
4506 operation->key_set = 1;
4507 }
4508 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004509 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004510
Ronald Cron5c522922020-11-14 16:35:34 +01004511 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004512
Ronald Cron5c522922020-11-14 16:35:34 +01004513 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004514}
4515
Gilles Peskinefe119512018-07-08 21:39:34 +02004516psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004517 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004518 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004519{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004520 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004521}
4522
Gilles Peskinefe119512018-07-08 21:39:34 +02004523psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004524 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004525 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004526{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004527 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004528}
4529
Gilles Peskinefe119512018-07-08 21:39:34 +02004530psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004531 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004532 size_t iv_size,
4533 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004534{
itayzafrir534bd7c2018-08-02 13:56:32 +03004535 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004537 if( operation->iv_set || ! operation->iv_required )
4538 {
4539 return( PSA_ERROR_BAD_STATE );
4540 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004541
Steven Cooremanef8575e2020-09-11 11:44:50 +02004542 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004543 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004544 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004545 iv,
4546 iv_size,
4547 iv_length );
4548 goto exit;
4549 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004550
Moran Peker41deec42018-04-04 15:43:05 +03004551 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004552 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004553 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004554 goto exit;
4555 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004556 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004557 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004558 if( ret != 0 )
4559 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004560 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004561 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004562 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004563
mohammad16038481e742018-03-18 13:57:31 +02004564 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004565 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004566
Moran Peker395db872018-05-31 14:07:14 +03004567exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004568 if( status == PSA_SUCCESS )
4569 operation->iv_set = 1;
4570 else
Moran Peker395db872018-05-31 14:07:14 +03004571 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004572 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004573}
4574
Gilles Peskinefe119512018-07-08 21:39:34 +02004575psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004576 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004577 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004578{
itayzafrir534bd7c2018-08-02 13:56:32 +03004579 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004581 if( operation->iv_set || ! operation->iv_required )
4582 {
4583 return( PSA_ERROR_BAD_STATE );
4584 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004585
Steven Cooremanef8575e2020-09-11 11:44:50 +02004586 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004587 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004588 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004589 iv,
4590 iv_length );
4591 goto exit;
4592 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004593
Moran Pekera28258c2018-05-29 16:25:04 +03004594 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004595 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004596 status = PSA_ERROR_INVALID_ARGUMENT;
4597 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004598 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004599 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4600 status = mbedtls_to_psa_error( ret );
4601exit:
4602 if( status == PSA_SUCCESS )
4603 operation->iv_set = 1;
4604 else
mohammad1603503973b2018-03-12 15:59:30 +02004605 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004606 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004607}
4608
Steven Cooreman177deba2020-09-07 17:14:14 +02004609/* Process input for which the algorithm is set to ECB mode. This requires
4610 * manual processing, since the PSA API is defined as being able to process
4611 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4612 * underlying mbedtls_cipher_update only takes full blocks. */
4613static psa_status_t psa_cipher_update_ecb_internal(
4614 mbedtls_cipher_context_t *ctx,
4615 const uint8_t *input,
4616 size_t input_length,
4617 uint8_t *output,
4618 size_t output_size,
4619 size_t *output_length )
4620{
4621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4622 size_t block_size = ctx->cipher_info->block_size;
4623 size_t internal_output_length = 0;
4624 *output_length = 0;
4625
4626 if( input_length == 0 )
4627 {
4628 status = PSA_SUCCESS;
4629 goto exit;
4630 }
4631
4632 if( ctx->unprocessed_len > 0 )
4633 {
4634 /* Fill up to block size, and run the block if there's a full one. */
4635 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4636
4637 if( input_length < bytes_to_copy )
4638 bytes_to_copy = input_length;
4639
4640 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4641 input, bytes_to_copy );
4642 input_length -= bytes_to_copy;
4643 input += bytes_to_copy;
4644 ctx->unprocessed_len += bytes_to_copy;
4645
4646 if( ctx->unprocessed_len == block_size )
4647 {
4648 status = mbedtls_to_psa_error(
4649 mbedtls_cipher_update( ctx,
4650 ctx->unprocessed_data,
4651 block_size,
4652 output, &internal_output_length ) );
4653
4654 if( status != PSA_SUCCESS )
4655 goto exit;
4656
4657 output += internal_output_length;
4658 output_size -= internal_output_length;
4659 *output_length += internal_output_length;
4660 ctx->unprocessed_len = 0;
4661 }
4662 }
4663
4664 while( input_length >= block_size )
4665 {
4666 /* Run all full blocks we have, one by one */
4667 status = mbedtls_to_psa_error(
4668 mbedtls_cipher_update( ctx, input,
4669 block_size,
4670 output, &internal_output_length ) );
4671
4672 if( status != PSA_SUCCESS )
4673 goto exit;
4674
4675 input_length -= block_size;
4676 input += block_size;
4677
4678 output += internal_output_length;
4679 output_size -= internal_output_length;
4680 *output_length += internal_output_length;
4681 }
4682
4683 if( input_length > 0 )
4684 {
4685 /* Save unprocessed bytes for later processing */
4686 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4687 input, input_length );
4688 ctx->unprocessed_len += input_length;
4689 }
4690
4691 status = PSA_SUCCESS;
4692
4693exit:
4694 return( status );
4695}
4696
Gilles Peskinee553c652018-06-04 16:22:46 +02004697psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4698 const uint8_t *input,
4699 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004700 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004701 size_t output_size,
4702 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004703{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004705 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004706 if( operation->alg == 0 )
4707 {
4708 return( PSA_ERROR_BAD_STATE );
4709 }
4710 if( operation->iv_required && ! operation->iv_set )
4711 {
4712 return( PSA_ERROR_BAD_STATE );
4713 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004714
Steven Cooremanef8575e2020-09-11 11:44:50 +02004715 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004716 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004717 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004718 input,
4719 input_length,
4720 output,
4721 output_size,
4722 output_length );
4723 goto exit;
4724 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004725
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004726 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004727 {
4728 /* Take the unprocessed partial block left over from previous
4729 * update calls, if any, plus the input to this call. Remove
4730 * the last partial block, if any. You get the data that will be
4731 * output in this call. */
4732 expected_output_size =
4733 ( operation->ctx.cipher.unprocessed_len + input_length )
4734 / operation->block_size * operation->block_size;
4735 }
4736 else
4737 {
4738 expected_output_size = input_length;
4739 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004740
Gilles Peskine89d789c2018-06-04 17:17:16 +02004741 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004742 {
4743 status = PSA_ERROR_BUFFER_TOO_SMALL;
4744 goto exit;
4745 }
mohammad160382759612018-03-12 18:16:40 +02004746
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004747 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4748 {
4749 /* mbedtls_cipher_update has an API inconsistency: it will only
4750 * process a single block at a time in ECB mode. Abstract away that
4751 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004752 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4753 input,
4754 input_length,
4755 output,
4756 output_size,
4757 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004758 }
4759 else
4760 {
4761 status = mbedtls_to_psa_error(
4762 mbedtls_cipher_update( &operation->ctx.cipher, input,
4763 input_length, output, output_length ) );
4764 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004765exit:
4766 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004767 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004768 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004769}
4770
Gilles Peskinee553c652018-06-04 16:22:46 +02004771psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4772 uint8_t *output,
4773 size_t output_size,
4774 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004775{
David Saadab4ecc272019-02-14 13:48:10 +02004776 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004777 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004778 if( operation->alg == 0 )
4779 {
4780 return( PSA_ERROR_BAD_STATE );
4781 }
4782 if( operation->iv_required && ! operation->iv_set )
4783 {
4784 return( PSA_ERROR_BAD_STATE );
4785 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004786
Steven Cooremanef8575e2020-09-11 11:44:50 +02004787 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004788 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004789 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004790 output,
4791 output_size,
4792 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004793 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004794 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004795
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004796 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004797 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004798 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004799 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004800 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004801 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004802 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004803 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004804 }
4805
Steven Cooremanef8575e2020-09-11 11:44:50 +02004806 status = mbedtls_to_psa_error(
4807 mbedtls_cipher_finish( &operation->ctx.cipher,
4808 temp_output_buffer,
4809 output_length ) );
4810 if( status != PSA_SUCCESS )
4811 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004812
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004813 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004814 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004815 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004816 memcpy( output, temp_output_buffer, *output_length );
4817 else
Janos Follath315b51c2018-07-09 16:04:51 +01004818 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004819
Steven Cooremanef8575e2020-09-11 11:44:50 +02004820exit:
4821 if( operation->mbedtls_in_use == 1 )
4822 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004823
Steven Cooremanef8575e2020-09-11 11:44:50 +02004824 if( status == PSA_SUCCESS )
4825 return( psa_cipher_abort( operation ) );
4826 else
4827 {
4828 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004829 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004830
Steven Cooremanef8575e2020-09-11 11:44:50 +02004831 return( status );
4832 }
mohammad1603503973b2018-03-12 15:59:30 +02004833}
4834
Gilles Peskinee553c652018-06-04 16:22:46 +02004835psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4836{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004837 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004838 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004839 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004840 * in use. It's ok to call abort on such an object, and there's
4841 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004842 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004843 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004844
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004845 /* Sanity check (shouldn't happen: operation->alg should
4846 * always have been initialized to a valid value). */
4847 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4848 return( PSA_ERROR_BAD_STATE );
4849
Steven Cooremanef8575e2020-09-11 11:44:50 +02004850 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004851 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004852 else
4853 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004854
Moran Peker41deec42018-04-04 15:43:05 +03004855 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004856 operation->key_set = 0;
4857 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004858 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004859 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004860 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004861 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004862
Moran Peker395db872018-05-31 14:07:14 +03004863 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004864}
4865
Gilles Peskinea0655c32018-04-30 17:06:50 +02004866
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004867
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004868
mohammad16035955c982018-04-26 00:53:03 +03004869/****************************************************************/
4870/* AEAD */
4871/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004872
Gilles Peskineedf9a652018-08-17 18:11:56 +02004873typedef struct
4874{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004875 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004876 const mbedtls_cipher_info_t *cipher_info;
4877 union
4878 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004879 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004880#if defined(MBEDTLS_CCM_C)
4881 mbedtls_ccm_context ccm;
4882#endif /* MBEDTLS_CCM_C */
4883#if defined(MBEDTLS_GCM_C)
4884 mbedtls_gcm_context gcm;
4885#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004886#if defined(MBEDTLS_CHACHAPOLY_C)
4887 mbedtls_chachapoly_context chachapoly;
4888#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004889 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004890 psa_algorithm_t core_alg;
4891 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004892 uint8_t tag_length;
4893} aead_operation_t;
4894
Ronald Cronf95a2b12020-10-22 15:24:49 +02004895#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4896
Gilles Peskine30a9e412019-01-14 18:36:12 +01004897static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004898{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004899 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004900 {
4901#if defined(MBEDTLS_CCM_C)
4902 case PSA_ALG_CCM:
4903 mbedtls_ccm_free( &operation->ctx.ccm );
4904 break;
4905#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004906#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004907 case PSA_ALG_GCM:
4908 mbedtls_gcm_free( &operation->ctx.gcm );
4909 break;
4910#endif /* MBEDTLS_GCM_C */
4911 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004912
Ronald Cron5c522922020-11-14 16:35:34 +01004913 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004914}
4915
4916static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004917 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004918 psa_key_usage_t usage,
4919 psa_algorithm_t alg )
4920{
4921 psa_status_t status;
4922 size_t key_bits;
4923 mbedtls_cipher_id_t cipher_id;
4924
Ronald Cron5c522922020-11-14 16:35:34 +01004925 status = psa_get_and_lock_transparent_key_slot_with_policy(
4926 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004927 if( status != PSA_SUCCESS )
4928 return( status );
4929
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004930 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004931
4932 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004933 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004934 &cipher_id );
4935 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004936 {
4937 status = PSA_ERROR_NOT_SUPPORTED;
4938 goto cleanup;
4939 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004940
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004941 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004942 {
4943#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004944 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4945 operation->core_alg = PSA_ALG_CCM;
4946 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004947 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4948 * The call to mbedtls_ccm_encrypt_and_tag or
4949 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004950 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004951 {
4952 status = PSA_ERROR_INVALID_ARGUMENT;
4953 goto cleanup;
4954 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004955 mbedtls_ccm_init( &operation->ctx.ccm );
4956 status = mbedtls_to_psa_error(
4957 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004958 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004959 (unsigned int) key_bits ) );
4960 if( status != 0 )
4961 goto cleanup;
4962 break;
4963#endif /* MBEDTLS_CCM_C */
4964
4965#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004966 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4967 operation->core_alg = PSA_ALG_GCM;
4968 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004969 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4970 * The call to mbedtls_gcm_crypt_and_tag or
4971 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004972 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004973 {
4974 status = PSA_ERROR_INVALID_ARGUMENT;
4975 goto cleanup;
4976 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004977 mbedtls_gcm_init( &operation->ctx.gcm );
4978 status = mbedtls_to_psa_error(
4979 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004980 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004981 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004982 if( status != 0 )
4983 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004984 break;
4985#endif /* MBEDTLS_GCM_C */
4986
Gilles Peskine26869f22019-05-06 15:25:00 +02004987#if defined(MBEDTLS_CHACHAPOLY_C)
4988 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4989 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4990 operation->full_tag_length = 16;
4991 /* We only support the default tag length. */
4992 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004993 {
4994 status = PSA_ERROR_NOT_SUPPORTED;
4995 goto cleanup;
4996 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004997 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4998 status = mbedtls_to_psa_error(
4999 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005000 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02005001 if( status != 0 )
5002 goto cleanup;
5003 break;
5004#endif /* MBEDTLS_CHACHAPOLY_C */
5005
Gilles Peskineedf9a652018-08-17 18:11:56 +02005006 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02005007 status = PSA_ERROR_NOT_SUPPORTED;
5008 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005009 }
5010
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005011 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
5012 {
5013 status = PSA_ERROR_INVALID_ARGUMENT;
5014 goto cleanup;
5015 }
5016 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005017
Gilles Peskineedf9a652018-08-17 18:11:56 +02005018 return( PSA_SUCCESS );
5019
5020cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005021 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005022 return( status );
5023}
5024
Ronald Croncf56a0a2020-08-04 09:51:30 +02005025psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005026 psa_algorithm_t alg,
5027 const uint8_t *nonce,
5028 size_t nonce_length,
5029 const uint8_t *additional_data,
5030 size_t additional_data_length,
5031 const uint8_t *plaintext,
5032 size_t plaintext_length,
5033 uint8_t *ciphertext,
5034 size_t ciphertext_size,
5035 size_t *ciphertext_length )
5036{
mohammad16035955c982018-04-26 00:53:03 +03005037 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005038 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03005039 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005040
mohammad1603f08a5502018-06-03 15:05:47 +03005041 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005042
Ronald Croncf56a0a2020-08-04 09:51:30 +02005043 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005044 if( status != PSA_SUCCESS )
5045 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005046
Gilles Peskineedf9a652018-08-17 18:11:56 +02005047 /* For all currently supported modes, the tag is at the end of the
5048 * ciphertext. */
5049 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5050 {
5051 status = PSA_ERROR_BUFFER_TOO_SMALL;
5052 goto exit;
5053 }
5054 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005055
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005056#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005057 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005058 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005059 status = mbedtls_to_psa_error(
5060 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5061 MBEDTLS_GCM_ENCRYPT,
5062 plaintext_length,
5063 nonce, nonce_length,
5064 additional_data, additional_data_length,
5065 plaintext, ciphertext,
5066 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005067 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005068 else
5069#endif /* MBEDTLS_GCM_C */
5070#if defined(MBEDTLS_CCM_C)
5071 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005072 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005073 status = mbedtls_to_psa_error(
5074 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5075 plaintext_length,
5076 nonce, nonce_length,
5077 additional_data,
5078 additional_data_length,
5079 plaintext, ciphertext,
5080 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005081 }
mohammad16035c8845f2018-05-09 05:40:09 -07005082 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005083#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005084#if defined(MBEDTLS_CHACHAPOLY_C)
5085 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5086 {
5087 if( nonce_length != 12 || operation.tag_length != 16 )
5088 {
5089 status = PSA_ERROR_NOT_SUPPORTED;
5090 goto exit;
5091 }
5092 status = mbedtls_to_psa_error(
5093 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5094 plaintext_length,
5095 nonce,
5096 additional_data,
5097 additional_data_length,
5098 plaintext,
5099 ciphertext,
5100 tag ) );
5101 }
5102 else
5103#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005104 {
mohammad1603554faad2018-06-03 15:07:38 +03005105 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005106 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005107
Gilles Peskineedf9a652018-08-17 18:11:56 +02005108 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5109 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005110
Gilles Peskineedf9a652018-08-17 18:11:56 +02005111exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005112 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005113 if( status == PSA_SUCCESS )
5114 *ciphertext_length = plaintext_length + operation.tag_length;
5115 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005116}
5117
Gilles Peskineee652a32018-06-01 19:23:52 +02005118/* Locate the tag in a ciphertext buffer containing the encrypted data
5119 * followed by the tag. Return the length of the part preceding the tag in
5120 * *plaintext_length. This is the size of the plaintext in modes where
5121 * the encrypted data has the same size as the plaintext, such as
5122 * CCM and GCM. */
5123static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5124 const uint8_t *ciphertext,
5125 size_t ciphertext_length,
5126 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005127 const uint8_t **p_tag )
5128{
5129 size_t payload_length;
5130 if( tag_length > ciphertext_length )
5131 return( PSA_ERROR_INVALID_ARGUMENT );
5132 payload_length = ciphertext_length - tag_length;
5133 if( payload_length > plaintext_size )
5134 return( PSA_ERROR_BUFFER_TOO_SMALL );
5135 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005136 return( PSA_SUCCESS );
5137}
5138
Ronald Croncf56a0a2020-08-04 09:51:30 +02005139psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005140 psa_algorithm_t alg,
5141 const uint8_t *nonce,
5142 size_t nonce_length,
5143 const uint8_t *additional_data,
5144 size_t additional_data_length,
5145 const uint8_t *ciphertext,
5146 size_t ciphertext_length,
5147 uint8_t *plaintext,
5148 size_t plaintext_size,
5149 size_t *plaintext_length )
5150{
mohammad16035955c982018-04-26 00:53:03 +03005151 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005152 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005153 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005154
Gilles Peskineee652a32018-06-01 19:23:52 +02005155 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005156
Ronald Croncf56a0a2020-08-04 09:51:30 +02005157 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005158 if( status != PSA_SUCCESS )
5159 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005160
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005161 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5162 ciphertext, ciphertext_length,
5163 plaintext_size, &tag );
5164 if( status != PSA_SUCCESS )
5165 goto exit;
5166
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005167#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005168 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005169 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005170 status = mbedtls_to_psa_error(
5171 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5172 ciphertext_length - operation.tag_length,
5173 nonce, nonce_length,
5174 additional_data,
5175 additional_data_length,
5176 tag, operation.tag_length,
5177 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005178 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005179 else
5180#endif /* MBEDTLS_GCM_C */
5181#if defined(MBEDTLS_CCM_C)
5182 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005183 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005184 status = mbedtls_to_psa_error(
5185 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5186 ciphertext_length - operation.tag_length,
5187 nonce, nonce_length,
5188 additional_data,
5189 additional_data_length,
5190 ciphertext, plaintext,
5191 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005192 }
mohammad160339574652018-06-01 04:39:53 -07005193 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005194#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005195#if defined(MBEDTLS_CHACHAPOLY_C)
5196 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5197 {
5198 if( nonce_length != 12 || operation.tag_length != 16 )
5199 {
5200 status = PSA_ERROR_NOT_SUPPORTED;
5201 goto exit;
5202 }
5203 status = mbedtls_to_psa_error(
5204 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5205 ciphertext_length - operation.tag_length,
5206 nonce,
5207 additional_data,
5208 additional_data_length,
5209 tag,
5210 ciphertext,
5211 plaintext ) );
5212 }
5213 else
5214#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005215 {
mohammad1603554faad2018-06-03 15:07:38 +03005216 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005217 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005218
Gilles Peskineedf9a652018-08-17 18:11:56 +02005219 if( status != PSA_SUCCESS && plaintext_size != 0 )
5220 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005221
Gilles Peskineedf9a652018-08-17 18:11:56 +02005222exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005223 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005224 if( status == PSA_SUCCESS )
5225 *plaintext_length = ciphertext_length - operation.tag_length;
5226 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005227}
5228
Gilles Peskinea0655c32018-04-30 17:06:50 +02005229
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005230
Gilles Peskine20035e32018-02-03 22:44:14 +01005231/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005232/* Generators */
5233/****************************************************************/
5234
John Durkop07cc04a2020-11-16 22:08:34 -08005235#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5236 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5237 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5238#define AT_LEAST_ONE_BUILTIN_KDF
5239#endif
5240
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005241#define HKDF_STATE_INIT 0 /* no input yet */
5242#define HKDF_STATE_STARTED 1 /* got salt */
5243#define HKDF_STATE_KEYED 2 /* got key */
5244#define HKDF_STATE_OUTPUT 3 /* output started */
5245
Gilles Peskinecbe66502019-05-16 16:59:18 +02005246static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005248{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5250 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005251 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005252 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005253}
5254
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005256{
5257 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005258 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005259 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005260 {
5261 /* The object has (apparently) been initialized but it is not
5262 * in use. It's ok to call abort on such an object, and there's
5263 * nothing to do. */
5264 }
5265 else
John Durkopd0321952020-10-29 21:37:36 -07005266#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005267 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005268 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 mbedtls_free( operation->ctx.hkdf.info );
5270 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005271 }
John Durkop07cc04a2020-11-16 22:08:34 -08005272 else
5273#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5274#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5275 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5276 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005278 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005279 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005280 if( operation->ctx.tls12_prf.seed != NULL )
5281 {
5282 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5283 operation->ctx.tls12_prf.seed_length );
5284 mbedtls_free( operation->ctx.tls12_prf.seed );
5285 }
5286
5287 if( operation->ctx.tls12_prf.label != NULL )
5288 {
5289 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5290 operation->ctx.tls12_prf.label_length );
5291 mbedtls_free( operation->ctx.tls12_prf.label );
5292 }
5293
5294 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5295
5296 /* We leave the fields Ai and output_block to be erased safely by the
5297 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005298 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005299 else
John Durkop07cc04a2020-11-16 22:08:34 -08005300#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5301 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005302 {
5303 status = PSA_ERROR_BAD_STATE;
5304 }
Janos Follath083036a2019-06-11 10:22:26 +01005305 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005306 return( status );
5307}
5308
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005310 size_t *capacity)
5311{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005312 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005313 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005314 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005315 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005316 }
5317
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005319 return( PSA_SUCCESS );
5320}
5321
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005323 size_t capacity )
5324{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005325 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005326 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005327 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005328 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005330 return( PSA_SUCCESS );
5331}
5332
John Durkopd0321952020-10-29 21:37:36 -07005333#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005335 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005336static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005337 psa_algorithm_t hash_alg,
5338 uint8_t *output,
5339 size_t output_length )
5340{
5341 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5342 psa_status_t status;
5343
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005344 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5345 return( PSA_ERROR_BAD_STATE );
5346 hkdf->state = HKDF_STATE_OUTPUT;
5347
Gilles Peskinebef7f142018-07-12 17:22:21 +02005348 while( output_length != 0 )
5349 {
5350 /* Copy what remains of the current block */
5351 uint8_t n = hash_length - hkdf->offset_in_block;
5352 if( n > output_length )
5353 n = (uint8_t) output_length;
5354 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5355 output += n;
5356 output_length -= n;
5357 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005358 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005359 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005360 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005361 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005362 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005363 * object was corrupted or if this function is called directly
5364 * inside the library. */
5365 if( hkdf->block_number == 0xff )
5366 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005367
5368 /* We need a new block */
5369 ++hkdf->block_number;
5370 hkdf->offset_in_block = 0;
5371 status = psa_hmac_setup_internal( &hkdf->hmac,
5372 hkdf->prk, hash_length,
5373 hash_alg );
5374 if( status != PSA_SUCCESS )
5375 return( status );
5376 if( hkdf->block_number != 1 )
5377 {
5378 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5379 hkdf->output_block,
5380 hash_length );
5381 if( status != PSA_SUCCESS )
5382 return( status );
5383 }
5384 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5385 hkdf->info,
5386 hkdf->info_length );
5387 if( status != PSA_SUCCESS )
5388 return( status );
5389 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5390 &hkdf->block_number, 1 );
5391 if( status != PSA_SUCCESS )
5392 return( status );
5393 status = psa_hmac_finish_internal( &hkdf->hmac,
5394 hkdf->output_block,
5395 sizeof( hkdf->output_block ) );
5396 if( status != PSA_SUCCESS )
5397 return( status );
5398 }
5399
5400 return( PSA_SUCCESS );
5401}
John Durkop07cc04a2020-11-16 22:08:34 -08005402#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005403
John Durkop07cc04a2020-11-16 22:08:34 -08005404#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5405 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005406static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5407 psa_tls12_prf_key_derivation_t *tls12_prf,
5408 psa_algorithm_t alg )
5409{
5410 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5411 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005412 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5413 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005414
Janos Follath7742fee2019-06-17 12:58:10 +01005415 /* We can't be wanting more output after block 0xff, otherwise
5416 * the capacity check in psa_key_derivation_output_bytes() would have
5417 * prevented this call. It could happen only if the operation
5418 * object was corrupted or if this function is called directly
5419 * inside the library. */
5420 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005421 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005422
5423 /* We need a new block */
5424 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005425 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005426
5427 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5428 *
5429 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5430 *
5431 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5432 * HMAC_hash(secret, A(2) + seed) +
5433 * HMAC_hash(secret, A(3) + seed) + ...
5434 *
5435 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005436 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005437 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005438 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005439 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005440 * is currently extracted as `output_block` and where i is
5441 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005442 */
5443
Janos Follathea29bfb2019-06-19 12:21:20 +01005444 /* Save the hash context before using it, to preserve the hash state with
5445 * only the inner padding in it. We need this, because inner padding depends
5446 * on the key (secret in the RFC's terminology). */
5447 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5448 if( status != PSA_SUCCESS )
5449 goto cleanup;
5450
5451 /* Calculate A(i) where i = tls12_prf->block_number. */
5452 if( tls12_prf->block_number == 1 )
5453 {
5454 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5455 * the variable seed and in this instance means it in the context of the
5456 * P_hash function, where seed = label + seed.) */
5457 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5458 tls12_prf->label, tls12_prf->label_length );
5459 if( status != PSA_SUCCESS )
5460 goto cleanup;
5461 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5462 tls12_prf->seed, tls12_prf->seed_length );
5463 if( status != PSA_SUCCESS )
5464 goto cleanup;
5465 }
5466 else
5467 {
5468 /* A(i) = HMAC_hash(secret, A(i-1)) */
5469 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5470 tls12_prf->Ai, hash_length );
5471 if( status != PSA_SUCCESS )
5472 goto cleanup;
5473 }
5474
5475 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5476 tls12_prf->Ai, hash_length );
5477 if( status != PSA_SUCCESS )
5478 goto cleanup;
5479 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5480 if( status != PSA_SUCCESS )
5481 goto cleanup;
5482
5483 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5484 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5485 tls12_prf->Ai, hash_length );
5486 if( status != PSA_SUCCESS )
5487 goto cleanup;
5488 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5489 tls12_prf->label, tls12_prf->label_length );
5490 if( status != PSA_SUCCESS )
5491 goto cleanup;
5492 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5493 tls12_prf->seed, tls12_prf->seed_length );
5494 if( status != PSA_SUCCESS )
5495 goto cleanup;
5496 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5497 tls12_prf->output_block, hash_length );
5498 if( status != PSA_SUCCESS )
5499 goto cleanup;
5500 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5501 if( status != PSA_SUCCESS )
5502 goto cleanup;
5503
Janos Follath7742fee2019-06-17 12:58:10 +01005504
5505cleanup:
5506
Janos Follathea29bfb2019-06-19 12:21:20 +01005507 cleanup_status = psa_hash_abort( &backup );
5508 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5509 status = cleanup_status;
5510
5511 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005512}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005513
Janos Follath844eb0e2019-06-19 12:10:49 +01005514static psa_status_t psa_key_derivation_tls12_prf_read(
5515 psa_tls12_prf_key_derivation_t *tls12_prf,
5516 psa_algorithm_t alg,
5517 uint8_t *output,
5518 size_t output_length )
5519{
5520 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5521 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5522 psa_status_t status;
5523 uint8_t offset, length;
5524
5525 while( output_length != 0 )
5526 {
5527 /* Check if we have fully processed the current block. */
5528 if( tls12_prf->left_in_block == 0 )
5529 {
5530 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5531 alg );
5532 if( status != PSA_SUCCESS )
5533 return( status );
5534
5535 continue;
5536 }
5537
5538 if( tls12_prf->left_in_block > output_length )
5539 length = (uint8_t) output_length;
5540 else
5541 length = tls12_prf->left_in_block;
5542
5543 offset = hash_length - tls12_prf->left_in_block;
5544 memcpy( output, tls12_prf->output_block + offset, length );
5545 output += length;
5546 output_length -= length;
5547 tls12_prf->left_in_block -= length;
5548 }
5549
5550 return( PSA_SUCCESS );
5551}
John Durkop07cc04a2020-11-16 22:08:34 -08005552#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5553 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005554
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005555psa_status_t psa_key_derivation_output_bytes(
5556 psa_key_derivation_operation_t *operation,
5557 uint8_t *output,
5558 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005559{
5560 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005561 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005562
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005563 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005564 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005566 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005567 }
5568
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005569 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005570 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005571 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005572 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005573 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005574 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005575 goto exit;
5576 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005577 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005578 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005579 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005580 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005581 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5582 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005583 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005584 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005585 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005586 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005587 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005588
John Durkopd0321952020-10-29 21:37:36 -07005589#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005590 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005591 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005592 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005593 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005594 output, output_length );
5595 }
Janos Follathadbec812019-06-14 11:05:39 +01005596 else
John Durkop07cc04a2020-11-16 22:08:34 -08005597#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5598#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5599 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005600 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005601 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005602 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005603 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005604 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005605 output_length );
5606 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005607 else
John Durkop07cc04a2020-11-16 22:08:34 -08005608#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5609 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005610 {
5611 return( PSA_ERROR_BAD_STATE );
5612 }
5613
5614exit:
5615 if( status != PSA_SUCCESS )
5616 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005617 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005618 * This allows us to differentiate between exhausted operations and
5619 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5620 * operations. */
5621 psa_algorithm_t alg = operation->alg;
5622 psa_key_derivation_abort( operation );
5623 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005624 memset( output, '!', output_length );
5625 }
5626 return( status );
5627}
5628
Gilles Peskine08542d82018-07-19 17:05:42 +02005629#if defined(MBEDTLS_DES_C)
5630static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5631{
5632 if( data_size >= 8 )
5633 mbedtls_des_key_set_parity( data );
5634 if( data_size >= 16 )
5635 mbedtls_des_key_set_parity( data + 8 );
5636 if( data_size >= 24 )
5637 mbedtls_des_key_set_parity( data + 16 );
5638}
5639#endif /* MBEDTLS_DES_C */
5640
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005641static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005642 psa_key_slot_t *slot,
5643 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005644 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005645{
5646 uint8_t *data = NULL;
5647 size_t bytes = PSA_BITS_TO_BYTES( bits );
5648 psa_status_t status;
5649
Gilles Peskine8e338702019-07-30 20:06:31 +02005650 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005651 return( PSA_ERROR_INVALID_ARGUMENT );
5652 if( bits % 8 != 0 )
5653 return( PSA_ERROR_INVALID_ARGUMENT );
5654 data = mbedtls_calloc( 1, bytes );
5655 if( data == NULL )
5656 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5657
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005658 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005659 if( status != PSA_SUCCESS )
5660 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005661#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005662 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005663 psa_des_set_key_parity( data, bytes );
5664#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005665 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005666
5667exit:
5668 mbedtls_free( data );
5669 return( status );
5670}
5671
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005672psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005673 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005674 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005675{
5676 psa_status_t status;
5677 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005678 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005679
Ronald Cron81709fc2020-11-14 12:10:32 +01005680 *key = MBEDTLS_SVC_KEY_ID_INIT;
5681
Gilles Peskine0f84d622019-09-12 19:03:13 +02005682 /* Reject any attempt to create a zero-length key so that we don't
5683 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5684 if( psa_get_key_bits( attributes ) == 0 )
5685 return( PSA_ERROR_INVALID_ARGUMENT );
5686
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005687 if( ! operation->can_output_key )
5688 return( PSA_ERROR_NOT_PERMITTED );
5689
Ronald Cron81709fc2020-11-14 12:10:32 +01005690 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5691 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005692#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5693 if( driver != NULL )
5694 {
5695 /* Deriving a key in a secure element is not implemented yet. */
5696 status = PSA_ERROR_NOT_SUPPORTED;
5697 }
5698#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005699 if( status == PSA_SUCCESS )
5700 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005701 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005702 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005703 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005704 }
5705 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005706 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005707 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005708 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005709
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005710 return( status );
5711}
5712
Gilles Peskineeab56e42018-07-12 17:12:33 +02005713
5714
5715/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005716/* Key derivation */
5717/****************************************************************/
5718
John Durkop07cc04a2020-11-16 22:08:34 -08005719#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005720static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005721 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005722 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005723{
John Durkop07cc04a2020-11-16 22:08:34 -08005724 int is_kdf_alg_supported;
5725
Janos Follath5fe19732019-06-20 15:09:30 +01005726 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5727 * initialisers for this union leave some bytes unspecified.) */
5728 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5729
Gilles Peskine969c5d62019-01-16 15:53:06 +01005730 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005731#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005732 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5733 is_kdf_alg_supported = 1;
5734 else
5735#endif
5736#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5737 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5738 is_kdf_alg_supported = 1;
5739 else
5740#endif
5741#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5742 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5743 is_kdf_alg_supported = 1;
5744 else
5745#endif
5746 is_kdf_alg_supported = 0;
5747
5748 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005749 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005750 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005751 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5752 if( hash_size == 0 )
5753 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005754 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5755 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005756 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005757 {
5758 return( PSA_ERROR_NOT_SUPPORTED );
5759 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005760 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005761 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005762 }
John Durkop07cc04a2020-11-16 22:08:34 -08005763
5764 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005765}
John Durkop07cc04a2020-11-16 22:08:34 -08005766#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005767
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005769 psa_algorithm_t alg )
5770{
5771 psa_status_t status;
5772
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005773 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005774 return( PSA_ERROR_BAD_STATE );
5775
Gilles Peskine6843c292019-01-18 16:44:49 +01005776 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5777 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005778#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005779 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005780 {
5781 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005782 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005783 }
5784 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5785 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005786 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005787 }
John Durkop07cc04a2020-11-16 22:08:34 -08005788#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005789 else
5790 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005791
5792 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005793 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005794 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005795}
5796
John Durkopd0321952020-10-29 21:37:36 -07005797#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005798static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005799 psa_algorithm_t hash_alg,
5800 psa_key_derivation_step_t step,
5801 const uint8_t *data,
5802 size_t data_length )
5803{
5804 psa_status_t status;
5805 switch( step )
5806 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005807 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005808 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005809 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005810 status = psa_hmac_setup_internal( &hkdf->hmac,
5811 data, data_length,
5812 hash_alg );
5813 if( status != PSA_SUCCESS )
5814 return( status );
5815 hkdf->state = HKDF_STATE_STARTED;
5816 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005817 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005818 /* If no salt was provided, use an empty salt. */
5819 if( hkdf->state == HKDF_STATE_INIT )
5820 {
5821 status = psa_hmac_setup_internal( &hkdf->hmac,
5822 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005823 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005824 if( status != PSA_SUCCESS )
5825 return( status );
5826 hkdf->state = HKDF_STATE_STARTED;
5827 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005828 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005829 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005830 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5831 data, data_length );
5832 if( status != PSA_SUCCESS )
5833 return( status );
5834 status = psa_hmac_finish_internal( &hkdf->hmac,
5835 hkdf->prk,
5836 sizeof( hkdf->prk ) );
5837 if( status != PSA_SUCCESS )
5838 return( status );
5839 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5840 hkdf->block_number = 0;
5841 hkdf->state = HKDF_STATE_KEYED;
5842 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005843 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005844 if( hkdf->state == HKDF_STATE_OUTPUT )
5845 return( PSA_ERROR_BAD_STATE );
5846 if( hkdf->info_set )
5847 return( PSA_ERROR_BAD_STATE );
5848 hkdf->info_length = data_length;
5849 if( data_length != 0 )
5850 {
5851 hkdf->info = mbedtls_calloc( 1, data_length );
5852 if( hkdf->info == NULL )
5853 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5854 memcpy( hkdf->info, data, data_length );
5855 }
5856 hkdf->info_set = 1;
5857 return( PSA_SUCCESS );
5858 default:
5859 return( PSA_ERROR_INVALID_ARGUMENT );
5860 }
5861}
John Durkop07cc04a2020-11-16 22:08:34 -08005862#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005863
John Durkop07cc04a2020-11-16 22:08:34 -08005864#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5865 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005866static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5867 const uint8_t *data,
5868 size_t data_length )
5869{
5870 if( prf->state != TLS12_PRF_STATE_INIT )
5871 return( PSA_ERROR_BAD_STATE );
5872
Janos Follathd6dce9f2019-07-04 09:11:38 +01005873 if( data_length != 0 )
5874 {
5875 prf->seed = mbedtls_calloc( 1, data_length );
5876 if( prf->seed == NULL )
5877 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005878
Janos Follathd6dce9f2019-07-04 09:11:38 +01005879 memcpy( prf->seed, data, data_length );
5880 prf->seed_length = data_length;
5881 }
Janos Follathf08e2652019-06-13 09:05:41 +01005882
5883 prf->state = TLS12_PRF_STATE_SEED_SET;
5884
5885 return( PSA_SUCCESS );
5886}
5887
Janos Follath81550542019-06-13 14:26:34 +01005888static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5889 psa_algorithm_t hash_alg,
5890 const uint8_t *data,
5891 size_t data_length )
5892{
5893 psa_status_t status;
5894 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5895 return( PSA_ERROR_BAD_STATE );
5896
5897 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5898 if( status != PSA_SUCCESS )
5899 return( status );
5900
5901 prf->state = TLS12_PRF_STATE_KEY_SET;
5902
5903 return( PSA_SUCCESS );
5904}
5905
Janos Follath63028dd2019-06-13 09:15:47 +01005906static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5907 const uint8_t *data,
5908 size_t data_length )
5909{
5910 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5911 return( PSA_ERROR_BAD_STATE );
5912
Janos Follathd6dce9f2019-07-04 09:11:38 +01005913 if( data_length != 0 )
5914 {
5915 prf->label = mbedtls_calloc( 1, data_length );
5916 if( prf->label == NULL )
5917 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005918
Janos Follathd6dce9f2019-07-04 09:11:38 +01005919 memcpy( prf->label, data, data_length );
5920 prf->label_length = data_length;
5921 }
Janos Follath63028dd2019-06-13 09:15:47 +01005922
5923 prf->state = TLS12_PRF_STATE_LABEL_SET;
5924
5925 return( PSA_SUCCESS );
5926}
5927
Janos Follathb03233e2019-06-11 15:30:30 +01005928static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5929 psa_algorithm_t hash_alg,
5930 psa_key_derivation_step_t step,
5931 const uint8_t *data,
5932 size_t data_length )
5933{
Janos Follathb03233e2019-06-11 15:30:30 +01005934 switch( step )
5935 {
Janos Follathf08e2652019-06-13 09:05:41 +01005936 case PSA_KEY_DERIVATION_INPUT_SEED:
5937 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005938 case PSA_KEY_DERIVATION_INPUT_SECRET:
5939 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005940 case PSA_KEY_DERIVATION_INPUT_LABEL:
5941 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005942 default:
5943 return( PSA_ERROR_INVALID_ARGUMENT );
5944 }
5945}
John Durkop07cc04a2020-11-16 22:08:34 -08005946#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5947 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5948
5949#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5950static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5951 psa_tls12_prf_key_derivation_t *prf,
5952 psa_algorithm_t hash_alg,
5953 const uint8_t *data,
5954 size_t data_length )
5955{
5956 psa_status_t status;
5957 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5958 uint8_t *cur = pms;
5959
5960 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5961 return( PSA_ERROR_INVALID_ARGUMENT );
5962
5963 /* Quoting RFC 4279, Section 2:
5964 *
5965 * The premaster secret is formed as follows: if the PSK is N octets
5966 * long, concatenate a uint16 with the value N, N zero octets, a second
5967 * uint16 with the value N, and the PSK itself.
5968 */
5969
5970 *cur++ = ( data_length >> 8 ) & 0xff;
5971 *cur++ = ( data_length >> 0 ) & 0xff;
5972 memset( cur, 0, data_length );
5973 cur += data_length;
5974 *cur++ = pms[0];
5975 *cur++ = pms[1];
5976 memcpy( cur, data, data_length );
5977 cur += data_length;
5978
5979 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5980
5981 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5982 return( status );
5983}
Janos Follath6660f0e2019-06-17 08:44:03 +01005984
5985static psa_status_t psa_tls12_prf_psk_to_ms_input(
5986 psa_tls12_prf_key_derivation_t *prf,
5987 psa_algorithm_t hash_alg,
5988 psa_key_derivation_step_t step,
5989 const uint8_t *data,
5990 size_t data_length )
5991{
5992 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005993 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005994 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5995 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005996 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005997
5998 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5999}
John Durkop07cc04a2020-11-16 22:08:34 -08006000#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006001
Gilles Peskineb8965192019-09-24 16:21:10 +02006002/** Check whether the given key type is acceptable for the given
6003 * input step of a key derivation.
6004 *
6005 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6006 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6007 * Both secret and non-secret inputs can alternatively have the type
6008 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6009 * that the input was passed as a buffer rather than via a key object.
6010 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006011static int psa_key_derivation_check_input_type(
6012 psa_key_derivation_step_t step,
6013 psa_key_type_t key_type )
6014{
6015 switch( step )
6016 {
6017 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02006018 if( key_type == PSA_KEY_TYPE_DERIVE )
6019 return( PSA_SUCCESS );
6020 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006021 return( PSA_SUCCESS );
6022 break;
6023 case PSA_KEY_DERIVATION_INPUT_LABEL:
6024 case PSA_KEY_DERIVATION_INPUT_SALT:
6025 case PSA_KEY_DERIVATION_INPUT_INFO:
6026 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02006027 if( key_type == PSA_KEY_TYPE_RAW_DATA )
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 }
6033 return( PSA_ERROR_INVALID_ARGUMENT );
6034}
6035
Janos Follathb80a94e2019-06-12 15:54:46 +01006036static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006037 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006038 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006039 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006040 const uint8_t *data,
6041 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006042{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006043 psa_status_t status;
6044 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6045
6046 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006047 if( status != PSA_SUCCESS )
6048 goto exit;
6049
John Durkopd0321952020-10-29 21:37:36 -07006050#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006051 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006052 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006053 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006054 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006055 step, data, data_length );
6056 }
John Durkop07cc04a2020-11-16 22:08:34 -08006057 else
6058#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6059#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6060 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006061 {
Janos Follathb03233e2019-06-11 15:30:30 +01006062 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6063 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6064 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006065 }
John Durkop07cc04a2020-11-16 22:08:34 -08006066 else
6067#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6068#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6069 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006070 {
6071 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6072 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6073 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006074 }
6075 else
John Durkop07cc04a2020-11-16 22:08:34 -08006076#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006077 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006078 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006079 return( PSA_ERROR_BAD_STATE );
6080 }
6081
Gilles Peskine224b0d62019-09-23 18:13:17 +02006082exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006083 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006084 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006085 return( status );
6086}
6087
Janos Follath51f4a0f2019-06-14 11:35:55 +01006088psa_status_t psa_key_derivation_input_bytes(
6089 psa_key_derivation_operation_t *operation,
6090 psa_key_derivation_step_t step,
6091 const uint8_t *data,
6092 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006093{
Gilles Peskineb8965192019-09-24 16:21:10 +02006094 return( psa_key_derivation_input_internal( operation, step,
6095 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006096 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006097}
6098
Janos Follath51f4a0f2019-06-14 11:35:55 +01006099psa_status_t psa_key_derivation_input_key(
6100 psa_key_derivation_operation_t *operation,
6101 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006102 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006103{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006104 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006105 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006106 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006107
Ronald Cron5c522922020-11-14 16:35:34 +01006108 status = psa_get_and_lock_transparent_key_slot_with_policy(
6109 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006110 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006111 {
6112 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006113 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006114 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006115
6116 /* Passing a key object as a SECRET input unlocks the permission
6117 * to output to a key object. */
6118 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6119 operation->can_output_key = 1;
6120
Ronald Cronf95a2b12020-10-22 15:24:49 +02006121 status = psa_key_derivation_input_internal( operation,
6122 step, slot->attr.type,
6123 slot->data.key.data,
6124 slot->data.key.bytes );
6125
Ronald Cron5c522922020-11-14 16:35:34 +01006126 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006127
Ronald Cron5c522922020-11-14 16:35:34 +01006128 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006129}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006130
6131
6132
6133/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006134/* Key agreement */
6135/****************************************************************/
6136
John Durkop6ba40d12020-11-10 08:50:04 -08006137#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006138static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6139 size_t peer_key_length,
6140 const mbedtls_ecp_keypair *our_key,
6141 uint8_t *shared_secret,
6142 size_t shared_secret_size,
6143 size_t *shared_secret_length )
6144{
Steven Cooremand4867872020-08-05 16:31:39 +02006145 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006146 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006147 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006148 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006149 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006150 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006151
Steven Cooreman4fed4552020-08-03 14:46:03 +02006152 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6153 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006154 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006155 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006156 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006157 goto exit;
6158
Jaeden Amero97271b32019-01-10 19:38:51 +00006159 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006160 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006161 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006162 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006163 status = mbedtls_to_psa_error(
6164 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6165 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(
6169 mbedtls_ecdh_calc_secret( &ecdh,
6170 shared_secret_length,
6171 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006172 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006173 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006174 if( status != PSA_SUCCESS )
6175 goto exit;
6176 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6177 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006178
6179exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006180 if( status != PSA_SUCCESS )
6181 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006182 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006183 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006184 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006185
Jaeden Amero97271b32019-01-10 19:38:51 +00006186 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006187}
John Durkop6ba40d12020-11-10 08:50:04 -08006188#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006189
Gilles Peskine01d718c2018-09-18 12:01:02 +02006190#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6191
Gilles Peskine0216fe12019-04-11 21:23:21 +02006192static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6193 psa_key_slot_t *private_key,
6194 const uint8_t *peer_key,
6195 size_t peer_key_length,
6196 uint8_t *shared_secret,
6197 size_t shared_secret_size,
6198 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006199{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006200 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006201 {
John Durkop6ba40d12020-11-10 08:50:04 -08006202#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006203 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006204 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006205 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006206 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006207 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006208 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006209 private_key->data.key.data,
6210 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006211 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006212 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006213 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006214 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006215 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006216 shared_secret, shared_secret_size,
6217 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006218 mbedtls_ecp_keypair_free( ecp );
6219 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006220 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006221#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006222 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006223 (void) private_key;
6224 (void) peer_key;
6225 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006226 (void) shared_secret;
6227 (void) shared_secret_size;
6228 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006229 return( PSA_ERROR_NOT_SUPPORTED );
6230 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006231}
6232
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006233/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006234 * to potentially free embedded data structures and wipe confidential data.
6235 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006236static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006237 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006238 psa_key_slot_t *private_key,
6239 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006240 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006241{
6242 psa_status_t status;
6243 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6244 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006245 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006246
6247 /* Step 1: run the secret agreement algorithm to generate the shared
6248 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006249 status = psa_key_agreement_raw_internal( ka_alg,
6250 private_key,
6251 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006252 shared_secret,
6253 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006254 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006255 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006256 goto exit;
6257
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006258 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006259 * the shared secret. A shared secret is permitted wherever a key
6260 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006261 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006262 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006263 shared_secret,
6264 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006265exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006266 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006267 return( status );
6268}
6269
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006270psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006271 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006272 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006273 const uint8_t *peer_key,
6274 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006275{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006276 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006277 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006278 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006279
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006280 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006281 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006282 status = psa_get_and_lock_transparent_key_slot_with_policy(
6283 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006284 if( status != PSA_SUCCESS )
6285 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006286 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006287 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006288 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006289 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006290 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006291 else
6292 {
6293 /* If a private key has been added as SECRET, we allow the derived
6294 * key material to be used as a key in PSA Crypto. */
6295 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6296 operation->can_output_key = 1;
6297 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006298
Ronald Cron5c522922020-11-14 16:35:34 +01006299 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006300
Ronald Cron5c522922020-11-14 16:35:34 +01006301 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006302}
6303
Gilles Peskinebe697d82019-05-16 18:00:41 +02006304psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006305 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006306 const uint8_t *peer_key,
6307 size_t peer_key_length,
6308 uint8_t *output,
6309 size_t output_size,
6310 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006311{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006312 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006313 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006314 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006315
6316 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6317 {
6318 status = PSA_ERROR_INVALID_ARGUMENT;
6319 goto exit;
6320 }
Ronald Cron5c522922020-11-14 16:35:34 +01006321 status = psa_get_and_lock_transparent_key_slot_with_policy(
6322 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006323 if( status != PSA_SUCCESS )
6324 goto exit;
6325
6326 status = psa_key_agreement_raw_internal( alg, slot,
6327 peer_key, peer_key_length,
6328 output, output_size,
6329 output_length );
6330
6331exit:
6332 if( status != PSA_SUCCESS )
6333 {
6334 /* If an error happens and is not handled properly, the output
6335 * may be used as a key to protect sensitive data. Arrange for such
6336 * a key to be random, which is likely to result in decryption or
6337 * verification errors. This is better than filling the buffer with
6338 * some constant data such as zeros, which would result in the data
6339 * being protected with a reproducible, easily knowable key.
6340 */
6341 psa_generate_random( output, output_size );
6342 *output_length = output_size;
6343 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006344
Ronald Cron5c522922020-11-14 16:35:34 +01006345 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006346
Ronald Cron5c522922020-11-14 16:35:34 +01006347 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006348}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006349
6350
Gilles Peskine30524eb2020-11-13 17:02:26 +01006351
Gilles Peskine86a440b2018-11-12 18:39:40 +01006352/****************************************************************/
6353/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006354/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006355
Gilles Peskine30524eb2020-11-13 17:02:26 +01006356/** Initialize the PSA random generator.
6357 */
6358static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6359{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006360#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6361 memset( rng, 0, sizeof( *rng ) );
6362#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6363
Gilles Peskine30524eb2020-11-13 17:02:26 +01006364 /* Set default configuration if
6365 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6366 if( rng->entropy_init == NULL )
6367 rng->entropy_init = mbedtls_entropy_init;
6368 if( rng->entropy_free == NULL )
6369 rng->entropy_free = mbedtls_entropy_free;
6370
6371 rng->entropy_init( &rng->entropy );
6372#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6373 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6374 /* The PSA entropy injection feature depends on using NV seed as an entropy
6375 * source. Add NV seed as an entropy source for PSA entropy injection. */
6376 mbedtls_entropy_add_source( &rng->entropy,
6377 mbedtls_nv_seed_poll, NULL,
6378 MBEDTLS_ENTROPY_BLOCK_SIZE,
6379 MBEDTLS_ENTROPY_SOURCE_STRONG );
6380#endif
6381
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006382 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006383#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006384}
6385
6386/** Deinitialize the PSA random generator.
6387 */
6388static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6389{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006390#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6391 memset( rng, 0, sizeof( *rng ) );
6392#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006393 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006394 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006395#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006396}
6397
6398/** Seed the PSA random generator.
6399 */
6400static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6401{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006402#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6403 /* Do nothing: the external RNG seeds itself. */
6404 (void) rng;
6405 return( PSA_SUCCESS );
6406#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006407 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006408 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6409 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006410 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006411#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006412}
6413
Gilles Peskinee59236f2018-01-27 23:32:46 +01006414psa_status_t psa_generate_random( uint8_t *output,
6415 size_t output_size )
6416{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006417 GUARD_MODULE_INITIALIZED;
6418
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006419#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6420
6421 size_t output_length = 0;
6422 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6423 output, output_size,
6424 &output_length );
6425 if( status != PSA_SUCCESS )
6426 return( status );
6427 /* Breaking up a request into smaller chunks is currently not supported
6428 * for the extrernal RNG interface. */
6429 if( output_length != output_size )
6430 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6431 return( PSA_SUCCESS );
6432
6433#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6434
Gilles Peskine71ddab92021-01-04 21:01:07 +01006435 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006436 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006437 size_t request_size =
6438 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6439 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6440 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006441 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6442 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006443 if( ret != 0 )
6444 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006445 output_size -= request_size;
6446 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006447 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006448 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006449#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006450}
6451
Gilles Peskine8814fc42020-12-14 15:33:44 +01006452/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006453 *
6454 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6455 * `psa_generate_random(...)`. The state parameter is ignored since the
6456 * PSA API doesn't support passing an explicit state.
6457 *
6458 * In the non-external case, psa_generate_random() calls an
6459 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6460 * and semantics as mbedtls_psa_get_random(). As an optimization,
6461 * instead of doing this back-and-forth between the PSA API and the
6462 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6463 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006464 */
6465#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6466int mbedtls_psa_get_random( void *p_rng,
6467 unsigned char *output,
6468 size_t output_size )
6469{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006470 /* This function takes a pointer to the RNG state because that's what
6471 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6472 * its own state internally and doesn't let the caller access that state.
6473 * So we just ignore the state parameter, and in practice we'll pass
6474 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006475 (void) p_rng;
6476 psa_status_t status = psa_generate_random( output, output_size );
6477 if( status == PSA_SUCCESS )
6478 return( 0 );
6479 else
6480 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6481}
6482#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6483
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006484#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6485#include "mbedtls/entropy_poll.h"
6486
Gilles Peskine7228da22019-07-15 11:06:15 +02006487psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006488 size_t seed_size )
6489{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006490 if( global_data.initialized )
6491 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006492
6493 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6494 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6495 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6496 return( PSA_ERROR_INVALID_ARGUMENT );
6497
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006498 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006499}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006500#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006501
John Durkop07cc04a2020-11-16 22:08:34 -08006502#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006503static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6504 size_t domain_parameters_size,
6505 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006506{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006507 size_t i;
6508 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006509
Gilles Peskinee56e8782019-04-26 17:34:02 +02006510 if( domain_parameters_size == 0 )
6511 {
6512 *exponent = 65537;
6513 return( PSA_SUCCESS );
6514 }
6515
6516 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6517 * support values that fit in a 32-bit integer, which is larger than
6518 * int on just about every platform anyway. */
6519 if( domain_parameters_size > sizeof( acc ) )
6520 return( PSA_ERROR_NOT_SUPPORTED );
6521 for( i = 0; i < domain_parameters_size; i++ )
6522 acc = ( acc << 8 ) | domain_parameters[i];
6523 if( acc > INT_MAX )
6524 return( PSA_ERROR_NOT_SUPPORTED );
6525 *exponent = acc;
6526 return( PSA_SUCCESS );
6527}
John Durkop07cc04a2020-11-16 22:08:34 -08006528#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006529
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006530static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006531 psa_key_slot_t *slot, size_t bits,
6532 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006533{
Gilles Peskine8e338702019-07-30 20:06:31 +02006534 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006535
Gilles Peskinee56e8782019-04-26 17:34:02 +02006536 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006537 return( PSA_ERROR_INVALID_ARGUMENT );
6538
Gilles Peskinee59236f2018-01-27 23:32:46 +01006539 if( key_type_is_raw_bytes( type ) )
6540 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006541 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006542
6543 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006544 if( status != PSA_SUCCESS )
6545 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006546
6547 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006548 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6549 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006550 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006551
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006552 status = psa_generate_random( slot->data.key.data,
6553 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006554 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006555 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006556
6557 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006558#if defined(MBEDTLS_DES_C)
6559 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006560 psa_des_set_key_parity( slot->data.key.data,
6561 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006562#endif /* MBEDTLS_DES_C */
6563 }
6564 else
6565
John Durkop07cc04a2020-11-16 22:08:34 -08006566#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006567 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006568 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006569 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006571 int exponent;
6572 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006573 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6574 return( PSA_ERROR_NOT_SUPPORTED );
6575 /* Accept only byte-aligned keys, for the same reasons as
6576 * in psa_import_rsa_key(). */
6577 if( bits % 8 != 0 )
6578 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006579 status = psa_read_rsa_exponent( domain_parameters,
6580 domain_parameters_size,
6581 &exponent );
6582 if( status != PSA_SUCCESS )
6583 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006584 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6585 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006586 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006587 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006588 (unsigned int) bits,
6589 exponent );
6590 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006591 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006592
6593 /* Make sure to always have an export representation available */
6594 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6595
Steven Cooreman75b74362020-07-28 14:30:13 +02006596 status = psa_allocate_buffer_to_slot( slot, bytes );
6597 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006598 {
6599 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006600 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006601 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006602
6603 status = psa_export_rsa_key( type,
6604 &rsa,
6605 slot->data.key.data,
6606 bytes,
6607 &slot->data.key.bytes );
6608 mbedtls_rsa_free( &rsa );
6609 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006610 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006611 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006612 }
6613 else
John Durkop07cc04a2020-11-16 22:08:34 -08006614#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006615
John Durkop9814fa22020-11-04 12:28:15 -08006616#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006617 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006618 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006619 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006620 mbedtls_ecp_group_id grp_id =
6621 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006622 const mbedtls_ecp_curve_info *curve_info =
6623 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006624 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006625 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006626 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006627 return( PSA_ERROR_NOT_SUPPORTED );
6628 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6629 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006630 mbedtls_ecp_keypair_init( &ecp );
6631 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006632 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006633 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006634 if( ret != 0 )
6635 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006636 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006637 return( mbedtls_to_psa_error( ret ) );
6638 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006639
6640
6641 /* Make sure to always have an export representation available */
6642 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006643 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6644 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006645 {
6646 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006647 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006648 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006649
6650 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006651 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6652
6653 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006654 if( status != PSA_SUCCESS ) {
6655 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006656 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006657 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006658 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006659 }
6660 else
John Durkop9814fa22020-11-04 12:28:15 -08006661#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006662 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006663 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006664 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006665
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006666 return( PSA_SUCCESS );
6667}
Darryl Green0c6575a2018-11-07 16:05:30 +00006668
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006669psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006670 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006671{
6672 psa_status_t status;
6673 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006674 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006675
Ronald Cron81709fc2020-11-14 12:10:32 +01006676 *key = MBEDTLS_SVC_KEY_ID_INIT;
6677
Gilles Peskine0f84d622019-09-12 19:03:13 +02006678 /* Reject any attempt to create a zero-length key so that we don't
6679 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6680 if( psa_get_key_bits( attributes ) == 0 )
6681 return( PSA_ERROR_INVALID_ARGUMENT );
6682
Ronald Cron81709fc2020-11-14 12:10:32 +01006683 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6684 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006685 if( status != PSA_SUCCESS )
6686 goto exit;
6687
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006688 status = psa_driver_wrapper_generate_key( attributes,
6689 slot );
6690 if( status != PSA_ERROR_NOT_SUPPORTED ||
6691 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6692 goto exit;
6693
6694 status = psa_generate_key_internal(
6695 slot, attributes->core.bits,
6696 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006697
6698exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006699 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006700 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006701 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006702 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006703
Darryl Green0c6575a2018-11-07 16:05:30 +00006704 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006705}
6706
6707
Gilles Peskinee59236f2018-01-27 23:32:46 +01006708
6709/****************************************************************/
6710/* Module setup */
6711/****************************************************************/
6712
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006713#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006714psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6715 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6716 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6717{
6718 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6719 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006720 global_data.rng.entropy_init = entropy_init;
6721 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006722 return( PSA_SUCCESS );
6723}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006724#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006725
Gilles Peskinee59236f2018-01-27 23:32:46 +01006726void mbedtls_psa_crypto_free( void )
6727{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006728 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006729 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6730 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006731 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006732 }
6733 /* Wipe all remaining data, including configuration.
6734 * In particular, this sets all state indicator to the value
6735 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006736 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006737#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006738 /* Unregister all secure element drivers, so that we restart from
6739 * a pristine state. */
6740 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006741#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006742}
6743
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006744#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6745/** Recover a transaction that was interrupted by a power failure.
6746 *
6747 * This function is called during initialization, before psa_crypto_init()
6748 * returns. If this function returns a failure status, the initialization
6749 * fails.
6750 */
6751static psa_status_t psa_crypto_recover_transaction(
6752 const psa_crypto_transaction_t *transaction )
6753{
6754 switch( transaction->unknown.type )
6755 {
6756 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6757 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006758 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006759 * is implemented.
6760 * https://github.com/ARMmbed/mbed-crypto/issues/218
6761 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006762 default:
6763 /* We found an unsupported transaction in the storage.
6764 * We don't know what state the storage is in. Give up. */
6765 return( PSA_ERROR_STORAGE_FAILURE );
6766 }
6767}
6768#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6769
Gilles Peskinee59236f2018-01-27 23:32:46 +01006770psa_status_t psa_crypto_init( void )
6771{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006772 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006773
Gilles Peskinec6b69072018-11-20 21:42:52 +01006774 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006775 if( global_data.initialized != 0 )
6776 return( PSA_SUCCESS );
6777
Gilles Peskine30524eb2020-11-13 17:02:26 +01006778 /* Initialize and seed the random generator. */
6779 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006780 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006781 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006782 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006783 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006784 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006785
Gilles Peskine66fb1262018-12-10 16:29:04 +01006786 status = psa_initialize_key_slots( );
6787 if( status != PSA_SUCCESS )
6788 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006789
Gilles Peskined9348f22019-10-01 15:22:29 +02006790#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6791 status = psa_init_all_se_drivers( );
6792 if( status != PSA_SUCCESS )
6793 goto exit;
6794#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6795
Gilles Peskine4b734222019-07-24 15:56:31 +02006796#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006797 status = psa_crypto_load_transaction( );
6798 if( status == PSA_SUCCESS )
6799 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006800 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6801 if( status != PSA_SUCCESS )
6802 goto exit;
6803 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006804 }
6805 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6806 {
6807 /* There's no transaction to complete. It's all good. */
6808 status = PSA_SUCCESS;
6809 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006810#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006811
Gilles Peskinec6b69072018-11-20 21:42:52 +01006812 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006813 global_data.initialized = 1;
6814
6815exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006816 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006817 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006818 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006819}
6820
6821#endif /* MBEDTLS_PSA_CRYPTO_C */