blob: fa27a64a6cdc839f0ec4720bb483a0ebac5dde81 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb2b64d32020-12-14 16:43:58 +010043#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010044
Gilles Peskineb46bef22019-07-30 21:32:04 +020045#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020049#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#define mbedtls_calloc calloc
51#define mbedtls_free free
52#endif
53
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010054#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000057#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020058#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/blowfish.h"
60#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
72#include "mbedtls/md2.h"
73#include "mbedtls/md4.h"
74#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010075#include "mbedtls/md.h"
76#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010079#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000080#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
86#include "mbedtls/xtea.h"
87
Gilles Peskine996deb12018-08-01 15:45:45 +020088#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
89
Gilles Peskine9ef733f2018-02-07 21:05:37 +010090/* constant-time buffer comparison */
91static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
92{
93 size_t i;
94 unsigned char diff = 0;
95
96 for( i = 0; i < n; i++ )
97 diff |= a[i] ^ b[i];
98
99 return( diff );
100}
101
102
103
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100104/****************************************************************/
105/* Global data, support functions and library management */
106/****************************************************************/
107
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108static int key_type_is_raw_bytes( psa_key_type_t type )
109{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200110 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111}
112
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100113/* Values for psa_global_data_t::rng_state */
114#define RNG_NOT_INITIALIZED 0
115#define RNG_INITIALIZED 1
116#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100117
Gilles Peskine2d277862018-06-18 15:41:12 +0200118typedef struct
119{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100120 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100127#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
128mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
129 &global_data.rng.drbg;
130#endif
131
itayzafrir0adf0fc2018-09-06 16:24:41 +0300132#define GUARD_MODULE_INITIALIZED \
133 if( global_data.initialized == 0 ) \
134 return( PSA_ERROR_BAD_STATE );
135
Steven Cooreman01164162020-07-20 15:31:37 +0200136psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100138 /* Mbed TLS error codes can combine a high-level error code and a
139 * low-level error code. The low-level error usually reflects the
140 * root cause better, so dispatch on that preferably. */
141 int low_level_ret = - ( -ret & 0x007f );
142 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 {
144 case 0:
145 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100146
147 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
148 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
149 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
150 return( PSA_ERROR_NOT_SUPPORTED );
151 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
152 return( PSA_ERROR_HARDWARE_FAILURE );
153
154 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
155 return( PSA_ERROR_HARDWARE_FAILURE );
156
Gilles Peskine9a944802018-06-21 09:35:35 +0200157 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
158 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
159 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
160 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
161 case MBEDTLS_ERR_ASN1_INVALID_DATA:
162 return( PSA_ERROR_INVALID_ARGUMENT );
163 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
164 return( PSA_ERROR_INSUFFICIENT_MEMORY );
165 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
166 return( PSA_ERROR_BUFFER_TOO_SMALL );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
Jaeden Amero93e21112019-02-20 13:57:28 +0000178#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000179 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
181 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
182#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_CCM_BAD_INPUT:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190 case MBEDTLS_ERR_CCM_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
193 return( PSA_ERROR_HARDWARE_FAILURE );
194
Gilles Peskine26869f22019-05-06 15:25:00 +0200195 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197
198 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
199 return( PSA_ERROR_BAD_STATE );
200 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
201 return( PSA_ERROR_INVALID_SIGNATURE );
202
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
204 return( PSA_ERROR_NOT_SUPPORTED );
205 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
206 return( PSA_ERROR_INVALID_ARGUMENT );
207 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
208 return( PSA_ERROR_INSUFFICIENT_MEMORY );
209 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
210 return( PSA_ERROR_INVALID_PADDING );
211 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200212 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
214 return( PSA_ERROR_INVALID_SIGNATURE );
215 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200216 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
218 return( PSA_ERROR_HARDWARE_FAILURE );
219
220 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
Gilles Peskine82e57d12020-11-13 21:31:17 +0100223#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
224 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100225 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
226 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
229 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
230 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
231 return( PSA_ERROR_NOT_SUPPORTED );
232 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100235
236 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
237 return( PSA_ERROR_NOT_SUPPORTED );
238 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
Gilles Peskinee59236f2018-01-27 23:32:46 +0100241 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
242 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
243 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
244 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100245
246 case MBEDTLS_ERR_GCM_AUTH_FAILED:
247 return( PSA_ERROR_INVALID_SIGNATURE );
248 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200249 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
Gilles Peskine82e57d12020-11-13 21:31:17 +0100253#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
254 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100255 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
256 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
258 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
259 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
260 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
261 return( PSA_ERROR_NOT_SUPPORTED );
262 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
263 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
264#endif
265
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
267 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
268 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
269 return( PSA_ERROR_HARDWARE_FAILURE );
270
271 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MD_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
278 return( PSA_ERROR_STORAGE_FAILURE );
279 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
280 return( PSA_ERROR_HARDWARE_FAILURE );
281
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
283 return( PSA_ERROR_STORAGE_FAILURE );
284 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
289 return( PSA_ERROR_BUFFER_TOO_SMALL );
290 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
297 return( PSA_ERROR_INSUFFICIENT_MEMORY );
298
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
302 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
313 return( PSA_ERROR_NOT_PERMITTED );
314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
321 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
323 return( PSA_ERROR_HARDWARE_FAILURE );
324
Gilles Peskineff2d2002019-05-06 15:26:23 +0200325 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
328 return( PSA_ERROR_NOT_SUPPORTED );
329
Gilles Peskinea5905292018-02-07 20:59:33 +0100330 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
334 return( PSA_ERROR_INVALID_ARGUMENT );
335 case MBEDTLS_ERR_RSA_INVALID_PADDING:
336 return( PSA_ERROR_INVALID_PADDING );
337 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
340 return( PSA_ERROR_INVALID_ARGUMENT );
341 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
342 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200343 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100344 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
345 return( PSA_ERROR_INVALID_SIGNATURE );
346 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
347 return( PSA_ERROR_BUFFER_TOO_SMALL );
348 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100349 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
351 return( PSA_ERROR_NOT_SUPPORTED );
352 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
354
355 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
356 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
357 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
358 return( PSA_ERROR_HARDWARE_FAILURE );
359
360 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
361 return( PSA_ERROR_INVALID_ARGUMENT );
362 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
363 return( PSA_ERROR_HARDWARE_FAILURE );
364
itayzafrir5c753392018-05-08 11:18:38 +0300365 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300367 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300368 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
369 return( PSA_ERROR_BUFFER_TOO_SMALL );
370 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
371 return( PSA_ERROR_NOT_SUPPORTED );
372 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
373 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
374 return( PSA_ERROR_INVALID_SIGNATURE );
375 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
376 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100377 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
378 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300379 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
380 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100381
Janos Follatha13b9052019-11-22 12:48:59 +0000382 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
383 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300384
Gilles Peskinee59236f2018-01-27 23:32:46 +0100385 default:
David Saadab4ecc272019-02-14 13:48:10 +0200386 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100387 }
388}
389
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200390
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200391
392
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100393/****************************************************************/
394/* Key management */
395/****************************************************************/
396
Gilles Peskine73167e12019-07-12 23:44:37 +0200397#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
398static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
399{
Gilles Peskine8e338702019-07-30 20:06:31 +0200400 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200401}
402#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
403
John Durkop07cc04a2020-11-16 22:08:34 -0800404/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
405 * current test driver in key_management.c is using this function
406 * when accelerators are used for ECC key pair and public key.
407 * Once that dependency is resolved these guards can be removed.
408 */
John Durkop9814fa22020-11-04 12:28:15 -0800409#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800410 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
411 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
412 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100413mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100414 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200415{
416 switch( curve )
417 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100418 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100419 switch( byte_length )
420 {
421 case PSA_BITS_TO_BYTES( 192 ):
422 return( MBEDTLS_ECP_DP_SECP192R1 );
423 case PSA_BITS_TO_BYTES( 224 ):
424 return( MBEDTLS_ECP_DP_SECP224R1 );
425 case PSA_BITS_TO_BYTES( 256 ):
426 return( MBEDTLS_ECP_DP_SECP256R1 );
427 case PSA_BITS_TO_BYTES( 384 ):
428 return( MBEDTLS_ECP_DP_SECP384R1 );
429 case PSA_BITS_TO_BYTES( 521 ):
430 return( MBEDTLS_ECP_DP_SECP521R1 );
431 default:
432 return( MBEDTLS_ECP_DP_NONE );
433 }
434 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100435
Paul Elliott8ff510a2020-06-02 17:19:28 +0100436 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100437 switch( byte_length )
438 {
439 case PSA_BITS_TO_BYTES( 256 ):
440 return( MBEDTLS_ECP_DP_BP256R1 );
441 case PSA_BITS_TO_BYTES( 384 ):
442 return( MBEDTLS_ECP_DP_BP384R1 );
443 case PSA_BITS_TO_BYTES( 512 ):
444 return( MBEDTLS_ECP_DP_BP512R1 );
445 default:
446 return( MBEDTLS_ECP_DP_NONE );
447 }
448 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100449
Paul Elliott8ff510a2020-06-02 17:19:28 +0100450 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100451 switch( byte_length )
452 {
453 case PSA_BITS_TO_BYTES( 255 ):
454 return( MBEDTLS_ECP_DP_CURVE25519 );
455 case PSA_BITS_TO_BYTES( 448 ):
456 return( MBEDTLS_ECP_DP_CURVE448 );
457 default:
458 return( MBEDTLS_ECP_DP_NONE );
459 }
460 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100461
Paul Elliott8ff510a2020-06-02 17:19:28 +0100462 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100463 switch( byte_length )
464 {
465 case PSA_BITS_TO_BYTES( 192 ):
466 return( MBEDTLS_ECP_DP_SECP192K1 );
467 case PSA_BITS_TO_BYTES( 224 ):
468 return( MBEDTLS_ECP_DP_SECP224K1 );
469 case PSA_BITS_TO_BYTES( 256 ):
470 return( MBEDTLS_ECP_DP_SECP256K1 );
471 default:
472 return( MBEDTLS_ECP_DP_NONE );
473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
Gilles Peskine12313cd2018-06-20 00:20:32 +0200476 default:
477 return( MBEDTLS_ECP_DP_NONE );
478 }
479}
John Durkop9814fa22020-11-04 12:28:15 -0800480#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800481 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
482 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
483 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200484
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200485static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
486 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487{
488 /* Check that the bit size is acceptable for the key type */
489 switch( type )
490 {
491 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200493 case PSA_KEY_TYPE_DERIVE:
494 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495#if defined(MBEDTLS_AES_C)
496 case PSA_KEY_TYPE_AES:
497 if( bits != 128 && bits != 192 && bits != 256 )
498 return( PSA_ERROR_INVALID_ARGUMENT );
499 break;
500#endif
501#if defined(MBEDTLS_CAMELLIA_C)
502 case PSA_KEY_TYPE_CAMELLIA:
503 if( bits != 128 && bits != 192 && bits != 256 )
504 return( PSA_ERROR_INVALID_ARGUMENT );
505 break;
506#endif
507#if defined(MBEDTLS_DES_C)
508 case PSA_KEY_TYPE_DES:
509 if( bits != 64 && bits != 128 && bits != 192 )
510 return( PSA_ERROR_INVALID_ARGUMENT );
511 break;
512#endif
513#if defined(MBEDTLS_ARC4_C)
514 case PSA_KEY_TYPE_ARC4:
515 if( bits < 8 || bits > 2048 )
516 return( PSA_ERROR_INVALID_ARGUMENT );
517 break;
518#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200519#if defined(MBEDTLS_CHACHA20_C)
520 case PSA_KEY_TYPE_CHACHA20:
521 if( bits != 256 )
522 return( PSA_ERROR_INVALID_ARGUMENT );
523 break;
524#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200525 default:
526 return( PSA_ERROR_NOT_SUPPORTED );
527 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200528 if( bits % 8 != 0 )
529 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200531 return( PSA_SUCCESS );
532}
533
John Durkop0e005192020-10-31 22:06:54 -0700534#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
535 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
536 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800537 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
538 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
539 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200540
Gilles Peskine86a440b2018-11-12 18:39:40 +0100541/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
542 * that are not a multiple of 8) well. For example, there is only
543 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
544 * way to return the exact bit size of a key.
545 * To keep things simple, reject non-byte-aligned key sizes. */
546static psa_status_t psa_check_rsa_key_byte_aligned(
547 const mbedtls_rsa_context *rsa )
548{
549 mbedtls_mpi n;
550 psa_status_t status;
551 mbedtls_mpi_init( &n );
552 status = mbedtls_to_psa_error(
553 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
554 if( status == PSA_SUCCESS )
555 {
556 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
557 status = PSA_ERROR_NOT_SUPPORTED;
558 }
559 mbedtls_mpi_free( &n );
560 return( status );
561}
562
Steven Cooreman7f391872020-07-30 14:57:44 +0200563/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200564 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200565 * \param[in] type The type of key contained in \p data.
566 * \param[in] data The buffer from which to load the representation.
567 * \param[in] data_length The size in bytes of \p data.
568 * \param[out] p_rsa Returns a pointer to an RSA context on success.
569 * The caller is responsible for freeing both the
570 * contents of the context and the context itself
571 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200572 */
Ronald Cron90857082020-11-25 14:58:33 +0100573static psa_status_t mbedtls_psa_rsa_load_representation(
574 psa_key_type_t type, const uint8_t *data, size_t data_length,
575 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200576{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000577 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200578 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000579 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200580 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000581
582 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200583 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000584 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200585 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200586 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000587 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200588 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000589 if( status != PSA_SUCCESS )
590 goto exit;
591
592 /* We have something that the pkparse module recognizes. If it is a
593 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200594 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200595 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000596 status = PSA_ERROR_INVALID_ARGUMENT;
597 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200598 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000599
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000600 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
601 * supports non-byte-aligned key sizes, but not well. For example,
602 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200603 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000604 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
605 {
606 status = PSA_ERROR_NOT_SUPPORTED;
607 goto exit;
608 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200609 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200610 if( status != PSA_SUCCESS )
611 goto exit;
612
Steven Cooremana2371e52020-07-28 14:30:39 +0200613 /* Copy out the pointer to the RSA context, and reset the PK context
614 * such that pk_free doesn't free the RSA context we just grabbed. */
615 *p_rsa = mbedtls_pk_rsa( ctx );
616 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000617
618exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200619 mbedtls_pk_free( &ctx );
620 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200621}
622
John Durkop6ba40d12020-11-10 08:50:04 -0800623#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
624 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
625 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
626 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
627 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
628 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
629
630#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
631 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
632
Steven Cooremana01795d2020-07-24 22:48:15 +0200633/** Export an RSA key to export representation
634 *
635 * \param[in] type The type of key (public/private) to export
636 * \param[in] rsa The internal RSA representation from which to export
637 * \param[out] data The buffer to export to
638 * \param[in] data_size The length of the buffer to export to
639 * \param[out] data_length The amount of bytes written to \p data
640 */
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100641static psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
642 mbedtls_rsa_context *rsa,
643 uint8_t *data,
644 size_t data_size,
645 size_t *data_length )
Steven Cooremana01795d2020-07-24 22:48:15 +0200646{
647#if defined(MBEDTLS_PK_WRITE_C)
648 int ret;
649 mbedtls_pk_context pk;
650 uint8_t *pos = data + data_size;
651
652 mbedtls_pk_init( &pk );
653 pk.pk_info = &mbedtls_rsa_info;
654 pk.pk_ctx = rsa;
655
656 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200657 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
658 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200659 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
660 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
661 else
662 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
663
664 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200665 {
666 /* Clean up in case pk_write failed halfway through. */
667 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200668 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200669 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200670
671 /* The mbedtls_pk_xxx functions write to the end of the buffer.
672 * Move the data to the beginning and erase remaining data
673 * at the original location. */
674 if( 2 * (size_t) ret <= data_size )
675 {
676 memcpy( data, data + data_size - ret, ret );
677 memset( data + data_size - ret, 0, ret );
678 }
679 else if( (size_t) ret < data_size )
680 {
681 memmove( data, data + data_size - ret, ret );
682 memset( data + ret, 0, data_size - ret );
683 }
684
685 *data_length = ret;
686 return( PSA_SUCCESS );
687#else
688 (void) type;
689 (void) rsa;
690 (void) data;
691 (void) data_size;
692 (void) data_length;
693 return( PSA_ERROR_NOT_SUPPORTED );
694#endif /* MBEDTLS_PK_WRITE_C */
695}
696
697/** Import an RSA key from import representation to a slot
698 *
699 * \param[in,out] slot The slot where to store the export representation to
700 * \param[in] data The buffer containing the import representation
701 * \param[in] data_length The amount of bytes in \p data
702 */
703static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
704 const uint8_t *data,
705 size_t data_length )
706{
707 psa_status_t status;
708 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200709 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200710
Steven Cooremana01795d2020-07-24 22:48:15 +0200711 /* Parse input */
Ronald Cron90857082020-11-25 14:58:33 +0100712 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
713 data,
714 data_length,
715 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200716 if( status != PSA_SUCCESS )
717 goto exit;
718
719 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200720 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200721
Steven Cooreman75b74362020-07-28 14:30:13 +0200722 /* Re-export the data to PSA export format, such that we can store export
723 * representation in the key slot. Export representation in case of RSA is
724 * the smallest representation that's allowed as input, so a straight-up
725 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200726 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200727 if( output == NULL )
728 {
729 status = PSA_ERROR_INSUFFICIENT_MEMORY;
730 goto exit;
731 }
732
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100733 status = mbedtls_psa_rsa_export_key( slot->attr.type,
734 rsa,
735 output,
736 data_length,
737 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200738exit:
739 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200740 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200741 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200742
743 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000744 if( status != PSA_SUCCESS )
745 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200746 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000747 return( status );
748 }
749
Steven Cooremana01795d2020-07-24 22:48:15 +0200750 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100751 slot->key.data = output;
752 slot->key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000753
754 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200755}
John Durkop6ba40d12020-11-10 08:50:04 -0800756
757#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800758 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200759
John Durkop9814fa22020-11-04 12:28:15 -0800760#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800761 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
762 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
763 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
764 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200765/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200766 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200767 * \param[in] type The type of key contained in \p data.
768 * \param[in] data The buffer from which to load the representation.
769 * \param[in] data_length The size in bytes of \p data.
770 * \param[out] p_ecp Returns a pointer to an ECP context on success.
771 * The caller is responsible for freeing both the
772 * contents of the context and the context itself
773 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200774 */
Ronald Cron90857082020-11-25 14:58:33 +0100775static psa_status_t mbedtls_psa_ecp_load_representation(
776 psa_key_type_t type, const uint8_t *data, size_t data_length,
777 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100778{
779 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200780 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200781 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200782 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100783
Steven Cooreman4fed4552020-08-03 14:46:03 +0200784 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
785 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100786 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200787 /* A Weierstrass public key is represented as:
788 * - The byte 0x04;
789 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
790 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200791 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200792 */
793 if( ( data_length & 1 ) == 0 )
794 return( PSA_ERROR_INVALID_ARGUMENT );
795 curve_size = data_length / 2;
796
797 /* Montgomery public keys are represented in compressed format, meaning
798 * their curve_size is equal to the amount of input. */
799
800 /* Private keys are represented in uncompressed private random integer
801 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100802 }
803
Steven Cooreman6d839f02020-07-30 11:36:45 +0200804 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200805 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200806 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200807 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200808 mbedtls_ecp_keypair_init( ecp );
809
Gilles Peskine4cd32772019-12-02 20:49:42 +0100810 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200811 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
812 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100813 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200814 {
815 status = PSA_ERROR_INVALID_ARGUMENT;
816 goto exit;
817 }
818
Steven Cooremanacda8342020-07-24 23:09:52 +0200819 status = mbedtls_to_psa_error(
820 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
821 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200822 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200823
Steven Cooreman6d839f02020-07-30 11:36:45 +0200824 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200825 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200826 {
827 /* Load the public value. */
828 status = mbedtls_to_psa_error(
829 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200830 data,
831 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200832 if( status != PSA_SUCCESS )
833 goto exit;
834
835 /* Check that the point is on the curve. */
836 status = mbedtls_to_psa_error(
837 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
838 if( status != PSA_SUCCESS )
839 goto exit;
840 }
841 else
842 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200843 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200844 status = mbedtls_to_psa_error(
845 mbedtls_ecp_read_key( ecp->grp.id,
846 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200847 data,
848 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200849 if( status != PSA_SUCCESS )
850 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200851 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200852
853 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200854exit:
855 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200856 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200857 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200858 mbedtls_free( ecp );
859 }
860
Steven Cooreman29149862020-08-05 15:43:42 +0200861 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100862}
John Durkop6ba40d12020-11-10 08:50:04 -0800863#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
864 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
865 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
866 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
867 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000868
John Durkop6ba40d12020-11-10 08:50:04 -0800869#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
870 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200871/** Export an ECP key to export representation
872 *
873 * \param[in] type The type of key (public/private) to export
874 * \param[in] ecp The internal ECP representation from which to export
875 * \param[out] data The buffer to export to
876 * \param[in] data_size The length of the buffer to export to
877 * \param[out] data_length The amount of bytes written to \p data
878 */
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100879static psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type,
880 mbedtls_ecp_keypair *ecp,
881 uint8_t *data,
882 size_t data_size,
883 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200884{
Steven Cooremanacda8342020-07-24 23:09:52 +0200885 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000886
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200888 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200889 /* Check whether the public part is loaded */
890 if( mbedtls_ecp_is_zero( &ecp->Q ) )
891 {
892 /* Calculate the public key */
893 status = mbedtls_to_psa_error(
894 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100895 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200896 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200897 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200898 }
899
Steven Cooreman4fed4552020-08-03 14:46:03 +0200900 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200901 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
902 MBEDTLS_ECP_PF_UNCOMPRESSED,
903 data_length,
904 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200905 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200906 if( status != PSA_SUCCESS )
907 memset( data, 0, data_size );
908
Steven Cooreman29149862020-08-05 15:43:42 +0200909 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200910 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200911 else
912 {
Steven Cooreman29149862020-08-05 15:43:42 +0200913 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200914 return( PSA_ERROR_BUFFER_TOO_SMALL );
915
916 status = mbedtls_to_psa_error(
917 mbedtls_ecp_write_key( ecp,
918 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200919 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200920 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200921 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200922 else
923 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200924
925 return( status );
926 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200927}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200928
Steven Cooreman4fed4552020-08-03 14:46:03 +0200929/** Import an ECP key from import representation to a slot
930 *
931 * \param[in,out] slot The slot where to store the export representation to
932 * \param[in] data The buffer containing the import representation
933 * \param[in] data_length The amount of bytes in \p data
934 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200935static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
936 const uint8_t *data,
937 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100938{
Steven Cooremanacda8342020-07-24 23:09:52 +0200939 psa_status_t status;
940 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200941 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100942
Steven Cooremanacda8342020-07-24 23:09:52 +0200943 /* Parse input */
Ronald Cron90857082020-11-25 14:58:33 +0100944 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
945 data,
946 data_length,
947 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100948 if( status != PSA_SUCCESS )
949 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100950
Steven Cooremanacda8342020-07-24 23:09:52 +0200951 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200952 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200953 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200954 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200955
Steven Cooremanacda8342020-07-24 23:09:52 +0200956 /* Re-export the data to PSA export format. There is currently no support
957 * for other input formats then the export format, so this is a 1-1
958 * copy operation. */
959 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200960 if( output == NULL )
961 {
962 status = PSA_ERROR_INSUFFICIENT_MEMORY;
963 goto exit;
964 }
965
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100966 status = mbedtls_psa_ecp_export_key( slot->attr.type,
967 ecp,
968 output,
969 data_length,
970 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100971exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200972 /* Always free the PK object (will also free contained ECP context) */
973 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200974 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200975
976 /* Free the allocated buffer only on error. */
977 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100978 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200979 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200980 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100981 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200982
983 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100984 slot->key.data = output;
985 slot->key.bytes = data_length;
Steven Cooremanacda8342020-07-24 23:09:52 +0200986
987 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100988}
John Durkop9814fa22020-11-04 12:28:15 -0800989#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
990 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100991
Gilles Peskineb46bef22019-07-30 21:32:04 +0200992/** Return the size of the key in the given slot, in bits.
993 *
994 * \param[in] slot A key slot.
995 *
996 * \return The key size in bits, read from the metadata in the slot.
997 */
998static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
999{
1000 return( slot->attr.bits );
1001}
1002
Steven Cooreman75b74362020-07-28 14:30:13 +02001003/** Try to allocate a buffer to an empty key slot.
1004 *
1005 * \param[in,out] slot Key slot to attach buffer to.
1006 * \param[in] buffer_length Requested size of the buffer.
1007 *
1008 * \retval #PSA_SUCCESS
1009 * The buffer has been successfully allocated.
1010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1011 * Not enough memory was available for allocation.
1012 * \retval #PSA_ERROR_ALREADY_EXISTS
1013 * Trying to allocate a buffer to a non-empty key slot.
1014 */
1015static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
1016 size_t buffer_length )
1017{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001018 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001019 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001020
Ronald Cronea0f8a62020-11-25 17:52:23 +01001021 slot->key.data = mbedtls_calloc( 1, buffer_length );
1022 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001023 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001024
Ronald Cronea0f8a62020-11-25 17:52:23 +01001025 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001026 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001027}
1028
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001029psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1030 const uint8_t* data,
1031 size_t data_length )
1032{
1033 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1034 data_length );
1035 if( status != PSA_SUCCESS )
1036 return( status );
1037
Ronald Cronea0f8a62020-11-25 17:52:23 +01001038 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001039 return( PSA_SUCCESS );
1040}
1041
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001042/** Import key data into a slot.
1043 *
1044 * `slot->type` must have been set previously.
1045 * This function assumes that the slot does not contain any key material yet.
1046 * On failure, the slot content is unchanged.
1047 *
1048 * Persistent storage is not affected.
1049 *
1050 * \param[in,out] slot The key slot to import data into.
1051 * Its `type` field must have previously been set to
1052 * the desired key type.
1053 * It must not contain any key material yet.
1054 * \param[in] data Buffer containing the key material to parse and import.
1055 * \param data_length Size of \p data in bytes.
1056 *
1057 * \retval #PSA_SUCCESS
1058 * \retval #PSA_ERROR_INVALID_ARGUMENT
1059 * \retval #PSA_ERROR_NOT_SUPPORTED
1060 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1061 */
1062static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1063 const uint8_t *data,
1064 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001065{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001066 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001067 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001068
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001069 /* zero-length keys are never supported. */
1070 if( data_length == 0 )
1071 return( PSA_ERROR_NOT_SUPPORTED );
1072
Gilles Peskine8e338702019-07-30 20:06:31 +02001073 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001074 {
Steven Cooreman04524762020-10-13 17:43:44 +02001075 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001076
Steven Cooreman75b74362020-07-28 14:30:13 +02001077 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1078 if( data_length > SIZE_MAX / 8 )
1079 return( PSA_ERROR_NOT_SUPPORTED );
1080
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001081 /* Enforce a size limit, and in particular ensure that the bit
1082 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001083 if( bit_size > PSA_MAX_KEY_BITS )
1084 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001085
1086 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001087 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001088 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001089
1090 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001091 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001092 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001093 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001094
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001095 /* Write the actual key size to the slot.
1096 * psa_start_key_creation() wrote the size declared by the
1097 * caller, which may be 0 (meaning unspecified) or wrong. */
1098 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001099
1100 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001101 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001102 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001103 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001104 /* Try validation through accelerators first. */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001105 psa_key_attributes_t attributes = {
1106 .core = slot->attr
1107 };
Ronald Cron83282872020-11-22 14:02:39 +01001108
1109 status = psa_allocate_buffer_to_slot( slot, data_length );
1110 if( status != PSA_SUCCESS )
1111 return( status );
1112
1113 bit_size = slot->attr.bits;
1114 status = psa_driver_wrapper_import_key( &attributes,
1115 data, data_length,
1116 slot->key.data,
1117 slot->key.bytes,
1118 &slot->key.bytes,
1119 &bit_size );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001120 if( status == PSA_SUCCESS )
1121 {
Ronald Cron83282872020-11-22 14:02:39 +01001122 if( slot->attr.bits == 0 )
1123 slot->attr.bits = (psa_key_bits_t) bit_size;
1124 else if( bit_size != slot->attr.bits )
1125 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001126
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001127 return( PSA_SUCCESS );
1128 }
Ronald Cron83282872020-11-22 14:02:39 +01001129 else
1130 {
1131 if( status != PSA_ERROR_NOT_SUPPORTED )
1132 return( status );
1133 }
1134
1135 mbedtls_platform_zeroize( slot->key.data, data_length );
1136 mbedtls_free( slot->key.data );
1137 slot->key.data = NULL;
1138 slot->key.bytes = 0;
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001139
1140 /* Key format is not supported by any accelerator, try software fallback
1141 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001142#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1143 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001144 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1145 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001146 return( psa_import_ecp_key( slot, data, data_length ) );
1147 }
John Durkop9814fa22020-11-04 12:28:15 -08001148#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1149 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001150#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1151 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001152 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001153 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001154 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001155 }
John Durkop9814fa22020-11-04 12:28:15 -08001156#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1157 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001158
1159 /* Fell through the fallback as well, so have nothing else to try. */
1160 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001161 }
1162 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001163 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001164 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001165 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001166 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001167}
1168
Gilles Peskinef603c712019-01-19 13:40:11 +01001169/** Calculate the intersection of two algorithm usage policies.
1170 *
1171 * Return 0 (which allows no operation) on incompatibility.
1172 */
1173static psa_algorithm_t psa_key_policy_algorithm_intersection(
1174 psa_algorithm_t alg1,
1175 psa_algorithm_t alg2 )
1176{
Gilles Peskine549ea862019-05-22 11:45:59 +02001177 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001178 if( alg1 == alg2 )
1179 return( alg1 );
1180 /* If the policies are from the same hash-and-sign family, check
1181 * if one is a wildcard. If so the other has the specific algorithm. */
1182 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1183 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1184 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1185 {
1186 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1187 return( alg2 );
1188 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1189 return( alg1 );
1190 }
1191 /* If the policies are incompatible, allow nothing. */
1192 return( 0 );
1193}
1194
Gilles Peskined6f371b2019-05-10 19:33:38 +02001195static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1196 psa_algorithm_t requested_alg )
1197{
Gilles Peskine549ea862019-05-22 11:45:59 +02001198 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001199 if( requested_alg == policy_alg )
1200 return( 1 );
1201 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001202 * and requested_alg is the same hash-and-sign family with any hash,
1203 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001204 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1205 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1206 {
1207 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1208 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1209 }
Steven Cooremance48e852020-10-05 16:02:45 +02001210 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001211 * a key derivation with that key agreement should also be allowed. This
1212 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001213 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1214 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1215 {
1216 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1217 policy_alg );
1218 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001219 /* If it isn't permitted, it's forbidden. */
1220 return( 0 );
1221}
1222
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001223/** Test whether a policy permits an algorithm.
1224 *
1225 * The caller must test usage flags separately.
1226 */
1227static int psa_key_policy_permits( const psa_key_policy_t *policy,
1228 psa_algorithm_t alg )
1229{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001230 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1231 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001232}
1233
Gilles Peskinef603c712019-01-19 13:40:11 +01001234/** Restrict a key policy based on a constraint.
1235 *
1236 * \param[in,out] policy The policy to restrict.
1237 * \param[in] constraint The policy constraint to apply.
1238 *
1239 * \retval #PSA_SUCCESS
1240 * \c *policy contains the intersection of the original value of
1241 * \c *policy and \c *constraint.
1242 * \retval #PSA_ERROR_INVALID_ARGUMENT
1243 * \c *policy and \c *constraint are incompatible.
1244 * \c *policy is unchanged.
1245 */
1246static psa_status_t psa_restrict_key_policy(
1247 psa_key_policy_t *policy,
1248 const psa_key_policy_t *constraint )
1249{
1250 psa_algorithm_t intersection_alg =
1251 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001252 psa_algorithm_t intersection_alg2 =
1253 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001254 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1255 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001256 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1257 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001258 policy->usage &= constraint->usage;
1259 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001260 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001261 return( PSA_SUCCESS );
1262}
1263
Ronald Cron5c522922020-11-14 16:35:34 +01001264/** Get the description of a key given its identifier and policy constraints
1265 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001266 *
Ronald Cron5c522922020-11-14 16:35:34 +01001267 * The key must have allow all the usage flags set in \p usage. If \p alg is
1268 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001269 *
Ronald Cron5c522922020-11-14 16:35:34 +01001270 * In case of a persistent key, the function loads the description of the key
1271 * into a key slot if not already done.
1272 *
1273 * On success, the returned key slot is locked. It is the responsibility of
1274 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001275 */
Ronald Cron5c522922020-11-14 16:35:34 +01001276static psa_status_t psa_get_and_lock_key_slot_with_policy(
1277 mbedtls_svc_key_id_t key,
1278 psa_key_slot_t **p_slot,
1279 psa_key_usage_t usage,
1280 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001281{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1283 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001284
Ronald Cron5c522922020-11-14 16:35:34 +01001285 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001286 if( status != PSA_SUCCESS )
1287 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001288 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001289
1290 /* Enforce that usage policy for the key slot contains all the flags
1291 * required by the usage parameter. There is one exception: public
1292 * keys can always be exported, so we treat public key objects as
1293 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001294 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001295 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001296
1297 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001298 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001299 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001300
1301 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001302 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001303 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001304
Darryl Green06fd18d2018-07-16 11:21:11 +01001305 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001306
1307error:
1308 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001309 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001310
1311 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001312}
Darryl Green940d72c2018-07-13 13:18:51 +01001313
Ronald Cron5c522922020-11-14 16:35:34 +01001314/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001315 *
1316 * A transparent key is a key for which the key material is directly
1317 * available, as opposed to a key in a secure element.
1318 *
Ronald Cron5c522922020-11-14 16:35:34 +01001319 * This is a temporary function to use instead of
1320 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1321 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001322 *
Ronald Cron5c522922020-11-14 16:35:34 +01001323 * On success, the returned key slot is locked. It is the responsibility of the
1324 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001325 */
1326#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001327static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1328 mbedtls_svc_key_id_t key,
1329 psa_key_slot_t **p_slot,
1330 psa_key_usage_t usage,
1331 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001332{
Ronald Cron5c522922020-11-14 16:35:34 +01001333 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1334 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001335 if( status != PSA_SUCCESS )
1336 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001337
Gilles Peskineadad8132019-07-25 11:31:23 +02001338 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001339 {
Ronald Cron5c522922020-11-14 16:35:34 +01001340 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001341 *p_slot = NULL;
1342 return( PSA_ERROR_NOT_SUPPORTED );
1343 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001344
Gilles Peskine28f8f302019-07-24 13:30:31 +02001345 return( PSA_SUCCESS );
1346}
1347#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1348/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001349#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1350 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001351#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1352
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001353/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001354static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001355{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001356 /* Data pointer will always be either a valid pointer or NULL in an
1357 * initialized slot, so we can just free it. */
1358 if( slot->key.data != NULL )
1359 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1360
1361 mbedtls_free( slot->key.data );
1362 slot->key.data = NULL;
1363 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001364
1365 return( PSA_SUCCESS );
1366}
1367
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001368/** Completely wipe a slot in memory, including its policy.
1369 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001370psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001371{
1372 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001373
1374 /*
1375 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001376 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001377 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1378 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001379 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001380 */
Ronald Cron5c522922020-11-14 16:35:34 +01001381 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001382 {
1383#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001384 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001385#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001386 status = PSA_ERROR_CORRUPTION_DETECTED;
1387 }
1388
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001389 /* Multipart operations may still be using the key. This is safe
1390 * because all multipart operation objects are independent from
1391 * the key slot: if they need to access the key after the setup
1392 * phase, they have a copy of the key. Note that this means that
1393 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001394 /* At this point, key material and other type-specific content has
1395 * been wiped. Clear remaining metadata. We can call memset and not
1396 * zeroize because the metadata is not particularly sensitive. */
1397 memset( slot, 0, sizeof( *slot ) );
1398 return( status );
1399}
1400
Ronald Croncf56a0a2020-08-04 09:51:30 +02001401psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001402{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001403 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001404 psa_status_t status; /* status of the last operation */
1405 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001406#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1407 psa_se_drv_table_entry_t *driver;
1408#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001409
Ronald Croncf56a0a2020-08-04 09:51:30 +02001410 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001411 return( PSA_SUCCESS );
1412
Ronald Cronf2911112020-10-29 17:51:10 +01001413 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001414 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001415 * key, this will load the key description from persistent memory if not
1416 * done yet. We cannot avoid this loading as without it we don't know if
1417 * the key is operated by an SE or not and this information is needed by
1418 * the current implementation.
1419 */
Ronald Cron5c522922020-11-14 16:35:34 +01001420 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001421 if( status != PSA_SUCCESS )
1422 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001423
Ronald Cronf2911112020-10-29 17:51:10 +01001424 /*
1425 * If the key slot containing the key description is under access by the
1426 * library (apart from the present access), the key cannot be destroyed
1427 * yet. For the time being, just return in error. Eventually (to be
1428 * implemented), the key should be destroyed when all accesses have
1429 * stopped.
1430 */
Ronald Cron5c522922020-11-14 16:35:34 +01001431 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001432 {
Ronald Cron5c522922020-11-14 16:35:34 +01001433 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001434 return( PSA_ERROR_GENERIC_ERROR );
1435 }
1436
Gilles Peskine354f7672019-07-12 23:46:38 +02001437#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001438 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001439 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001440 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001441 /* For a key in a secure element, we need to do three things:
1442 * remove the key file in internal storage, destroy the
1443 * key inside the secure element, and update the driver's
1444 * persistent data. Start a transaction that will encompass these
1445 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001446 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001447 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001448 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001449 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001450 status = psa_crypto_save_transaction( );
1451 if( status != PSA_SUCCESS )
1452 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001453 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001454 /* We should still try to destroy the key in the secure
1455 * element and the key metadata in storage. This is especially
1456 * important if the error is that the storage is full.
1457 * But how to do it exactly without risking an inconsistent
1458 * state after a reset?
1459 * https://github.com/ARMmbed/mbed-crypto/issues/215
1460 */
1461 overall_status = status;
1462 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001463 }
1464
Ronald Cronea0f8a62020-11-25 17:52:23 +01001465 status = psa_destroy_se_key( driver,
1466 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001467 if( overall_status == PSA_SUCCESS )
1468 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001469 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001470#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1471
Darryl Greend49a4992018-06-18 17:27:26 +01001472#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001473 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001474 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001475 status = psa_destroy_persistent_key( slot->attr.id );
1476 if( overall_status == PSA_SUCCESS )
1477 overall_status = status;
1478
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001479 /* TODO: other slots may have a copy of the same key. We should
1480 * invalidate them.
1481 * https://github.com/ARMmbed/mbed-crypto/issues/214
1482 */
Darryl Greend49a4992018-06-18 17:27:26 +01001483 }
1484#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001485
Gilles Peskinefc762652019-07-22 19:30:34 +02001486#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1487 if( driver != NULL )
1488 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001489 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001490 if( overall_status == PSA_SUCCESS )
1491 overall_status = status;
1492 status = psa_crypto_stop_transaction( );
1493 if( overall_status == PSA_SUCCESS )
1494 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001495 }
1496#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1497
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001498#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1499exit:
1500#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001501 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001502 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1503 if( overall_status == PSA_SUCCESS )
1504 overall_status = status;
1505 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001506}
1507
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001508void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001509{
Gilles Peskineb699f072019-04-26 16:06:02 +02001510 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001511 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001512}
1513
Gilles Peskineb699f072019-04-26 16:06:02 +02001514psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1515 psa_key_type_t type,
1516 const uint8_t *data,
1517 size_t data_length )
1518{
1519 uint8_t *copy = NULL;
1520
1521 if( data_length != 0 )
1522 {
1523 copy = mbedtls_calloc( 1, data_length );
1524 if( copy == NULL )
1525 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1526 memcpy( copy, data, data_length );
1527 }
1528 /* After this point, this function is guaranteed to succeed, so it
1529 * can start modifying `*attributes`. */
1530
1531 if( attributes->domain_parameters != NULL )
1532 {
1533 mbedtls_free( attributes->domain_parameters );
1534 attributes->domain_parameters = NULL;
1535 attributes->domain_parameters_size = 0;
1536 }
1537
1538 attributes->domain_parameters = copy;
1539 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001540 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001541 return( PSA_SUCCESS );
1542}
1543
1544psa_status_t psa_get_key_domain_parameters(
1545 const psa_key_attributes_t *attributes,
1546 uint8_t *data, size_t data_size, size_t *data_length )
1547{
1548 if( attributes->domain_parameters_size > data_size )
1549 return( PSA_ERROR_BUFFER_TOO_SMALL );
1550 *data_length = attributes->domain_parameters_size;
1551 if( attributes->domain_parameters_size != 0 )
1552 memcpy( data, attributes->domain_parameters,
1553 attributes->domain_parameters_size );
1554 return( PSA_SUCCESS );
1555}
1556
John Durkop07cc04a2020-11-16 22:08:34 -08001557#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001558 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001559static psa_status_t psa_get_rsa_public_exponent(
1560 const mbedtls_rsa_context *rsa,
1561 psa_key_attributes_t *attributes )
1562{
1563 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001565 uint8_t *buffer = NULL;
1566 size_t buflen;
1567 mbedtls_mpi_init( &mpi );
1568
1569 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1570 if( ret != 0 )
1571 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001572 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1573 {
1574 /* It's the default value, which is reported as an empty string,
1575 * so there's nothing to do. */
1576 goto exit;
1577 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001578
1579 buflen = mbedtls_mpi_size( &mpi );
1580 buffer = mbedtls_calloc( 1, buflen );
1581 if( buffer == NULL )
1582 {
1583 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1584 goto exit;
1585 }
1586 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1587 if( ret != 0 )
1588 goto exit;
1589 attributes->domain_parameters = buffer;
1590 attributes->domain_parameters_size = buflen;
1591
1592exit:
1593 mbedtls_mpi_free( &mpi );
1594 if( ret != 0 )
1595 mbedtls_free( buffer );
1596 return( mbedtls_to_psa_error( ret ) );
1597}
John Durkop07cc04a2020-11-16 22:08:34 -08001598#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001599 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001600
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001601/** Retrieve all the publicly-accessible attributes of a key.
1602 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001603psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001604 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001605{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001606 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001607 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001608 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001609
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001610 psa_reset_key_attributes( attributes );
1611
Ronald Cron5c522922020-11-14 16:35:34 +01001612 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001613 if( status != PSA_SUCCESS )
1614 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001615
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001616 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001617 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1618 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1619
1620#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1621 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001622 psa_set_key_slot_number( attributes,
1623 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001624#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001625
Gilles Peskine8e338702019-07-30 20:06:31 +02001626 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001627 {
John Durkop07cc04a2020-11-16 22:08:34 -08001628#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001629 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001630 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001631 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001632#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001633 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001634 * is not yet implemented.
1635 * https://github.com/ARMmbed/mbed-crypto/issues/216
1636 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001637 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001638 break;
1639#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001640 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001641 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001642
Ronald Cron90857082020-11-25 14:58:33 +01001643 status = mbedtls_psa_rsa_load_representation(
1644 slot->attr.type,
1645 slot->key.data,
1646 slot->key.bytes,
1647 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001648 if( status != PSA_SUCCESS )
1649 break;
1650
Steven Cooremana2371e52020-07-28 14:30:39 +02001651 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001652 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001653 mbedtls_rsa_free( rsa );
1654 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001655 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001656 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001657#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001658 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001659 default:
1660 /* Nothing else to do. */
1661 break;
1662 }
1663
1664 if( status != PSA_SUCCESS )
1665 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001666
Ronald Cron5c522922020-11-14 16:35:34 +01001667 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001668
Ronald Cron5c522922020-11-14 16:35:34 +01001669 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001670}
1671
Gilles Peskinec8000c02019-08-02 20:15:51 +02001672#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1673psa_status_t psa_get_key_slot_number(
1674 const psa_key_attributes_t *attributes,
1675 psa_key_slot_number_t *slot_number )
1676{
1677 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1678 {
1679 *slot_number = attributes->slot_number;
1680 return( PSA_SUCCESS );
1681 }
1682 else
1683 return( PSA_ERROR_INVALID_ARGUMENT );
1684}
1685#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1686
Steven Cooremana01795d2020-07-24 22:48:15 +02001687static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1688 uint8_t *data,
1689 size_t data_size,
1690 size_t *data_length )
1691{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001692 if( slot->key.bytes > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001693 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Cronea0f8a62020-11-25 17:52:23 +01001694 memcpy( data, slot->key.data, slot->key.bytes );
1695 memset( data + slot->key.bytes, 0,
1696 data_size - slot->key.bytes );
1697 *data_length = slot->key.bytes;
Steven Cooremana01795d2020-07-24 22:48:15 +02001698 return( PSA_SUCCESS );
1699}
1700
Gilles Peskinef603c712019-01-19 13:40:11 +01001701static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1702 uint8_t *data,
1703 size_t data_size,
1704 size_t *data_length,
1705 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001706{
Gilles Peskine5d309672019-07-12 23:47:28 +02001707#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1708 const psa_drv_se_t *drv;
1709 psa_drv_se_context_t *drv_context;
1710#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1711
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001712 *data_length = 0;
1713
Gilles Peskine8e338702019-07-30 20:06:31 +02001714 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001715 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001716
Gilles Peskinef9168942019-09-12 19:20:29 +02001717 /* Reject a zero-length output buffer now, since this can never be a
1718 * valid key representation. This way we know that data must be a valid
1719 * pointer and we can do things like memset(data, ..., data_size). */
1720 if( data_size == 0 )
1721 return( PSA_ERROR_BUFFER_TOO_SMALL );
1722
Gilles Peskine5d309672019-07-12 23:47:28 +02001723#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001724 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001725 {
1726 psa_drv_se_export_key_t method;
1727 if( drv->key_management == NULL )
1728 return( PSA_ERROR_NOT_SUPPORTED );
1729 method = ( export_public_key ?
1730 drv->key_management->p_export_public :
1731 drv->key_management->p_export );
1732 if( method == NULL )
1733 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001734 return( method( drv_context,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001735 psa_key_slot_get_slot_number( slot ),
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001736 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001737 }
1738#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1739
Gilles Peskine8e338702019-07-30 20:06:31 +02001740 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001741 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001742 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001743 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001744 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1745 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001746 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001747 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001748 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001749 /* Exporting public -> public */
1750 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1751 }
1752 else if( !export_public_key )
1753 {
1754 /* Exporting private -> private */
1755 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1756 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001757
Steven Cooreman560c28a2020-07-24 23:20:24 +02001758 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001759 * so conversion is needed. Try the accelerators first. */
Ronald Cron84cc9942020-11-25 14:30:05 +01001760 psa_key_attributes_t attributes = {
1761 .core = slot->attr
1762 };
1763 psa_status_t status = psa_driver_wrapper_export_public_key(
1764 &attributes, slot->key.data, slot->key.bytes,
1765 data, data_size, data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +02001766
1767 if( status != PSA_ERROR_NOT_SUPPORTED ||
1768 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1769 return( status );
1770
Steven Cooreman560c28a2020-07-24 23:20:24 +02001771 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1772 {
John Durkop0e005192020-10-31 22:06:54 -07001773#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1774 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001775 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01001776 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
1777 slot->key.data,
1778 slot->key.bytes,
1779 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001780 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001781 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001782
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01001783 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
1784 rsa,
1785 data,
1786 data_size,
1787 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001788
Steven Cooremana2371e52020-07-28 14:30:39 +02001789 mbedtls_rsa_free( rsa );
1790 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001791
Steven Cooreman560c28a2020-07-24 23:20:24 +02001792 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001793#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001794 /* We don't know how to convert a private RSA key to public. */
1795 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001796#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1797 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001798 }
1799 else
Moran Pekera998bc62018-04-16 18:16:20 +03001800 {
John Durkop6ba40d12020-11-10 08:50:04 -08001801#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1802 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001803 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01001804 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
1805 slot->key.data,
1806 slot->key.bytes,
1807 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001808 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001809 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001810
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01001811 status = mbedtls_psa_ecp_export_key(
1812 PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1813 PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) ),
1814 ecp,
1815 data,
1816 data_size,
1817 data_length );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001818
Steven Cooremana2371e52020-07-28 14:30:39 +02001819 mbedtls_ecp_keypair_free( ecp );
1820 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001821 return( status );
1822#else
1823 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001824 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001825#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1826 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001827 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001828 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001829 else
1830 {
1831 /* This shouldn't happen in the reference implementation, but
1832 it is valid for a special-purpose implementation to omit
1833 support for exporting certain key types. */
1834 return( PSA_ERROR_NOT_SUPPORTED );
1835 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001836}
1837
Ronald Croncf56a0a2020-08-04 09:51:30 +02001838psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001839 uint8_t *data,
1840 size_t data_size,
1841 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001842{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001843 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001844 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001845 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001846
1847 /* Set the key to empty now, so that even when there are errors, we always
1848 * set data_length to a value between 0 and data_size. On error, setting
1849 * the key to empty is a good choice because an empty key representation is
1850 * unlikely to be accepted anywhere. */
1851 *data_length = 0;
1852
1853 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001854 * which don't require any flag, but
1855 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1856 */
1857 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1858 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001859 if( status != PSA_SUCCESS )
1860 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001861
1862 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001863 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001864
Ronald Cron5c522922020-11-14 16:35:34 +01001865 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001866}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001867
Ronald Croncf56a0a2020-08-04 09:51:30 +02001868psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001869 uint8_t *data,
1870 size_t data_size,
1871 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001872{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001874 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001875 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001876
1877 /* Set the key to empty now, so that even when there are errors, we always
1878 * set data_length to a value between 0 and data_size. On error, setting
1879 * the key to empty is a good choice because an empty key representation is
1880 * unlikely to be accepted anywhere. */
1881 *data_length = 0;
1882
1883 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001884 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001885 if( status != PSA_SUCCESS )
1886 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001887
1888 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001889 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001890
Ronald Cron5c522922020-11-14 16:35:34 +01001891 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001892}
1893
Gilles Peskine91e8c332019-08-02 19:19:39 +02001894#if defined(static_assert)
1895static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1896 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001897static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001898 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001899static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001900 "One or more key attribute flag is listed as both internal-only and external-only" );
1901#endif
1902
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001903/** Validate that a key policy is internally well-formed.
1904 *
1905 * This function only rejects invalid policies. It does not validate the
1906 * consistency of the policy with respect to other attributes of the key
1907 * such as the key type.
1908 */
1909static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001910{
1911 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001912 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001913 PSA_KEY_USAGE_ENCRYPT |
1914 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001915 PSA_KEY_USAGE_SIGN_HASH |
1916 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001917 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1918 return( PSA_ERROR_INVALID_ARGUMENT );
1919
Gilles Peskine4747d192019-04-17 15:05:45 +02001920 return( PSA_SUCCESS );
1921}
1922
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001923/** Validate the internal consistency of key attributes.
1924 *
1925 * This function only rejects invalid attribute values. If does not
1926 * validate the consistency of the attributes with any key data that may
1927 * be involved in the creation of the key.
1928 *
1929 * Call this function early in the key creation process.
1930 *
1931 * \param[in] attributes Key attributes for the new key.
1932 * \param[out] p_drv On any return, the driver for the key, if any.
1933 * NULL for a transparent key.
1934 *
1935 */
1936static psa_status_t psa_validate_key_attributes(
1937 const psa_key_attributes_t *attributes,
1938 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001939{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001940 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001941 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001942 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001943
Ronald Cron54b90082020-10-29 15:26:43 +01001944 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001945 if( status != PSA_SUCCESS )
1946 return( status );
1947
Ronald Crond2ed4812020-07-17 16:11:30 +02001948 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001949 if( status != PSA_SUCCESS )
1950 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001951
Ronald Cron65f38a32020-10-23 17:11:13 +02001952 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1953 {
1954 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1955 return( PSA_ERROR_INVALID_ARGUMENT );
1956 }
1957 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001958 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001959 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001960 if( status != PSA_SUCCESS )
1961 return( status );
1962 }
1963
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001964 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001965 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001966 return( status );
1967
1968 /* Refuse to create overly large keys.
1969 * Note that this doesn't trigger on import if the attributes don't
1970 * explicitly specify a size (so psa_get_key_bits returns 0), so
1971 * psa_import_key() needs its own checks. */
1972 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1973 return( PSA_ERROR_NOT_SUPPORTED );
1974
Gilles Peskine91e8c332019-08-02 19:19:39 +02001975 /* Reject invalid flags. These should not be reachable through the API. */
1976 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1977 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1978 return( PSA_ERROR_INVALID_ARGUMENT );
1979
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001980 return( PSA_SUCCESS );
1981}
1982
Gilles Peskine4747d192019-04-17 15:05:45 +02001983/** Prepare a key slot to receive key material.
1984 *
1985 * This function allocates a key slot and sets its metadata.
1986 *
1987 * If this function fails, call psa_fail_key_creation().
1988 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001989 * This function is intended to be used as follows:
1990 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001991 * it with the specified attributes, and in case of a volatile key assign it
1992 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001993 * -# Populate the slot with the key material.
1994 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1995 * In case of failure at any step, stop the sequence and call
1996 * psa_fail_key_creation().
1997 *
Ronald Cron5c522922020-11-14 16:35:34 +01001998 * On success, the key slot is locked. It is the responsibility of the caller
1999 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02002000 *
Gilles Peskinedf179142019-07-15 22:02:14 +02002001 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02002002 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02002003 * \param[out] p_slot On success, a pointer to the prepared slot.
2004 * \param[out] p_drv On any return, the driver for the key, if any.
2005 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002006 *
2007 * \retval #PSA_SUCCESS
2008 * The key slot is ready to receive key material.
2009 * \return If this function fails, the key slot is an invalid state.
2010 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002011 */
2012static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02002013 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02002014 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002015 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002016 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02002017{
2018 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02002019 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02002020 psa_key_slot_t *slot;
2021
Gilles Peskinedf179142019-07-15 22:02:14 +02002022 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02002023 *p_drv = NULL;
2024
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002025 status = psa_validate_key_attributes( attributes, p_drv );
2026 if( status != PSA_SUCCESS )
2027 return( status );
2028
Ronald Cronc4d1b512020-07-31 11:26:37 +02002029 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02002030 if( status != PSA_SUCCESS )
2031 return( status );
2032 slot = *p_slot;
2033
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002034 /* We're storing the declared bit-size of the key. It's up to each
2035 * creation mechanism to verify that this information is correct.
2036 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002037 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002038 * is optional (import, copy). In case of a volatile key, assign it the
2039 * volatile key identifier associated to the slot returned to contain its
2040 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002041
2042 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002043 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2044 {
2045#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2046 slot->attr.id = volatile_key_id;
2047#else
2048 slot->attr.id.key_id = volatile_key_id;
2049#endif
2050 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002051
Gilles Peskine91e8c332019-08-02 19:19:39 +02002052 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002053 * external-only flags, query `attributes`. Thanks to the check
2054 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002055 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002056 * may have set. */
2057 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002058
Gilles Peskinecbaff462019-07-12 23:46:04 +02002059#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002060 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002061 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002062 * create the key file in internal storage, create the
2063 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002064 * persistent data. This is done by starting a transaction that will
2065 * encompass these three actions.
2066 * For registering a volatile key, we just need to find an appropriate
2067 * slot number inside the SE. Since the key is designated volatile, creating
2068 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002069 /* The first thing to do is to find a slot number for the new key.
2070 * We save the slot number in persistent storage as part of the
2071 * transaction data. It will be needed to recover if the power
2072 * fails during the key creation process, to clean up on the secure
2073 * element side after restarting. Obtaining a slot number from the
2074 * secure element driver updates its persistent state, but we do not yet
2075 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002076 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002077 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002078 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01002079 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002080 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002081 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002082 if( status != PSA_SUCCESS )
2083 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002084
2085 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002086 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002087 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2088 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01002089 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002090 psa_crypto_transaction.key.id = slot->attr.id;
2091 status = psa_crypto_save_transaction( );
2092 if( status != PSA_SUCCESS )
2093 {
2094 (void) psa_crypto_stop_transaction( );
2095 return( status );
2096 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002097 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01002098
2099 status = psa_copy_key_material_into_slot(
2100 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00002101 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002102
2103 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2104 {
2105 /* Key registration only makes sense with a secure element. */
2106 return( PSA_ERROR_INVALID_ARGUMENT );
2107 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002108#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2109
Ronald Cronc4d1b512020-07-31 11:26:37 +02002110 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002111}
Gilles Peskine4747d192019-04-17 15:05:45 +02002112
2113/** Finalize the creation of a key once its key material has been set.
2114 *
2115 * This entails writing the key to persistent storage.
2116 *
2117 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002118 * See the documentation of psa_start_key_creation() for the intended use
2119 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002120 *
Ronald Cron5c522922020-11-14 16:35:34 +01002121 * If the finalization succeeds, the function unlocks the key slot (it was
2122 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2123 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002124 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002125 * \param[in,out] slot Pointer to the slot with key material.
2126 * \param[in] driver The secure element driver for the key,
2127 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002128 * \param[out] key On success, identifier of the key. Note that the
2129 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002130 *
2131 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002132 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002133 * \return If this function fails, the key slot is an invalid state.
2134 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002135 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002136static psa_status_t psa_finish_key_creation(
2137 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002138 psa_se_drv_table_entry_t *driver,
2139 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002140{
2141 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002142 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002143 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002144
2145#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002146 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002147 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002148#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2149 if( driver != NULL )
2150 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002151 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01002152 psa_key_slot_number_t slot_number =
2153 psa_key_slot_get_slot_number( slot ) ;
2154
Gilles Peskineb46bef22019-07-30 21:32:04 +02002155#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01002156 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02002157 sizeof( data.slot_number ),
2158 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002159#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01002160 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002161 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002162 (uint8_t*) &data,
2163 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002164 }
2165 else
2166#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2167 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002168 /* Key material is saved in export representation in the slot, so
2169 * just pass the slot buffer for storage. */
2170 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002171 slot->key.data,
2172 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002173 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002174 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002175#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2176
Gilles Peskinecbaff462019-07-12 23:46:04 +02002177#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002178 /* Finish the transaction for a key creation. This does not
2179 * happen when registering an existing key. Detect this case
2180 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002181 * creation of a persistent key in a secure element requires a transaction,
2182 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002183 if( driver != NULL &&
2184 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002185 {
2186 status = psa_save_se_persistent_data( driver );
2187 if( status != PSA_SUCCESS )
2188 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002189 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002190 return( status );
2191 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002192 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002193 }
2194#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2195
Ronald Cron50972942020-11-14 11:28:25 +01002196 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002197 {
2198 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002199 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002200 if( status != PSA_SUCCESS )
2201 *key = MBEDTLS_SVC_KEY_ID_INIT;
2202 }
Ronald Cron50972942020-11-14 11:28:25 +01002203
Gilles Peskine4747d192019-04-17 15:05:45 +02002204 return( status );
2205}
2206
2207/** Abort the creation of a key.
2208 *
2209 * You may call this function after calling psa_start_key_creation(),
2210 * or after psa_finish_key_creation() fails. In other circumstances, this
2211 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002212 * See the documentation of psa_start_key_creation() for the intended use
2213 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002214 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002215 * \param[in,out] slot Pointer to the slot with key material.
2216 * \param[in] driver The secure element driver for the key,
2217 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002218 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002219static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002220 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002221{
Gilles Peskine011e4282019-06-26 18:34:38 +02002222 (void) driver;
2223
Gilles Peskine4747d192019-04-17 15:05:45 +02002224 if( slot == NULL )
2225 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002226
2227#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002228 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002229 * element, and the failure happened later (when saving metadata
2230 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002231 * element.
2232 * https://github.com/ARMmbed/mbed-crypto/issues/217
2233 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002234
Gilles Peskined7729582019-08-05 15:55:54 +02002235 /* Abort the ongoing transaction if any (there may not be one if
2236 * the creation process failed before starting one, or if the
2237 * key creation is a registration of a key in a secure element).
2238 * Earlier functions must already have done what it takes to undo any
2239 * partial creation. All that's left is to update the transaction data
2240 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002241 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002242#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2243
Gilles Peskine4747d192019-04-17 15:05:45 +02002244 psa_wipe_key_slot( slot );
2245}
2246
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002247/** Validate optional attributes during key creation.
2248 *
2249 * Some key attributes are optional during key creation. If they are
2250 * specified in the attributes structure, check that they are consistent
2251 * with the data in the slot.
2252 *
2253 * This function should be called near the end of key creation, after
2254 * the slot in memory is fully populated but before saving persistent data.
2255 */
2256static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002257 const psa_key_slot_t *slot,
2258 const psa_key_attributes_t *attributes )
2259{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002260 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002261 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002262 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002263 return( PSA_ERROR_INVALID_ARGUMENT );
2264 }
2265
2266 if( attributes->domain_parameters_size != 0 )
2267 {
John Durkop0e005192020-10-31 22:06:54 -07002268#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2269 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002270 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002271 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002272 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002273 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002274 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002275
Ronald Cron90857082020-11-25 14:58:33 +01002276 psa_status_t status = mbedtls_psa_rsa_load_representation(
2277 slot->attr.type,
2278 slot->key.data,
2279 slot->key.bytes,
2280 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002281 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002282 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002283
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002284 mbedtls_mpi_init( &actual );
2285 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002286 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002287 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002288 mbedtls_rsa_free( rsa );
2289 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002290 if( ret != 0 )
2291 goto rsa_exit;
2292 ret = mbedtls_mpi_read_binary( &required,
2293 attributes->domain_parameters,
2294 attributes->domain_parameters_size );
2295 if( ret != 0 )
2296 goto rsa_exit;
2297 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2298 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2299 rsa_exit:
2300 mbedtls_mpi_free( &actual );
2301 mbedtls_mpi_free( &required );
2302 if( ret != 0)
2303 return( mbedtls_to_psa_error( ret ) );
2304 }
2305 else
John Durkop9814fa22020-11-04 12:28:15 -08002306#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2307 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002308 {
2309 return( PSA_ERROR_INVALID_ARGUMENT );
2310 }
2311 }
2312
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002313 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002314 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002315 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002316 return( PSA_ERROR_INVALID_ARGUMENT );
2317 }
2318
2319 return( PSA_SUCCESS );
2320}
2321
Gilles Peskine4747d192019-04-17 15:05:45 +02002322psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002323 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002324 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002325 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002326{
2327 psa_status_t status;
2328 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002329 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002330
Ronald Cron81709fc2020-11-14 12:10:32 +01002331 *key = MBEDTLS_SVC_KEY_ID_INIT;
2332
Gilles Peskine0f84d622019-09-12 19:03:13 +02002333 /* Reject zero-length symmetric keys (including raw data key objects).
2334 * This also rejects any key which might be encoded as an empty string,
2335 * which is never valid. */
2336 if( data_length == 0 )
2337 return( PSA_ERROR_INVALID_ARGUMENT );
2338
Gilles Peskinedf179142019-07-15 22:02:14 +02002339 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002340 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002341 if( status != PSA_SUCCESS )
2342 goto exit;
2343
Gilles Peskine5d309672019-07-12 23:47:28 +02002344#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2345 if( driver != NULL )
2346 {
2347 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002348 /* The driver should set the number of key bits, however in
2349 * case it doesn't, we initialize bits to an invalid value. */
2350 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002351 if( drv->key_management == NULL ||
2352 drv->key_management->p_import == NULL )
2353 {
2354 status = PSA_ERROR_NOT_SUPPORTED;
2355 goto exit;
2356 }
2357 status = drv->key_management->p_import(
2358 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01002359 psa_key_slot_get_slot_number( slot ),
2360 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002361 if( status != PSA_SUCCESS )
2362 goto exit;
2363 if( bits > PSA_MAX_KEY_BITS )
2364 {
2365 status = PSA_ERROR_NOT_SUPPORTED;
2366 goto exit;
2367 }
2368 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002369 }
2370 else
2371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002372 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2373 {
2374 /* Importing a key with external lifetime through the driver wrapper
2375 * interface is not yet supported. Return as if this was an invalid
2376 * lifetime. */
2377 status = PSA_ERROR_INVALID_ARGUMENT;
2378 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002379 }
2380 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002381 {
2382 status = psa_import_key_into_slot( slot, data, data_length );
2383 if( status != PSA_SUCCESS )
2384 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002385 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002386 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002387 if( status != PSA_SUCCESS )
2388 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002389
Ronald Cron81709fc2020-11-14 12:10:32 +01002390 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002391exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002392 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002393 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002394
Gilles Peskine4747d192019-04-17 15:05:45 +02002395 return( status );
2396}
2397
Gilles Peskined7729582019-08-05 15:55:54 +02002398#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2399psa_status_t mbedtls_psa_register_se_key(
2400 const psa_key_attributes_t *attributes )
2401{
2402 psa_status_t status;
2403 psa_key_slot_t *slot = NULL;
2404 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002405 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002406
2407 /* Leaving attributes unspecified is not currently supported.
2408 * It could make sense to query the key type and size from the
2409 * secure element, but not all secure elements support this
2410 * and the driver HAL doesn't currently support it. */
2411 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2412 return( PSA_ERROR_NOT_SUPPORTED );
2413 if( psa_get_key_bits( attributes ) == 0 )
2414 return( PSA_ERROR_NOT_SUPPORTED );
2415
2416 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002417 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002418 if( status != PSA_SUCCESS )
2419 goto exit;
2420
Ronald Cron81709fc2020-11-14 12:10:32 +01002421 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002422
2423exit:
2424 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002425 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002426
Gilles Peskined7729582019-08-05 15:55:54 +02002427 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002428 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002429 return( status );
2430}
2431#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2432
Gilles Peskinef603c712019-01-19 13:40:11 +01002433static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002434 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002435{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002436 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002437 source->key.data,
2438 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002439 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002440 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002441
Steven Cooreman398aee52020-10-13 14:35:45 +02002442 target->attr.type = source->attr.type;
2443 target->attr.bits = source->attr.bits;
2444
2445 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002446}
2447
Ronald Croncf56a0a2020-08-04 09:51:30 +02002448psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002449 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002450 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002451{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002452 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002453 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002454 psa_key_slot_t *source_slot = NULL;
2455 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002456 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002457 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002458
Ronald Cron81709fc2020-11-14 12:10:32 +01002459 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2460
Ronald Cron5c522922020-11-14 16:35:34 +01002461 status = psa_get_and_lock_transparent_key_slot_with_policy(
2462 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002463 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002464 goto exit;
2465
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002466 status = psa_validate_optional_attributes( source_slot,
2467 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002468 if( status != PSA_SUCCESS )
2469 goto exit;
2470
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002471 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002472 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002473 if( status != PSA_SUCCESS )
2474 goto exit;
2475
Ronald Cron81709fc2020-11-14 12:10:32 +01002476 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2477 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002478 if( status != PSA_SUCCESS )
2479 goto exit;
2480
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002481#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2482 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002483 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002484 /* Copying to a secure element is not implemented yet. */
2485 status = PSA_ERROR_NOT_SUPPORTED;
2486 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002487 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002488#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002489
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002490 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002491 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002492 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002493
Ronald Cron81709fc2020-11-14 12:10:32 +01002494 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002495exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002496 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002497 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002498
Ronald Cron5c522922020-11-14 16:35:34 +01002499 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002500
Ronald Cron5c522922020-11-14 16:35:34 +01002501 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002502}
2503
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002504
2505
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002506/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002507/* Message digests */
2508/****************************************************************/
2509
John Durkop07cc04a2020-11-16 22:08:34 -08002510#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002511 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2512 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002513 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002514static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002515{
2516 switch( alg )
2517 {
John Durkopee4e6602020-11-27 08:48:46 -08002518#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002519 case PSA_ALG_MD2:
2520 return( &mbedtls_md2_info );
2521#endif
John Durkopee4e6602020-11-27 08:48:46 -08002522#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002523 case PSA_ALG_MD4:
2524 return( &mbedtls_md4_info );
2525#endif
John Durkopee4e6602020-11-27 08:48:46 -08002526#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002527 case PSA_ALG_MD5:
2528 return( &mbedtls_md5_info );
2529#endif
John Durkopee4e6602020-11-27 08:48:46 -08002530#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002531 case PSA_ALG_RIPEMD160:
2532 return( &mbedtls_ripemd160_info );
2533#endif
John Durkopee4e6602020-11-27 08:48:46 -08002534#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002535 case PSA_ALG_SHA_1:
2536 return( &mbedtls_sha1_info );
2537#endif
John Durkopee4e6602020-11-27 08:48:46 -08002538#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002539 case PSA_ALG_SHA_224:
2540 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002541#endif
2542#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002543 case PSA_ALG_SHA_256:
2544 return( &mbedtls_sha256_info );
2545#endif
John Durkopee4e6602020-11-27 08:48:46 -08002546#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002547 case PSA_ALG_SHA_384:
2548 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002549#endif
John Durkopee4e6602020-11-27 08:48:46 -08002550#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002551 case PSA_ALG_SHA_512:
2552 return( &mbedtls_sha512_info );
2553#endif
2554 default:
2555 return( NULL );
2556 }
2557}
John Durkop07cc04a2020-11-16 22:08:34 -08002558#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002559 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2560 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2561 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002562
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002563psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2564{
2565 switch( operation->alg )
2566 {
Gilles Peskine81736312018-06-26 15:04:31 +02002567 case 0:
2568 /* The object has (apparently) been initialized but it is not
2569 * in use. It's ok to call abort on such an object, and there's
2570 * nothing to do. */
2571 break;
John Durkopee4e6602020-11-27 08:48:46 -08002572#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 case PSA_ALG_MD2:
2574 mbedtls_md2_free( &operation->ctx.md2 );
2575 break;
2576#endif
John Durkopee4e6602020-11-27 08:48:46 -08002577#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002578 case PSA_ALG_MD4:
2579 mbedtls_md4_free( &operation->ctx.md4 );
2580 break;
2581#endif
John Durkopee4e6602020-11-27 08:48:46 -08002582#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002583 case PSA_ALG_MD5:
2584 mbedtls_md5_free( &operation->ctx.md5 );
2585 break;
2586#endif
John Durkopee4e6602020-11-27 08:48:46 -08002587#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002588 case PSA_ALG_RIPEMD160:
2589 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2590 break;
2591#endif
John Durkopee4e6602020-11-27 08:48:46 -08002592#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002593 case PSA_ALG_SHA_1:
2594 mbedtls_sha1_free( &operation->ctx.sha1 );
2595 break;
2596#endif
John Durkop6ca23272020-12-03 06:01:32 -08002597#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002598 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002599 mbedtls_sha256_free( &operation->ctx.sha256 );
2600 break;
2601#endif
2602#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002603 case PSA_ALG_SHA_256:
2604 mbedtls_sha256_free( &operation->ctx.sha256 );
2605 break;
2606#endif
John Durkop6ca23272020-12-03 06:01:32 -08002607#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002608 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002609 mbedtls_sha512_free( &operation->ctx.sha512 );
2610 break;
2611#endif
2612#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002613 case PSA_ALG_SHA_512:
2614 mbedtls_sha512_free( &operation->ctx.sha512 );
2615 break;
2616#endif
2617 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002618 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002619 }
2620 operation->alg = 0;
2621 return( PSA_SUCCESS );
2622}
2623
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002624psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002625 psa_algorithm_t alg )
2626{
Janos Follath24eed8d2019-11-22 13:21:35 +00002627 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002628
2629 /* A context must be freshly initialized before it can be set up. */
2630 if( operation->alg != 0 )
2631 {
2632 return( PSA_ERROR_BAD_STATE );
2633 }
2634
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002635 switch( alg )
2636 {
John Durkopee4e6602020-11-27 08:48:46 -08002637#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002638 case PSA_ALG_MD2:
2639 mbedtls_md2_init( &operation->ctx.md2 );
2640 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2641 break;
2642#endif
John Durkopee4e6602020-11-27 08:48:46 -08002643#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002644 case PSA_ALG_MD4:
2645 mbedtls_md4_init( &operation->ctx.md4 );
2646 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2647 break;
2648#endif
John Durkopee4e6602020-11-27 08:48:46 -08002649#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002650 case PSA_ALG_MD5:
2651 mbedtls_md5_init( &operation->ctx.md5 );
2652 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2653 break;
2654#endif
John Durkopee4e6602020-11-27 08:48:46 -08002655#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002656 case PSA_ALG_RIPEMD160:
2657 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2658 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2659 break;
2660#endif
John Durkopee4e6602020-11-27 08:48:46 -08002661#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002662 case PSA_ALG_SHA_1:
2663 mbedtls_sha1_init( &operation->ctx.sha1 );
2664 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2665 break;
2666#endif
John Durkopee4e6602020-11-27 08:48:46 -08002667#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002668 case PSA_ALG_SHA_224:
2669 mbedtls_sha256_init( &operation->ctx.sha256 );
2670 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2671 break;
John Durkopee4e6602020-11-27 08:48:46 -08002672#endif
2673#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002674 case PSA_ALG_SHA_256:
2675 mbedtls_sha256_init( &operation->ctx.sha256 );
2676 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2677 break;
2678#endif
John Durkopee4e6602020-11-27 08:48:46 -08002679#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002680 case PSA_ALG_SHA_384:
2681 mbedtls_sha512_init( &operation->ctx.sha512 );
2682 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2683 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002684#endif
John Durkopee4e6602020-11-27 08:48:46 -08002685#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002686 case PSA_ALG_SHA_512:
2687 mbedtls_sha512_init( &operation->ctx.sha512 );
2688 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2689 break;
2690#endif
2691 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002692 return( PSA_ALG_IS_HASH( alg ) ?
2693 PSA_ERROR_NOT_SUPPORTED :
2694 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002695 }
2696 if( ret == 0 )
2697 operation->alg = alg;
2698 else
2699 psa_hash_abort( operation );
2700 return( mbedtls_to_psa_error( ret ) );
2701}
2702
2703psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2704 const uint8_t *input,
2705 size_t input_length )
2706{
Janos Follath24eed8d2019-11-22 13:21:35 +00002707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002708
2709 /* Don't require hash implementations to behave correctly on a
2710 * zero-length input, which may have an invalid pointer. */
2711 if( input_length == 0 )
2712 return( PSA_SUCCESS );
2713
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002714 switch( operation->alg )
2715 {
John Durkopee4e6602020-11-27 08:48:46 -08002716#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002717 case PSA_ALG_MD2:
2718 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2719 input, input_length );
2720 break;
2721#endif
John Durkopee4e6602020-11-27 08:48:46 -08002722#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002723 case PSA_ALG_MD4:
2724 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2725 input, input_length );
2726 break;
2727#endif
John Durkopee4e6602020-11-27 08:48:46 -08002728#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002729 case PSA_ALG_MD5:
2730 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2731 input, input_length );
2732 break;
2733#endif
John Durkopee4e6602020-11-27 08:48:46 -08002734#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002735 case PSA_ALG_RIPEMD160:
2736 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2737 input, input_length );
2738 break;
2739#endif
John Durkopee4e6602020-11-27 08:48:46 -08002740#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002741 case PSA_ALG_SHA_1:
2742 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2743 input, input_length );
2744 break;
2745#endif
John Durkop6ca23272020-12-03 06:01:32 -08002746#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002747 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002748 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2749 input, input_length );
2750 break;
2751#endif
2752#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002753 case PSA_ALG_SHA_256:
2754 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2755 input, input_length );
2756 break;
2757#endif
John Durkop6ca23272020-12-03 06:01:32 -08002758#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002759 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002760 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2761 input, input_length );
2762 break;
2763#endif
2764#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002765 case PSA_ALG_SHA_512:
2766 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2767 input, input_length );
2768 break;
2769#endif
2770 default:
John Durkopee4e6602020-11-27 08:48:46 -08002771 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002772 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002773 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002774
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002775 if( ret != 0 )
2776 psa_hash_abort( operation );
2777 return( mbedtls_to_psa_error( ret ) );
2778}
2779
2780psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2781 uint8_t *hash,
2782 size_t hash_size,
2783 size_t *hash_length )
2784{
itayzafrir40835d42018-08-02 13:14:17 +03002785 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002786 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002787 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002788
2789 /* Fill the output buffer with something that isn't a valid hash
2790 * (barring an attack on the hash and deliberately-crafted input),
2791 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002792 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002793 /* If hash_size is 0 then hash may be NULL and then the
2794 * call to memset would have undefined behavior. */
2795 if( hash_size != 0 )
2796 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002797
2798 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002799 {
2800 status = PSA_ERROR_BUFFER_TOO_SMALL;
2801 goto exit;
2802 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002803
2804 switch( operation->alg )
2805 {
John Durkopee4e6602020-11-27 08:48:46 -08002806#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002807 case PSA_ALG_MD2:
2808 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2809 break;
2810#endif
John Durkopee4e6602020-11-27 08:48:46 -08002811#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002812 case PSA_ALG_MD4:
2813 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2814 break;
2815#endif
John Durkopee4e6602020-11-27 08:48:46 -08002816#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002817 case PSA_ALG_MD5:
2818 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2819 break;
2820#endif
John Durkopee4e6602020-11-27 08:48:46 -08002821#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002822 case PSA_ALG_RIPEMD160:
2823 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2824 break;
2825#endif
John Durkopee4e6602020-11-27 08:48:46 -08002826#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002827 case PSA_ALG_SHA_1:
2828 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2829 break;
2830#endif
John Durkop6ca23272020-12-03 06:01:32 -08002831#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002832 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002833 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2834 break;
2835#endif
2836#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002837 case PSA_ALG_SHA_256:
2838 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2839 break;
2840#endif
John Durkop6ca23272020-12-03 06:01:32 -08002841#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002842 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002843 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2844 break;
2845#endif
2846#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002847 case PSA_ALG_SHA_512:
2848 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2849 break;
2850#endif
2851 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002852 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002853 }
itayzafrir40835d42018-08-02 13:14:17 +03002854 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002855
itayzafrir40835d42018-08-02 13:14:17 +03002856exit:
2857 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002858 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002859 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002860 return( psa_hash_abort( operation ) );
2861 }
2862 else
2863 {
2864 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002865 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002866 }
2867}
2868
Gilles Peskine2d277862018-06-18 15:41:12 +02002869psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2870 const uint8_t *hash,
2871 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002872{
2873 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2874 size_t actual_hash_length;
2875 psa_status_t status = psa_hash_finish( operation,
2876 actual_hash, sizeof( actual_hash ),
2877 &actual_hash_length );
2878 if( status != PSA_SUCCESS )
2879 return( status );
2880 if( actual_hash_length != hash_length )
2881 return( PSA_ERROR_INVALID_SIGNATURE );
2882 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2883 return( PSA_ERROR_INVALID_SIGNATURE );
2884 return( PSA_SUCCESS );
2885}
2886
Gilles Peskine0a749c82019-11-28 19:33:58 +01002887psa_status_t psa_hash_compute( psa_algorithm_t alg,
2888 const uint8_t *input, size_t input_length,
2889 uint8_t *hash, size_t hash_size,
2890 size_t *hash_length )
2891{
2892 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2894
2895 *hash_length = hash_size;
2896 status = psa_hash_setup( &operation, alg );
2897 if( status != PSA_SUCCESS )
2898 goto exit;
2899 status = psa_hash_update( &operation, input, input_length );
2900 if( status != PSA_SUCCESS )
2901 goto exit;
2902 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2903 if( status != PSA_SUCCESS )
2904 goto exit;
2905
2906exit:
2907 if( status == PSA_SUCCESS )
2908 status = psa_hash_abort( &operation );
2909 else
2910 psa_hash_abort( &operation );
2911 return( status );
2912}
2913
2914psa_status_t psa_hash_compare( psa_algorithm_t alg,
2915 const uint8_t *input, size_t input_length,
2916 const uint8_t *hash, size_t hash_length )
2917{
2918 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2919 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2920
2921 status = psa_hash_setup( &operation, alg );
2922 if( status != PSA_SUCCESS )
2923 goto exit;
2924 status = psa_hash_update( &operation, input, input_length );
2925 if( status != PSA_SUCCESS )
2926 goto exit;
2927 status = psa_hash_verify( &operation, hash, hash_length );
2928 if( status != PSA_SUCCESS )
2929 goto exit;
2930
2931exit:
2932 if( status == PSA_SUCCESS )
2933 status = psa_hash_abort( &operation );
2934 else
2935 psa_hash_abort( &operation );
2936 return( status );
2937}
2938
Gilles Peskineeb35d782019-01-22 17:56:16 +01002939psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2940 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002941{
2942 if( target_operation->alg != 0 )
2943 return( PSA_ERROR_BAD_STATE );
2944
2945 switch( source_operation->alg )
2946 {
2947 case 0:
2948 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002949#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002950 case PSA_ALG_MD2:
2951 mbedtls_md2_clone( &target_operation->ctx.md2,
2952 &source_operation->ctx.md2 );
2953 break;
2954#endif
John Durkopee4e6602020-11-27 08:48:46 -08002955#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002956 case PSA_ALG_MD4:
2957 mbedtls_md4_clone( &target_operation->ctx.md4,
2958 &source_operation->ctx.md4 );
2959 break;
2960#endif
John Durkopee4e6602020-11-27 08:48:46 -08002961#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002962 case PSA_ALG_MD5:
2963 mbedtls_md5_clone( &target_operation->ctx.md5,
2964 &source_operation->ctx.md5 );
2965 break;
2966#endif
John Durkopee4e6602020-11-27 08:48:46 -08002967#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002968 case PSA_ALG_RIPEMD160:
2969 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2970 &source_operation->ctx.ripemd160 );
2971 break;
2972#endif
John Durkopee4e6602020-11-27 08:48:46 -08002973#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002974 case PSA_ALG_SHA_1:
2975 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2976 &source_operation->ctx.sha1 );
2977 break;
2978#endif
John Durkop6ca23272020-12-03 06:01:32 -08002979#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002980 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002981 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2982 &source_operation->ctx.sha256 );
2983 break;
2984#endif
2985#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002986 case PSA_ALG_SHA_256:
2987 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2988 &source_operation->ctx.sha256 );
2989 break;
2990#endif
John Durkop6ca23272020-12-03 06:01:32 -08002991#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002992 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002993 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2994 &source_operation->ctx.sha512 );
2995 break;
2996#endif
2997#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002998 case PSA_ALG_SHA_512:
2999 mbedtls_sha512_clone( &target_operation->ctx.sha512,
3000 &source_operation->ctx.sha512 );
3001 break;
3002#endif
3003 default:
3004 return( PSA_ERROR_NOT_SUPPORTED );
3005 }
3006
3007 target_operation->alg = source_operation->alg;
3008 return( PSA_SUCCESS );
3009}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003010
3011
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003012/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013/* MAC */
3014/****************************************************************/
3015
Gilles Peskinedc2fc842018-03-07 16:42:59 +01003016static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01003017 psa_algorithm_t alg,
3018 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02003019 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03003020 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03003023 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003024
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003025 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003026 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003027
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
3029 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03003030 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003031 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01003032 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033 mode = MBEDTLS_MODE_STREAM;
3034 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003035 case PSA_ALG_CTR:
3036 mode = MBEDTLS_MODE_CTR;
3037 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003038 case PSA_ALG_CFB:
3039 mode = MBEDTLS_MODE_CFB;
3040 break;
3041 case PSA_ALG_OFB:
3042 mode = MBEDTLS_MODE_OFB;
3043 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003044 case PSA_ALG_ECB_NO_PADDING:
3045 mode = MBEDTLS_MODE_ECB;
3046 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003047 case PSA_ALG_CBC_NO_PADDING:
3048 mode = MBEDTLS_MODE_CBC;
3049 break;
3050 case PSA_ALG_CBC_PKCS7:
3051 mode = MBEDTLS_MODE_CBC;
3052 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003053 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003054 mode = MBEDTLS_MODE_CCM;
3055 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003056 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057 mode = MBEDTLS_MODE_GCM;
3058 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003059 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3060 mode = MBEDTLS_MODE_CHACHAPOLY;
3061 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003062 default:
3063 return( NULL );
3064 }
3065 }
3066 else if( alg == PSA_ALG_CMAC )
3067 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068 else
3069 return( NULL );
3070
3071 switch( key_type )
3072 {
3073 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003074 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003075 break;
3076 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003077 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3078 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003079 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003080 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003081 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003082 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003083 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3084 * but two-key Triple-DES is functionally three-key Triple-DES
3085 * with K1=K3, so that's how we present it to mbedtls. */
3086 if( key_bits == 128 )
3087 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003088 break;
3089 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003090 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091 break;
3092 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003093 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003094 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003095 case PSA_KEY_TYPE_CHACHA20:
3096 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3097 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003098 default:
3099 return( NULL );
3100 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003101 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003102 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003103
Jaeden Amero23bbb752018-06-26 14:16:54 +01003104 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3105 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003106}
3107
John Durkop6ba40d12020-11-10 08:50:04 -08003108#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003109static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003110{
Gilles Peskine2d277862018-06-18 15:41:12 +02003111 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003112 {
3113 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003114 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003115 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003116 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003117 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003118 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003119 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003120 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003121 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003122 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003123 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003124 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003125 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003126 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003127 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003128 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003129 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003130 return( 128 );
3131 default:
3132 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003133 }
3134}
John Durkop07cc04a2020-11-16 22:08:34 -08003135#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003136
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003137/* Initialize the MAC operation structure. Once this function has been
3138 * called, psa_mac_abort can run and will do the right thing. */
3139static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3140 psa_algorithm_t alg )
3141{
3142 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3143
3144 operation->alg = alg;
3145 operation->key_set = 0;
3146 operation->iv_set = 0;
3147 operation->iv_required = 0;
3148 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003149 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003150
3151#if defined(MBEDTLS_CMAC_C)
3152 if( alg == PSA_ALG_CMAC )
3153 {
3154 operation->iv_required = 0;
3155 mbedtls_cipher_init( &operation->ctx.cmac );
3156 status = PSA_SUCCESS;
3157 }
3158 else
3159#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003160#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003161 if( PSA_ALG_IS_HMAC( operation->alg ) )
3162 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003163 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3164 operation->ctx.hmac.hash_ctx.alg = 0;
3165 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003166 }
3167 else
John Durkopd0321952020-10-29 21:37:36 -07003168#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003169 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003170 if( ! PSA_ALG_IS_MAC( alg ) )
3171 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003172 }
3173
3174 if( status != PSA_SUCCESS )
3175 memset( operation, 0, sizeof( *operation ) );
3176 return( status );
3177}
3178
John Durkop6ba40d12020-11-10 08:50:04 -08003179#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003180static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3181{
Gilles Peskine3f108122018-12-07 18:14:53 +01003182 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003183 return( psa_hash_abort( &hmac->hash_ctx ) );
3184}
John Durkop6ba40d12020-11-10 08:50:04 -08003185#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003186
Gilles Peskine8c9def32018-02-08 10:02:12 +01003187psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3188{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003189 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003190 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003191 /* The object has (apparently) been initialized but it is not
3192 * in use. It's ok to call abort on such an object, and there's
3193 * nothing to do. */
3194 return( PSA_SUCCESS );
3195 }
3196 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003197#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003198 if( operation->alg == PSA_ALG_CMAC )
3199 {
3200 mbedtls_cipher_free( &operation->ctx.cmac );
3201 }
3202 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003203#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003204#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003205 if( PSA_ALG_IS_HMAC( operation->alg ) )
3206 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003207 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003208 }
3209 else
John Durkopd0321952020-10-29 21:37:36 -07003210#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003211 {
3212 /* Sanity check (shouldn't happen: operation->alg should
3213 * always have been initialized to a valid value). */
3214 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003215 }
Moran Peker41deec42018-04-04 15:43:05 +03003216
Gilles Peskine8c9def32018-02-08 10:02:12 +01003217 operation->alg = 0;
3218 operation->key_set = 0;
3219 operation->iv_set = 0;
3220 operation->iv_required = 0;
3221 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003222 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003223
Gilles Peskine8c9def32018-02-08 10:02:12 +01003224 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003225
3226bad_state:
3227 /* If abort is called on an uninitialized object, we can't trust
3228 * anything. Wipe the object in case it contains confidential data.
3229 * This may result in a memory leak if a pointer gets overwritten,
3230 * but it's too late to do anything about this. */
3231 memset( operation, 0, sizeof( *operation ) );
3232 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003233}
3234
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003235#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003236static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003237 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003238 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003239 const mbedtls_cipher_info_t *cipher_info )
3240{
Janos Follath24eed8d2019-11-22 13:21:35 +00003241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003242
3243 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003244
3245 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3246 if( ret != 0 )
3247 return( ret );
3248
3249 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003250 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003251 key_bits );
3252 return( ret );
3253}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003254#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003255
John Durkop6ba40d12020-11-10 08:50:04 -08003256#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003257static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3258 const uint8_t *key,
3259 size_t key_length,
3260 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003261{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003262 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003263 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003264 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003265 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003266 psa_status_t status;
3267
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003268 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3269 * overflow below. This should never trigger if the hash algorithm
3270 * is implemented correctly. */
3271 /* The size checks against the ipad and opad buffers cannot be written
3272 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3273 * because that triggers -Wlogical-op on GCC 7.3. */
3274 if( block_size > sizeof( ipad ) )
3275 return( PSA_ERROR_NOT_SUPPORTED );
3276 if( block_size > sizeof( hmac->opad ) )
3277 return( PSA_ERROR_NOT_SUPPORTED );
3278 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003279 return( PSA_ERROR_NOT_SUPPORTED );
3280
Gilles Peskined223b522018-06-11 18:12:58 +02003281 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003282 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003283 status = psa_hash_compute( hash_alg, key, key_length,
3284 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003285 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003286 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003287 }
Gilles Peskine96889972018-07-12 17:07:03 +02003288 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3289 * but it is permitted. It is common when HMAC is used in HKDF, for
3290 * example. Don't call `memcpy` in the 0-length because `key` could be
3291 * an invalid pointer which would make the behavior undefined. */
3292 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003293 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003294
Gilles Peskined223b522018-06-11 18:12:58 +02003295 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3296 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003297 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003298 ipad[i] ^= 0x36;
3299 memset( ipad + key_length, 0x36, block_size - key_length );
3300
3301 /* Copy the key material from ipad to opad, flipping the requisite bits,
3302 * and filling the rest of opad with the requisite constant. */
3303 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003304 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3305 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003306
Gilles Peskine01126fa2018-07-12 17:04:55 +02003307 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003308 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003309 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003310
Gilles Peskine01126fa2018-07-12 17:04:55 +02003311 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003312
3313cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003314 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003315
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003316 return( status );
3317}
John Durkop6ba40d12020-11-10 08:50:04 -08003318#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003319
Gilles Peskine89167cb2018-07-08 20:12:23 +02003320static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003321 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003322 psa_algorithm_t alg,
3323 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003324{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003325 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003326 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003327 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003328 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003329 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003330 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003331 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003332 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003333
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003334 /* A context must be freshly initialized before it can be set up. */
3335 if( operation->alg != 0 )
3336 {
3337 return( PSA_ERROR_BAD_STATE );
3338 }
3339
Gilles Peskined911eb72018-08-14 15:18:45 +02003340 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003341 if( status != PSA_SUCCESS )
3342 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003343 if( is_sign )
3344 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003345
Ronald Cron5c522922020-11-14 16:35:34 +01003346 status = psa_get_and_lock_transparent_key_slot_with_policy(
3347 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003348 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003349 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003350 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003351
Gilles Peskine8c9def32018-02-08 10:02:12 +01003352#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003353 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003354 {
3355 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003356 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003357 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003359 if( cipher_info == NULL )
3360 {
3361 status = PSA_ERROR_NOT_SUPPORTED;
3362 goto exit;
3363 }
3364 operation->mac_size = cipher_info->block_size;
3365 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3366 status = mbedtls_to_psa_error( ret );
3367 }
3368 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003369#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003370#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003371 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003372 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003373 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003374 if( hash_alg == 0 )
3375 {
3376 status = PSA_ERROR_NOT_SUPPORTED;
3377 goto exit;
3378 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003379
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003380 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003381 /* Sanity check. This shouldn't fail on a valid configuration. */
3382 if( operation->mac_size == 0 ||
3383 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3384 {
3385 status = PSA_ERROR_NOT_SUPPORTED;
3386 goto exit;
3387 }
3388
Gilles Peskine8e338702019-07-30 20:06:31 +02003389 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003390 {
3391 status = PSA_ERROR_INVALID_ARGUMENT;
3392 goto exit;
3393 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003394
Gilles Peskine01126fa2018-07-12 17:04:55 +02003395 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003396 slot->key.data,
3397 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003398 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003399 }
3400 else
John Durkopd0321952020-10-29 21:37:36 -07003401#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003402 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003403 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003404 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003405 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003406
Gilles Peskined911eb72018-08-14 15:18:45 +02003407 if( truncated == 0 )
3408 {
3409 /* The "normal" case: untruncated algorithm. Nothing to do. */
3410 }
3411 else if( truncated < 4 )
3412 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003413 /* A very short MAC is too short for security since it can be
3414 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3415 * so we make this our minimum, even though 32 bits is still
3416 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003417 status = PSA_ERROR_NOT_SUPPORTED;
3418 }
3419 else if( truncated > operation->mac_size )
3420 {
3421 /* It's impossible to "truncate" to a larger length. */
3422 status = PSA_ERROR_INVALID_ARGUMENT;
3423 }
3424 else
3425 operation->mac_size = truncated;
3426
Gilles Peskinefbfac682018-07-08 20:51:54 +02003427exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003428 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003429 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003430 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003431 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003432 else
3433 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003434 operation->key_set = 1;
3435 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003436
Ronald Cron5c522922020-11-14 16:35:34 +01003437 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003438
Ronald Cron5c522922020-11-14 16:35:34 +01003439 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003440}
3441
Gilles Peskine89167cb2018-07-08 20:12:23 +02003442psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003443 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003444 psa_algorithm_t alg )
3445{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003446 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003447}
3448
3449psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003450 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003451 psa_algorithm_t alg )
3452{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003453 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003454}
3455
Gilles Peskine8c9def32018-02-08 10:02:12 +01003456psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3457 const uint8_t *input,
3458 size_t input_length )
3459{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003460 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003461 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003462 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003463 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003464 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003465 operation->has_input = 1;
3466
Gilles Peskine8c9def32018-02-08 10:02:12 +01003467#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003468 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003469 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003470 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3471 input, input_length );
3472 status = mbedtls_to_psa_error( ret );
3473 }
3474 else
3475#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003476#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003477 if( PSA_ALG_IS_HMAC( operation->alg ) )
3478 {
3479 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3480 input_length );
3481 }
3482 else
John Durkopd0321952020-10-29 21:37:36 -07003483#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003484 {
3485 /* This shouldn't happen if `operation` was initialized by
3486 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003487 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003488 }
3489
Gilles Peskinefbfac682018-07-08 20:51:54 +02003490 if( status != PSA_SUCCESS )
3491 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003492 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003493}
3494
John Durkop6ba40d12020-11-10 08:50:04 -08003495#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003496static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3497 uint8_t *mac,
3498 size_t mac_size )
3499{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003500 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003501 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3502 size_t hash_size = 0;
3503 size_t block_size = psa_get_hash_block_size( hash_alg );
3504 psa_status_t status;
3505
Gilles Peskine01126fa2018-07-12 17:04:55 +02003506 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3507 if( status != PSA_SUCCESS )
3508 return( status );
3509 /* From here on, tmp needs to be wiped. */
3510
3511 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3512 if( status != PSA_SUCCESS )
3513 goto exit;
3514
3515 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3516 if( status != PSA_SUCCESS )
3517 goto exit;
3518
3519 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3520 if( status != PSA_SUCCESS )
3521 goto exit;
3522
Gilles Peskined911eb72018-08-14 15:18:45 +02003523 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3524 if( status != PSA_SUCCESS )
3525 goto exit;
3526
3527 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003528
3529exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003530 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003531 return( status );
3532}
John Durkop6ba40d12020-11-10 08:50:04 -08003533#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003534
mohammad16036df908f2018-04-02 08:34:15 -07003535static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003536 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003537 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003538{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003539 if( ! operation->key_set )
3540 return( PSA_ERROR_BAD_STATE );
3541 if( operation->iv_required && ! operation->iv_set )
3542 return( PSA_ERROR_BAD_STATE );
3543
Gilles Peskine8c9def32018-02-08 10:02:12 +01003544 if( mac_size < operation->mac_size )
3545 return( PSA_ERROR_BUFFER_TOO_SMALL );
3546
Gilles Peskine8c9def32018-02-08 10:02:12 +01003547#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003548 if( operation->alg == PSA_ALG_CMAC )
3549 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003550 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003551 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3552 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003553 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003554 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003555 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003556 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003557 else
3558#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003559#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003560 if( PSA_ALG_IS_HMAC( operation->alg ) )
3561 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003562 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003563 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003564 }
3565 else
John Durkopd0321952020-10-29 21:37:36 -07003566#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003567 {
3568 /* This shouldn't happen if `operation` was initialized by
3569 * a setup function. */
3570 return( PSA_ERROR_BAD_STATE );
3571 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003572}
3573
Gilles Peskineacd4be32018-07-08 19:56:25 +02003574psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3575 uint8_t *mac,
3576 size_t mac_size,
3577 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003578{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003579 psa_status_t status;
3580
Jaeden Amero252ef282019-02-15 14:05:35 +00003581 if( operation->alg == 0 )
3582 {
3583 return( PSA_ERROR_BAD_STATE );
3584 }
3585
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003586 /* Fill the output buffer with something that isn't a valid mac
3587 * (barring an attack on the mac and deliberately-crafted input),
3588 * in case the caller doesn't check the return status properly. */
3589 *mac_length = mac_size;
3590 /* If mac_size is 0 then mac may be NULL and then the
3591 * call to memset would have undefined behavior. */
3592 if( mac_size != 0 )
3593 memset( mac, '!', mac_size );
3594
Gilles Peskine89167cb2018-07-08 20:12:23 +02003595 if( ! operation->is_sign )
3596 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003597 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003598 }
mohammad16036df908f2018-04-02 08:34:15 -07003599
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003600 status = psa_mac_finish_internal( operation, mac, mac_size );
3601
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003602 if( status == PSA_SUCCESS )
3603 {
3604 status = psa_mac_abort( operation );
3605 if( status == PSA_SUCCESS )
3606 *mac_length = operation->mac_size;
3607 else
3608 memset( mac, '!', mac_size );
3609 }
3610 else
3611 psa_mac_abort( operation );
3612 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003613}
3614
Gilles Peskineacd4be32018-07-08 19:56:25 +02003615psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3616 const uint8_t *mac,
3617 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003618{
Gilles Peskine828ed142018-06-18 23:25:51 +02003619 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003620 psa_status_t status;
3621
Jaeden Amero252ef282019-02-15 14:05:35 +00003622 if( operation->alg == 0 )
3623 {
3624 return( PSA_ERROR_BAD_STATE );
3625 }
3626
Gilles Peskine89167cb2018-07-08 20:12:23 +02003627 if( operation->is_sign )
3628 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003629 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003630 }
3631 if( operation->mac_size != mac_length )
3632 {
3633 status = PSA_ERROR_INVALID_SIGNATURE;
3634 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003635 }
mohammad16036df908f2018-04-02 08:34:15 -07003636
3637 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003638 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003639 if( status != PSA_SUCCESS )
3640 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003641
3642 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3643 status = PSA_ERROR_INVALID_SIGNATURE;
3644
3645cleanup:
3646 if( status == PSA_SUCCESS )
3647 status = psa_mac_abort( operation );
3648 else
3649 psa_mac_abort( operation );
3650
Gilles Peskine3f108122018-12-07 18:14:53 +01003651 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003652
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003653 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003654}
3655
3656
Gilles Peskine20035e32018-02-03 22:44:14 +01003657
Gilles Peskine20035e32018-02-03 22:44:14 +01003658/****************************************************************/
3659/* Asymmetric cryptography */
3660/****************************************************************/
3661
John Durkop6ba40d12020-11-10 08:50:04 -08003662#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3663 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003664/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003665 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003666static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3667 size_t hash_length,
3668 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003669{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003670 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003671 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003672 *md_alg = mbedtls_md_get_type( md_info );
3673
3674 /* The Mbed TLS RSA module uses an unsigned int for hash length
3675 * parameters. Validate that it fits so that we don't risk an
3676 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003677#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003678 if( hash_length > UINT_MAX )
3679 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003680#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003681
John Durkop0e005192020-10-31 22:06:54 -07003682#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003683 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3684 * must be correct. */
3685 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3686 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003687 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003688 if( md_info == NULL )
3689 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003690 if( mbedtls_md_get_size( md_info ) != hash_length )
3691 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003692 }
John Durkop0e005192020-10-31 22:06:54 -07003693#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003694
John Durkop0e005192020-10-31 22:06:54 -07003695#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003696 /* PSS requires a hash internally. */
3697 if( PSA_ALG_IS_RSA_PSS( alg ) )
3698 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003699 if( md_info == NULL )
3700 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003701 }
John Durkop0e005192020-10-31 22:06:54 -07003702#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003703
Gilles Peskine61b91d42018-06-08 16:09:36 +02003704 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003705}
3706
Gilles Peskine2b450e32018-06-27 15:42:46 +02003707static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3708 psa_algorithm_t alg,
3709 const uint8_t *hash,
3710 size_t hash_length,
3711 uint8_t *signature,
3712 size_t signature_size,
3713 size_t *signature_length )
3714{
3715 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003717 mbedtls_md_type_t md_alg;
3718
3719 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3720 if( status != PSA_SUCCESS )
3721 return( status );
3722
Gilles Peskine630a18a2018-06-29 17:49:35 +02003723 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003724 return( PSA_ERROR_BUFFER_TOO_SMALL );
3725
John Durkop0e005192020-10-31 22:06:54 -07003726#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003727 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3728 {
3729 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3730 MBEDTLS_MD_NONE );
3731 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003732 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003733 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003734 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003735 md_alg,
3736 (unsigned int) hash_length,
3737 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003738 signature );
3739 }
3740 else
John Durkop0e005192020-10-31 22:06:54 -07003741#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3742#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003743 if( PSA_ALG_IS_RSA_PSS( alg ) )
3744 {
3745 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3746 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003747 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003748 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003749 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003750 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003751 (unsigned int) hash_length,
3752 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003753 signature );
3754 }
3755 else
John Durkop0e005192020-10-31 22:06:54 -07003756#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003757 {
3758 return( PSA_ERROR_INVALID_ARGUMENT );
3759 }
3760
3761 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003762 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003763 return( mbedtls_to_psa_error( ret ) );
3764}
3765
3766static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3767 psa_algorithm_t alg,
3768 const uint8_t *hash,
3769 size_t hash_length,
3770 const uint8_t *signature,
3771 size_t signature_length )
3772{
3773 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003775 mbedtls_md_type_t md_alg;
3776
3777 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3778 if( status != PSA_SUCCESS )
3779 return( status );
3780
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003781 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3782 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003783
John Durkop0e005192020-10-31 22:06:54 -07003784#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003785 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3786 {
3787 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3788 MBEDTLS_MD_NONE );
3789 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003790 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003791 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003792 MBEDTLS_RSA_PUBLIC,
3793 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003794 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003795 hash,
3796 signature );
3797 }
3798 else
John Durkop0e005192020-10-31 22:06:54 -07003799#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3800#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003801 if( PSA_ALG_IS_RSA_PSS( alg ) )
3802 {
3803 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3804 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003805 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003806 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003807 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003808 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003809 (unsigned int) hash_length,
3810 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003811 signature );
3812 }
3813 else
John Durkop0e005192020-10-31 22:06:54 -07003814#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003815 {
3816 return( PSA_ERROR_INVALID_ARGUMENT );
3817 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003818
3819 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3820 * the rest of the signature is invalid". This has little use in
3821 * practice and PSA doesn't report this distinction. */
3822 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3823 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003824 return( mbedtls_to_psa_error( ret ) );
3825}
John Durkop6ba40d12020-11-10 08:50:04 -08003826#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3827 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003828
John Durkop6ba40d12020-11-10 08:50:04 -08003829#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3830 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003831/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3832 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3833 * (even though these functions don't modify it). */
3834static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3835 psa_algorithm_t alg,
3836 const uint8_t *hash,
3837 size_t hash_length,
3838 uint8_t *signature,
3839 size_t signature_size,
3840 size_t *signature_length )
3841{
Janos Follath24eed8d2019-11-22 13:21:35 +00003842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003843 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003844 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003845 mbedtls_mpi_init( &r );
3846 mbedtls_mpi_init( &s );
3847
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003848 if( signature_size < 2 * curve_bytes )
3849 {
3850 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3851 goto cleanup;
3852 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003853
John Durkop0ea39e02020-10-13 19:58:20 -07003854#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003855 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3856 {
3857 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3858 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3859 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003860 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3861 &ecp->d, hash,
3862 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003863 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003864 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003865 }
3866 else
John Durkop0ea39e02020-10-13 19:58:20 -07003867#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003868 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003869 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003870 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3871 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003872 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003873 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003874 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003875
3876 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3877 signature,
3878 curve_bytes ) );
3879 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3880 signature + curve_bytes,
3881 curve_bytes ) );
3882
3883cleanup:
3884 mbedtls_mpi_free( &r );
3885 mbedtls_mpi_free( &s );
3886 if( ret == 0 )
3887 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003888 return( mbedtls_to_psa_error( ret ) );
3889}
3890
3891static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3892 const uint8_t *hash,
3893 size_t hash_length,
3894 const uint8_t *signature,
3895 size_t signature_length )
3896{
Janos Follath24eed8d2019-11-22 13:21:35 +00003897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003898 mbedtls_mpi r, s;
3899 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3900 mbedtls_mpi_init( &r );
3901 mbedtls_mpi_init( &s );
3902
3903 if( signature_length != 2 * curve_bytes )
3904 return( PSA_ERROR_INVALID_SIGNATURE );
3905
3906 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3907 signature,
3908 curve_bytes ) );
3909 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3910 signature + curve_bytes,
3911 curve_bytes ) );
3912
Steven Cooremanacda8342020-07-24 23:09:52 +02003913 /* Check whether the public part is loaded. If not, load it. */
3914 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3915 {
3916 MBEDTLS_MPI_CHK(
3917 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003918 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003919 }
3920
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003921 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3922 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003923
3924cleanup:
3925 mbedtls_mpi_free( &r );
3926 mbedtls_mpi_free( &s );
3927 return( mbedtls_to_psa_error( ret ) );
3928}
John Durkop6ba40d12020-11-10 08:50:04 -08003929#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3930 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003931
Ronald Croncf56a0a2020-08-04 09:51:30 +02003932psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003933 psa_algorithm_t alg,
3934 const uint8_t *hash,
3935 size_t hash_length,
3936 uint8_t *signature,
3937 size_t signature_size,
3938 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003939{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003940 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003941 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003942 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003943
3944 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003945 /* Immediately reject a zero-length signature buffer. This guarantees
3946 * that signature must be a valid pointer. (On the other hand, the hash
3947 * buffer can in principle be empty since it doesn't actually have
3948 * to be a hash.) */
3949 if( signature_size == 0 )
3950 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003951
Ronald Cron5c522922020-11-14 16:35:34 +01003952 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3953 PSA_KEY_USAGE_SIGN_HASH,
3954 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003955 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003956 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003957 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003958 {
3959 status = PSA_ERROR_INVALID_ARGUMENT;
3960 goto exit;
3961 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003962
Steven Cooremancd84cb42020-07-16 20:28:36 +02003963 /* Try any of the available accelerators first */
3964 status = psa_driver_wrapper_sign_hash( slot,
3965 alg,
3966 hash,
3967 hash_length,
3968 signature,
3969 signature_size,
3970 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003971 if( status != PSA_ERROR_NOT_SUPPORTED ||
3972 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003973 goto exit;
3974
Steven Cooreman7a250572020-07-17 16:43:05 +02003975 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003976#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3977 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003978 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003979 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003980 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003981
Ronald Cron90857082020-11-25 14:58:33 +01003982 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3983 slot->key.data,
3984 slot->key.bytes,
3985 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003986 if( status != PSA_SUCCESS )
3987 goto exit;
3988
Steven Cooremana2371e52020-07-28 14:30:39 +02003989 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003990 alg,
3991 hash, hash_length,
3992 signature, signature_size,
3993 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003994
Steven Cooremana2371e52020-07-28 14:30:39 +02003995 mbedtls_rsa_free( rsa );
3996 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003997 }
3998 else
John Durkop6ba40d12020-11-10 08:50:04 -08003999#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4000 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004001 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01004002 {
John Durkop9814fa22020-11-04 12:28:15 -08004003#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4004 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01004005 if(
John Durkop0ea39e02020-10-13 19:58:20 -07004006#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01004007 PSA_ALG_IS_ECDSA( alg )
4008#else
4009 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
4010#endif
4011 )
Steven Cooremanacda8342020-07-24 23:09:52 +02004012 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004013 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004014 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
4015 slot->key.data,
4016 slot->key.bytes,
4017 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004018 if( status != PSA_SUCCESS )
4019 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004020 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004021 alg,
4022 hash, hash_length,
4023 signature, signature_size,
4024 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004025 mbedtls_ecp_keypair_free( ecp );
4026 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004027 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004028 else
John Durkop9814fa22020-11-04 12:28:15 -08004029#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4030 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004031 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004032 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004033 }
itayzafrir5c753392018-05-08 11:18:38 +03004034 }
4035 else
itayzafrir5c753392018-05-08 11:18:38 +03004036 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004037 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004038 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004039
4040exit:
4041 /* Fill the unused part of the output buffer (the whole buffer on error,
4042 * the trailing part on success) with something that isn't a valid mac
4043 * (barring an attack on the mac and deliberately-crafted input),
4044 * in case the caller doesn't check the return status properly. */
4045 if( status == PSA_SUCCESS )
4046 memset( signature + *signature_length, '!',
4047 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02004048 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004049 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004050 /* If signature_size is 0 then we have nothing to do. We must not call
4051 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02004052
Ronald Cron5c522922020-11-14 16:35:34 +01004053 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004054
Ronald Cron5c522922020-11-14 16:35:34 +01004055 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03004056}
4057
Ronald Croncf56a0a2020-08-04 09:51:30 +02004058psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004059 psa_algorithm_t alg,
4060 const uint8_t *hash,
4061 size_t hash_length,
4062 const uint8_t *signature,
4063 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004064{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004065 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004066 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004067 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004068
Ronald Cron5c522922020-11-14 16:35:34 +01004069 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4070 PSA_KEY_USAGE_VERIFY_HASH,
4071 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004072 if( status != PSA_SUCCESS )
4073 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004074
Steven Cooreman55ae2172020-07-17 19:46:15 +02004075 /* Try any of the available accelerators first */
4076 status = psa_driver_wrapper_verify_hash( slot,
4077 alg,
4078 hash,
4079 hash_length,
4080 signature,
4081 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004082 if( status != PSA_ERROR_NOT_SUPPORTED ||
4083 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004084 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004085
John Durkop6ba40d12020-11-10 08:50:04 -08004086#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4087 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004088 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004089 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004090 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004091
Ronald Cron90857082020-11-25 14:58:33 +01004092 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
4093 slot->key.data,
4094 slot->key.bytes,
4095 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004096 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004097 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004098
Steven Cooremana2371e52020-07-28 14:30:39 +02004099 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004100 alg,
4101 hash, hash_length,
4102 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004103 mbedtls_rsa_free( rsa );
4104 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004105 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004106 }
4107 else
John Durkop6ba40d12020-11-10 08:50:04 -08004108#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4109 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004110 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004111 {
John Durkop9814fa22020-11-04 12:28:15 -08004112#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4113 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004114 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004115 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004116 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004117 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
4118 slot->key.data,
4119 slot->key.bytes,
4120 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004121 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004122 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004123 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004124 hash, hash_length,
4125 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004126 mbedtls_ecp_keypair_free( ecp );
4127 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004128 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004129 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004130 else
John Durkop9814fa22020-11-04 12:28:15 -08004131#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4132 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004133 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004134 status = PSA_ERROR_INVALID_ARGUMENT;
4135 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004136 }
itayzafrir5c753392018-05-08 11:18:38 +03004137 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004138 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004139 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004140 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004141 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004142
4143exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004144 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004145
Ronald Cron5c522922020-11-14 16:35:34 +01004146 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004147}
4148
John Durkop0e005192020-10-31 22:06:54 -07004149#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004150static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4151 mbedtls_rsa_context *rsa )
4152{
4153 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4154 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4155 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4156 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4157}
John Durkop0e005192020-10-31 22:06:54 -07004158#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004159
Ronald Croncf56a0a2020-08-04 09:51:30 +02004160psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004161 psa_algorithm_t alg,
4162 const uint8_t *input,
4163 size_t input_length,
4164 const uint8_t *salt,
4165 size_t salt_length,
4166 uint8_t *output,
4167 size_t output_size,
4168 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004169{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004171 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004172 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004173
Darryl Green5cc689a2018-07-24 15:34:10 +01004174 (void) input;
4175 (void) input_length;
4176 (void) salt;
4177 (void) output;
4178 (void) output_size;
4179
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004180 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004181
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004182 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4183 return( PSA_ERROR_INVALID_ARGUMENT );
4184
Ronald Cron5c522922020-11-14 16:35:34 +01004185 status = psa_get_and_lock_transparent_key_slot_with_policy(
4186 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004187 if( status != PSA_SUCCESS )
4188 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004189 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4190 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004191 {
4192 status = PSA_ERROR_INVALID_ARGUMENT;
4193 goto exit;
4194 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195
John Durkop6ba40d12020-11-10 08:50:04 -08004196#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4197 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004198 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004200 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004201 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
4202 slot->key.data,
4203 slot->key.bytes,
4204 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004205 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004206 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004207
4208 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004209 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004210 status = PSA_ERROR_BUFFER_TOO_SMALL;
4211 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004212 }
John Durkop0e005192020-10-31 22:06:54 -07004213#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004214 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004215 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004216 status = mbedtls_to_psa_error(
4217 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004218 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004219 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004220 MBEDTLS_RSA_PUBLIC,
4221 input_length,
4222 input,
4223 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004224 }
4225 else
John Durkop0e005192020-10-31 22:06:54 -07004226#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4227#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004228 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004229 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004230 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004231 status = mbedtls_to_psa_error(
4232 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004233 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004234 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004235 MBEDTLS_RSA_PUBLIC,
4236 salt, salt_length,
4237 input_length,
4238 input,
4239 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004240 }
4241 else
John Durkop0e005192020-10-31 22:06:54 -07004242#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004243 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004244 status = PSA_ERROR_INVALID_ARGUMENT;
4245 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004246 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004247rsa_exit:
4248 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004249 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004250
Steven Cooremana2371e52020-07-28 14:30:39 +02004251 mbedtls_rsa_free( rsa );
4252 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004253 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004254 else
John Durkop9814fa22020-11-04 12:28:15 -08004255#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004256 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004257 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004258 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004259 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004260
4261exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004262 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004263
Ronald Cron5c522922020-11-14 16:35:34 +01004264 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004265}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004266
Ronald Croncf56a0a2020-08-04 09:51:30 +02004267psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004268 psa_algorithm_t alg,
4269 const uint8_t *input,
4270 size_t input_length,
4271 const uint8_t *salt,
4272 size_t salt_length,
4273 uint8_t *output,
4274 size_t output_size,
4275 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004276{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004277 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004278 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004279 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004280
Darryl Green5cc689a2018-07-24 15:34:10 +01004281 (void) input;
4282 (void) input_length;
4283 (void) salt;
4284 (void) output;
4285 (void) output_size;
4286
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004287 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004288
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004289 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4290 return( PSA_ERROR_INVALID_ARGUMENT );
4291
Ronald Cron5c522922020-11-14 16:35:34 +01004292 status = psa_get_and_lock_transparent_key_slot_with_policy(
4293 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004294 if( status != PSA_SUCCESS )
4295 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004296 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004297 {
4298 status = PSA_ERROR_INVALID_ARGUMENT;
4299 goto exit;
4300 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004301
John Durkop6ba40d12020-11-10 08:50:04 -08004302#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4303 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004304 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004305 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004306 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004307 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
4308 slot->key.data,
4309 slot->key.bytes,
4310 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004311 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004312 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004313
Steven Cooremana2371e52020-07-28 14:30:39 +02004314 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004315 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004316 status = PSA_ERROR_INVALID_ARGUMENT;
4317 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004318 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004319
John Durkop0e005192020-10-31 22:06:54 -07004320#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004321 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004322 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004323 status = mbedtls_to_psa_error(
4324 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004325 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004326 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004327 MBEDTLS_RSA_PRIVATE,
4328 output_length,
4329 input,
4330 output,
4331 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004332 }
4333 else
John Durkop0e005192020-10-31 22:06:54 -07004334#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4335#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004336 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004337 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004338 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004339 status = mbedtls_to_psa_error(
4340 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004341 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004342 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004343 MBEDTLS_RSA_PRIVATE,
4344 salt, salt_length,
4345 output_length,
4346 input,
4347 output,
4348 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004349 }
4350 else
John Durkop0e005192020-10-31 22:06:54 -07004351#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004352 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004353 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004354 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004355
Steven Cooreman4fed4552020-08-03 14:46:03 +02004356rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004357 mbedtls_rsa_free( rsa );
4358 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004359 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004360 else
John Durkop6ba40d12020-11-10 08:50:04 -08004361#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4362 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004363 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004364 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004365 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004366
4367exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004368 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004369
Ronald Cron5c522922020-11-14 16:35:34 +01004370 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004371}
Gilles Peskine20035e32018-02-03 22:44:14 +01004372
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004373
4374
mohammad1603503973b2018-03-12 15:59:30 +02004375/****************************************************************/
4376/* Symmetric cryptography */
4377/****************************************************************/
4378
Gilles Peskinee553c652018-06-04 16:22:46 +02004379static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004380 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004381 psa_algorithm_t alg,
4382 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004383{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004384 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004385 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004386 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004387 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004388 size_t key_bits;
4389 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004390 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4391 PSA_KEY_USAGE_ENCRYPT :
4392 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004393
Steven Cooremand3feccd2020-09-01 15:56:14 +02004394 /* A context must be freshly initialized before it can be set up. */
4395 if( operation->alg != 0 )
4396 return( PSA_ERROR_BAD_STATE );
4397
Steven Cooremana07b9972020-09-10 14:54:14 +02004398 /* The requested algorithm must be one that can be processed by cipher. */
4399 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004400 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004401
Steven Cooremanef8575e2020-09-11 11:44:50 +02004402 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004403 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004404 if( status != PSA_SUCCESS )
4405 goto exit;
4406
4407 /* Initialize the operation struct members, except for alg. The alg member
4408 * is used to indicate to psa_cipher_abort that there are resources to free,
4409 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004410 operation->key_set = 0;
4411 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004412 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004413 operation->iv_size = 0;
4414 operation->block_size = 0;
4415 if( alg == PSA_ALG_ECB_NO_PADDING )
4416 operation->iv_required = 0;
4417 else
4418 operation->iv_required = 1;
4419
Steven Cooremana07b9972020-09-10 14:54:14 +02004420 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004421 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004422 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004423 slot,
4424 alg );
4425 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004426 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004427 slot,
4428 alg );
4429
Steven Cooremanef8575e2020-09-11 11:44:50 +02004430 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004431 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004432 /* Once the driver context is initialised, it needs to be freed using
4433 * psa_cipher_abort. Indicate this through setting alg. */
4434 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004435 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004436
Steven Cooremand3feccd2020-09-01 15:56:14 +02004437 if( status != PSA_ERROR_NOT_SUPPORTED ||
4438 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4439 goto exit;
4440
Steven Cooremana07b9972020-09-10 14:54:14 +02004441 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004442 * available for the given algorithm & key. */
4443 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004444
Steven Cooremana07b9972020-09-10 14:54:14 +02004445 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004446 * psa_cipher_abort. Indicate there is something to be freed through setting
4447 * alg, and indicate the operation is being done using mbedtls crypto through
4448 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004449 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004450 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004451
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004452 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004453 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004454 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004455 {
4456 status = PSA_ERROR_NOT_SUPPORTED;
4457 goto exit;
4458 }
mohammad1603503973b2018-03-12 15:59:30 +02004459
mohammad1603503973b2018-03-12 15:59:30 +02004460 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004461 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004462 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004463
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004464#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004465 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004466 {
4467 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004468 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004469 memcpy( keys, slot->key.data, 16 );
4470 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004471 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4472 keys,
4473 192, cipher_operation );
4474 }
4475 else
4476#endif
4477 {
4478 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004479 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004480 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004481 }
Moran Peker41deec42018-04-04 15:43:05 +03004482 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004483 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004484
mohammad16038481e742018-03-18 13:57:31 +02004485#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004486 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004487 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004488 case PSA_ALG_CBC_NO_PADDING:
4489 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4490 MBEDTLS_PADDING_NONE );
4491 break;
4492 case PSA_ALG_CBC_PKCS7:
4493 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4494 MBEDTLS_PADDING_PKCS7 );
4495 break;
4496 default:
4497 /* The algorithm doesn't involve padding. */
4498 ret = 0;
4499 break;
4500 }
4501 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004502 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004503#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4504
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004505 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004506 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004507 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4508 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004509 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004510 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004511 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004512#if defined(MBEDTLS_CHACHA20_C)
4513 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004514 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004515 operation->iv_size = 12;
4516#endif
mohammad1603503973b2018-03-12 15:59:30 +02004517
Steven Cooreman7df02922020-09-09 15:28:49 +02004518 status = PSA_SUCCESS;
4519
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004520exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004521 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004522 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004523 if( status == PSA_SUCCESS )
4524 {
4525 /* Update operation flags for both driver and software implementations */
4526 operation->key_set = 1;
4527 }
4528 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004529 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004530
Ronald Cron5c522922020-11-14 16:35:34 +01004531 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004532
Ronald Cron5c522922020-11-14 16:35:34 +01004533 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004534}
4535
Gilles Peskinefe119512018-07-08 21:39:34 +02004536psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004537 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004538 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004539{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004540 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004541}
4542
Gilles Peskinefe119512018-07-08 21:39:34 +02004543psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004544 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004545 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004546{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004547 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004548}
4549
Gilles Peskinefe119512018-07-08 21:39:34 +02004550psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004551 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004552 size_t iv_size,
4553 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004554{
itayzafrir534bd7c2018-08-02 13:56:32 +03004555 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004557 if( operation->iv_set || ! operation->iv_required )
4558 {
4559 return( PSA_ERROR_BAD_STATE );
4560 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004561
Steven Cooremanef8575e2020-09-11 11:44:50 +02004562 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004563 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004564 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004565 iv,
4566 iv_size,
4567 iv_length );
4568 goto exit;
4569 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004570
Moran Peker41deec42018-04-04 15:43:05 +03004571 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004572 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004573 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004574 goto exit;
4575 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004576 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004577 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004578 if( ret != 0 )
4579 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004580 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004581 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004582 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004583
mohammad16038481e742018-03-18 13:57:31 +02004584 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004585 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004586
Moran Peker395db872018-05-31 14:07:14 +03004587exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004588 if( status == PSA_SUCCESS )
4589 operation->iv_set = 1;
4590 else
Moran Peker395db872018-05-31 14:07:14 +03004591 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004592 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004593}
4594
Gilles Peskinefe119512018-07-08 21:39:34 +02004595psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004596 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004597 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004598{
itayzafrir534bd7c2018-08-02 13:56:32 +03004599 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004601 if( operation->iv_set || ! operation->iv_required )
4602 {
4603 return( PSA_ERROR_BAD_STATE );
4604 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004605
Steven Cooremanef8575e2020-09-11 11:44:50 +02004606 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004607 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004608 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004609 iv,
4610 iv_length );
4611 goto exit;
4612 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004613
Moran Pekera28258c2018-05-29 16:25:04 +03004614 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004615 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004616 status = PSA_ERROR_INVALID_ARGUMENT;
4617 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004618 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004619 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4620 status = mbedtls_to_psa_error( ret );
4621exit:
4622 if( status == PSA_SUCCESS )
4623 operation->iv_set = 1;
4624 else
mohammad1603503973b2018-03-12 15:59:30 +02004625 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004626 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004627}
4628
Steven Cooreman177deba2020-09-07 17:14:14 +02004629/* Process input for which the algorithm is set to ECB mode. This requires
4630 * manual processing, since the PSA API is defined as being able to process
4631 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4632 * underlying mbedtls_cipher_update only takes full blocks. */
4633static psa_status_t psa_cipher_update_ecb_internal(
4634 mbedtls_cipher_context_t *ctx,
4635 const uint8_t *input,
4636 size_t input_length,
4637 uint8_t *output,
4638 size_t output_size,
4639 size_t *output_length )
4640{
4641 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4642 size_t block_size = ctx->cipher_info->block_size;
4643 size_t internal_output_length = 0;
4644 *output_length = 0;
4645
4646 if( input_length == 0 )
4647 {
4648 status = PSA_SUCCESS;
4649 goto exit;
4650 }
4651
4652 if( ctx->unprocessed_len > 0 )
4653 {
4654 /* Fill up to block size, and run the block if there's a full one. */
4655 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4656
4657 if( input_length < bytes_to_copy )
4658 bytes_to_copy = input_length;
4659
4660 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4661 input, bytes_to_copy );
4662 input_length -= bytes_to_copy;
4663 input += bytes_to_copy;
4664 ctx->unprocessed_len += bytes_to_copy;
4665
4666 if( ctx->unprocessed_len == block_size )
4667 {
4668 status = mbedtls_to_psa_error(
4669 mbedtls_cipher_update( ctx,
4670 ctx->unprocessed_data,
4671 block_size,
4672 output, &internal_output_length ) );
4673
4674 if( status != PSA_SUCCESS )
4675 goto exit;
4676
4677 output += internal_output_length;
4678 output_size -= internal_output_length;
4679 *output_length += internal_output_length;
4680 ctx->unprocessed_len = 0;
4681 }
4682 }
4683
4684 while( input_length >= block_size )
4685 {
4686 /* Run all full blocks we have, one by one */
4687 status = mbedtls_to_psa_error(
4688 mbedtls_cipher_update( ctx, input,
4689 block_size,
4690 output, &internal_output_length ) );
4691
4692 if( status != PSA_SUCCESS )
4693 goto exit;
4694
4695 input_length -= block_size;
4696 input += block_size;
4697
4698 output += internal_output_length;
4699 output_size -= internal_output_length;
4700 *output_length += internal_output_length;
4701 }
4702
4703 if( input_length > 0 )
4704 {
4705 /* Save unprocessed bytes for later processing */
4706 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4707 input, input_length );
4708 ctx->unprocessed_len += input_length;
4709 }
4710
4711 status = PSA_SUCCESS;
4712
4713exit:
4714 return( status );
4715}
4716
Gilles Peskinee553c652018-06-04 16:22:46 +02004717psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4718 const uint8_t *input,
4719 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004720 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004721 size_t output_size,
4722 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004723{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004724 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004725 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004726 if( operation->alg == 0 )
4727 {
4728 return( PSA_ERROR_BAD_STATE );
4729 }
4730 if( operation->iv_required && ! operation->iv_set )
4731 {
4732 return( PSA_ERROR_BAD_STATE );
4733 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004734
Steven Cooremanef8575e2020-09-11 11:44:50 +02004735 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004736 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004737 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004738 input,
4739 input_length,
4740 output,
4741 output_size,
4742 output_length );
4743 goto exit;
4744 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004745
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004746 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004747 {
4748 /* Take the unprocessed partial block left over from previous
4749 * update calls, if any, plus the input to this call. Remove
4750 * the last partial block, if any. You get the data that will be
4751 * output in this call. */
4752 expected_output_size =
4753 ( operation->ctx.cipher.unprocessed_len + input_length )
4754 / operation->block_size * operation->block_size;
4755 }
4756 else
4757 {
4758 expected_output_size = input_length;
4759 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004760
Gilles Peskine89d789c2018-06-04 17:17:16 +02004761 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004762 {
4763 status = PSA_ERROR_BUFFER_TOO_SMALL;
4764 goto exit;
4765 }
mohammad160382759612018-03-12 18:16:40 +02004766
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004767 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4768 {
4769 /* mbedtls_cipher_update has an API inconsistency: it will only
4770 * process a single block at a time in ECB mode. Abstract away that
4771 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004772 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4773 input,
4774 input_length,
4775 output,
4776 output_size,
4777 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004778 }
4779 else
4780 {
4781 status = mbedtls_to_psa_error(
4782 mbedtls_cipher_update( &operation->ctx.cipher, input,
4783 input_length, output, output_length ) );
4784 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004785exit:
4786 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004787 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004788 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004789}
4790
Gilles Peskinee553c652018-06-04 16:22:46 +02004791psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4792 uint8_t *output,
4793 size_t output_size,
4794 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004795{
David Saadab4ecc272019-02-14 13:48:10 +02004796 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004797 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004798 if( operation->alg == 0 )
4799 {
4800 return( PSA_ERROR_BAD_STATE );
4801 }
4802 if( operation->iv_required && ! operation->iv_set )
4803 {
4804 return( PSA_ERROR_BAD_STATE );
4805 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004806
Steven Cooremanef8575e2020-09-11 11:44:50 +02004807 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004808 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004809 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004810 output,
4811 output_size,
4812 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004813 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004814 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004815
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004816 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004817 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004818 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004819 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004820 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004821 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004822 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004823 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004824 }
4825
Steven Cooremanef8575e2020-09-11 11:44:50 +02004826 status = mbedtls_to_psa_error(
4827 mbedtls_cipher_finish( &operation->ctx.cipher,
4828 temp_output_buffer,
4829 output_length ) );
4830 if( status != PSA_SUCCESS )
4831 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004832
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004833 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004834 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004835 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004836 memcpy( output, temp_output_buffer, *output_length );
4837 else
Janos Follath315b51c2018-07-09 16:04:51 +01004838 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004839
Steven Cooremanef8575e2020-09-11 11:44:50 +02004840exit:
4841 if( operation->mbedtls_in_use == 1 )
4842 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004843
Steven Cooremanef8575e2020-09-11 11:44:50 +02004844 if( status == PSA_SUCCESS )
4845 return( psa_cipher_abort( operation ) );
4846 else
4847 {
4848 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004849 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004850
Steven Cooremanef8575e2020-09-11 11:44:50 +02004851 return( status );
4852 }
mohammad1603503973b2018-03-12 15:59:30 +02004853}
4854
Gilles Peskinee553c652018-06-04 16:22:46 +02004855psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4856{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004857 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004858 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004859 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004860 * in use. It's ok to call abort on such an object, and there's
4861 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004862 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004863 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004864
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004865 /* Sanity check (shouldn't happen: operation->alg should
4866 * always have been initialized to a valid value). */
4867 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4868 return( PSA_ERROR_BAD_STATE );
4869
Steven Cooremanef8575e2020-09-11 11:44:50 +02004870 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004871 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004872 else
4873 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004874
Moran Peker41deec42018-04-04 15:43:05 +03004875 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004876 operation->key_set = 0;
4877 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004878 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004879 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004880 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004881 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004882
Moran Peker395db872018-05-31 14:07:14 +03004883 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004884}
4885
Gilles Peskinea0655c32018-04-30 17:06:50 +02004886
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004887
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004888
mohammad16035955c982018-04-26 00:53:03 +03004889/****************************************************************/
4890/* AEAD */
4891/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004892
Gilles Peskineedf9a652018-08-17 18:11:56 +02004893typedef struct
4894{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004895 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004896 const mbedtls_cipher_info_t *cipher_info;
4897 union
4898 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004899 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004900#if defined(MBEDTLS_CCM_C)
4901 mbedtls_ccm_context ccm;
4902#endif /* MBEDTLS_CCM_C */
4903#if defined(MBEDTLS_GCM_C)
4904 mbedtls_gcm_context gcm;
4905#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004906#if defined(MBEDTLS_CHACHAPOLY_C)
4907 mbedtls_chachapoly_context chachapoly;
4908#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004909 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004910 psa_algorithm_t core_alg;
4911 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004912 uint8_t tag_length;
4913} aead_operation_t;
4914
Ronald Cronf95a2b12020-10-22 15:24:49 +02004915#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4916
Gilles Peskine30a9e412019-01-14 18:36:12 +01004917static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004918{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004919 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004920 {
4921#if defined(MBEDTLS_CCM_C)
4922 case PSA_ALG_CCM:
4923 mbedtls_ccm_free( &operation->ctx.ccm );
4924 break;
4925#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004926#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004927 case PSA_ALG_GCM:
4928 mbedtls_gcm_free( &operation->ctx.gcm );
4929 break;
4930#endif /* MBEDTLS_GCM_C */
4931 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004932
Ronald Cron5c522922020-11-14 16:35:34 +01004933 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004934}
4935
4936static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004937 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004938 psa_key_usage_t usage,
4939 psa_algorithm_t alg )
4940{
4941 psa_status_t status;
4942 size_t key_bits;
4943 mbedtls_cipher_id_t cipher_id;
4944
Ronald Cron5c522922020-11-14 16:35:34 +01004945 status = psa_get_and_lock_transparent_key_slot_with_policy(
4946 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004947 if( status != PSA_SUCCESS )
4948 return( status );
4949
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004950 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004951
4952 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004953 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004954 &cipher_id );
4955 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004956 {
4957 status = PSA_ERROR_NOT_SUPPORTED;
4958 goto cleanup;
4959 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004960
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004961 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004962 {
4963#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004964 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4965 operation->core_alg = PSA_ALG_CCM;
4966 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004967 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4968 * The call to mbedtls_ccm_encrypt_and_tag or
4969 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004970 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004971 {
4972 status = PSA_ERROR_INVALID_ARGUMENT;
4973 goto cleanup;
4974 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004975 mbedtls_ccm_init( &operation->ctx.ccm );
4976 status = mbedtls_to_psa_error(
4977 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004978 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004979 (unsigned int) key_bits ) );
4980 if( status != 0 )
4981 goto cleanup;
4982 break;
4983#endif /* MBEDTLS_CCM_C */
4984
4985#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004986 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4987 operation->core_alg = PSA_ALG_GCM;
4988 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004989 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4990 * The call to mbedtls_gcm_crypt_and_tag or
4991 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004992 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004993 {
4994 status = PSA_ERROR_INVALID_ARGUMENT;
4995 goto cleanup;
4996 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004997 mbedtls_gcm_init( &operation->ctx.gcm );
4998 status = mbedtls_to_psa_error(
4999 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005000 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02005001 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005002 if( status != 0 )
5003 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005004 break;
5005#endif /* MBEDTLS_GCM_C */
5006
Gilles Peskine26869f22019-05-06 15:25:00 +02005007#if defined(MBEDTLS_CHACHAPOLY_C)
5008 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
5009 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
5010 operation->full_tag_length = 16;
5011 /* We only support the default tag length. */
5012 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02005013 {
5014 status = PSA_ERROR_NOT_SUPPORTED;
5015 goto cleanup;
5016 }
Gilles Peskine26869f22019-05-06 15:25:00 +02005017 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
5018 status = mbedtls_to_psa_error(
5019 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005020 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02005021 if( status != 0 )
5022 goto cleanup;
5023 break;
5024#endif /* MBEDTLS_CHACHAPOLY_C */
5025
Gilles Peskineedf9a652018-08-17 18:11:56 +02005026 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02005027 status = PSA_ERROR_NOT_SUPPORTED;
5028 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005029 }
5030
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005031 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
5032 {
5033 status = PSA_ERROR_INVALID_ARGUMENT;
5034 goto cleanup;
5035 }
5036 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005037
Gilles Peskineedf9a652018-08-17 18:11:56 +02005038 return( PSA_SUCCESS );
5039
5040cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005041 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005042 return( status );
5043}
5044
Ronald Croncf56a0a2020-08-04 09:51:30 +02005045psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005046 psa_algorithm_t alg,
5047 const uint8_t *nonce,
5048 size_t nonce_length,
5049 const uint8_t *additional_data,
5050 size_t additional_data_length,
5051 const uint8_t *plaintext,
5052 size_t plaintext_length,
5053 uint8_t *ciphertext,
5054 size_t ciphertext_size,
5055 size_t *ciphertext_length )
5056{
mohammad16035955c982018-04-26 00:53:03 +03005057 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005058 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03005059 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005060
mohammad1603f08a5502018-06-03 15:05:47 +03005061 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005062
Ronald Croncf56a0a2020-08-04 09:51:30 +02005063 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005064 if( status != PSA_SUCCESS )
5065 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005066
Gilles Peskineedf9a652018-08-17 18:11:56 +02005067 /* For all currently supported modes, the tag is at the end of the
5068 * ciphertext. */
5069 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5070 {
5071 status = PSA_ERROR_BUFFER_TOO_SMALL;
5072 goto exit;
5073 }
5074 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005075
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005076#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005077 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005078 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005079 status = mbedtls_to_psa_error(
5080 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5081 MBEDTLS_GCM_ENCRYPT,
5082 plaintext_length,
5083 nonce, nonce_length,
5084 additional_data, additional_data_length,
5085 plaintext, ciphertext,
5086 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005087 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005088 else
5089#endif /* MBEDTLS_GCM_C */
5090#if defined(MBEDTLS_CCM_C)
5091 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005092 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005093 status = mbedtls_to_psa_error(
5094 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5095 plaintext_length,
5096 nonce, nonce_length,
5097 additional_data,
5098 additional_data_length,
5099 plaintext, ciphertext,
5100 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005101 }
mohammad16035c8845f2018-05-09 05:40:09 -07005102 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005103#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005104#if defined(MBEDTLS_CHACHAPOLY_C)
5105 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5106 {
5107 if( nonce_length != 12 || operation.tag_length != 16 )
5108 {
5109 status = PSA_ERROR_NOT_SUPPORTED;
5110 goto exit;
5111 }
5112 status = mbedtls_to_psa_error(
5113 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5114 plaintext_length,
5115 nonce,
5116 additional_data,
5117 additional_data_length,
5118 plaintext,
5119 ciphertext,
5120 tag ) );
5121 }
5122 else
5123#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005124 {
mohammad1603554faad2018-06-03 15:07:38 +03005125 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005126 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005127
Gilles Peskineedf9a652018-08-17 18:11:56 +02005128 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5129 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005130
Gilles Peskineedf9a652018-08-17 18:11:56 +02005131exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005132 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005133 if( status == PSA_SUCCESS )
5134 *ciphertext_length = plaintext_length + operation.tag_length;
5135 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005136}
5137
Gilles Peskineee652a32018-06-01 19:23:52 +02005138/* Locate the tag in a ciphertext buffer containing the encrypted data
5139 * followed by the tag. Return the length of the part preceding the tag in
5140 * *plaintext_length. This is the size of the plaintext in modes where
5141 * the encrypted data has the same size as the plaintext, such as
5142 * CCM and GCM. */
5143static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5144 const uint8_t *ciphertext,
5145 size_t ciphertext_length,
5146 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005147 const uint8_t **p_tag )
5148{
5149 size_t payload_length;
5150 if( tag_length > ciphertext_length )
5151 return( PSA_ERROR_INVALID_ARGUMENT );
5152 payload_length = ciphertext_length - tag_length;
5153 if( payload_length > plaintext_size )
5154 return( PSA_ERROR_BUFFER_TOO_SMALL );
5155 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005156 return( PSA_SUCCESS );
5157}
5158
Ronald Croncf56a0a2020-08-04 09:51:30 +02005159psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005160 psa_algorithm_t alg,
5161 const uint8_t *nonce,
5162 size_t nonce_length,
5163 const uint8_t *additional_data,
5164 size_t additional_data_length,
5165 const uint8_t *ciphertext,
5166 size_t ciphertext_length,
5167 uint8_t *plaintext,
5168 size_t plaintext_size,
5169 size_t *plaintext_length )
5170{
mohammad16035955c982018-04-26 00:53:03 +03005171 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005172 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005173 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005174
Gilles Peskineee652a32018-06-01 19:23:52 +02005175 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005176
Ronald Croncf56a0a2020-08-04 09:51:30 +02005177 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005178 if( status != PSA_SUCCESS )
5179 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005180
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005181 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5182 ciphertext, ciphertext_length,
5183 plaintext_size, &tag );
5184 if( status != PSA_SUCCESS )
5185 goto exit;
5186
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005187#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005188 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005189 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005190 status = mbedtls_to_psa_error(
5191 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5192 ciphertext_length - operation.tag_length,
5193 nonce, nonce_length,
5194 additional_data,
5195 additional_data_length,
5196 tag, operation.tag_length,
5197 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005198 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005199 else
5200#endif /* MBEDTLS_GCM_C */
5201#if defined(MBEDTLS_CCM_C)
5202 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005203 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005204 status = mbedtls_to_psa_error(
5205 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5206 ciphertext_length - operation.tag_length,
5207 nonce, nonce_length,
5208 additional_data,
5209 additional_data_length,
5210 ciphertext, plaintext,
5211 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005212 }
mohammad160339574652018-06-01 04:39:53 -07005213 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005214#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005215#if defined(MBEDTLS_CHACHAPOLY_C)
5216 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5217 {
5218 if( nonce_length != 12 || operation.tag_length != 16 )
5219 {
5220 status = PSA_ERROR_NOT_SUPPORTED;
5221 goto exit;
5222 }
5223 status = mbedtls_to_psa_error(
5224 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5225 ciphertext_length - operation.tag_length,
5226 nonce,
5227 additional_data,
5228 additional_data_length,
5229 tag,
5230 ciphertext,
5231 plaintext ) );
5232 }
5233 else
5234#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005235 {
mohammad1603554faad2018-06-03 15:07:38 +03005236 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005237 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005238
Gilles Peskineedf9a652018-08-17 18:11:56 +02005239 if( status != PSA_SUCCESS && plaintext_size != 0 )
5240 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005241
Gilles Peskineedf9a652018-08-17 18:11:56 +02005242exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005243 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005244 if( status == PSA_SUCCESS )
5245 *plaintext_length = ciphertext_length - operation.tag_length;
5246 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005247}
5248
Gilles Peskinea0655c32018-04-30 17:06:50 +02005249
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005250
Gilles Peskine20035e32018-02-03 22:44:14 +01005251/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005252/* Generators */
5253/****************************************************************/
5254
John Durkop07cc04a2020-11-16 22:08:34 -08005255#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5256 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5257 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5258#define AT_LEAST_ONE_BUILTIN_KDF
5259#endif
5260
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005261#define HKDF_STATE_INIT 0 /* no input yet */
5262#define HKDF_STATE_STARTED 1 /* got salt */
5263#define HKDF_STATE_KEYED 2 /* got key */
5264#define HKDF_STATE_OUTPUT 3 /* output started */
5265
Gilles Peskinecbe66502019-05-16 16:59:18 +02005266static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005268{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5270 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005271 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005273}
5274
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005276{
5277 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005279 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005280 {
5281 /* The object has (apparently) been initialized but it is not
5282 * in use. It's ok to call abort on such an object, and there's
5283 * nothing to do. */
5284 }
5285 else
John Durkopd0321952020-10-29 21:37:36 -07005286#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005287 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005288 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 mbedtls_free( operation->ctx.hkdf.info );
5290 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005291 }
John Durkop07cc04a2020-11-16 22:08:34 -08005292 else
5293#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5294#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5295 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5296 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005297 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005298 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005299 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005300 if( operation->ctx.tls12_prf.seed != NULL )
5301 {
5302 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5303 operation->ctx.tls12_prf.seed_length );
5304 mbedtls_free( operation->ctx.tls12_prf.seed );
5305 }
5306
5307 if( operation->ctx.tls12_prf.label != NULL )
5308 {
5309 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5310 operation->ctx.tls12_prf.label_length );
5311 mbedtls_free( operation->ctx.tls12_prf.label );
5312 }
5313
5314 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5315
5316 /* We leave the fields Ai and output_block to be erased safely by the
5317 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005318 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005319 else
John Durkop07cc04a2020-11-16 22:08:34 -08005320#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5321 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005322 {
5323 status = PSA_ERROR_BAD_STATE;
5324 }
Janos Follath083036a2019-06-11 10:22:26 +01005325 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005326 return( status );
5327}
5328
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005330 size_t *capacity)
5331{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005333 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005335 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005336 }
5337
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005339 return( PSA_SUCCESS );
5340}
5341
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005343 size_t capacity )
5344{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005346 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005348 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005349 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005350 return( PSA_SUCCESS );
5351}
5352
John Durkopd0321952020-10-29 21:37:36 -07005353#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005354/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005355 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005356static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005357 psa_algorithm_t hash_alg,
5358 uint8_t *output,
5359 size_t output_length )
5360{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005361 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005362 psa_status_t status;
5363
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005364 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5365 return( PSA_ERROR_BAD_STATE );
5366 hkdf->state = HKDF_STATE_OUTPUT;
5367
Gilles Peskinebef7f142018-07-12 17:22:21 +02005368 while( output_length != 0 )
5369 {
5370 /* Copy what remains of the current block */
5371 uint8_t n = hash_length - hkdf->offset_in_block;
5372 if( n > output_length )
5373 n = (uint8_t) output_length;
5374 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5375 output += n;
5376 output_length -= n;
5377 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005378 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005379 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005380 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005381 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005382 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005383 * object was corrupted or if this function is called directly
5384 * inside the library. */
5385 if( hkdf->block_number == 0xff )
5386 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005387
5388 /* We need a new block */
5389 ++hkdf->block_number;
5390 hkdf->offset_in_block = 0;
5391 status = psa_hmac_setup_internal( &hkdf->hmac,
5392 hkdf->prk, hash_length,
5393 hash_alg );
5394 if( status != PSA_SUCCESS )
5395 return( status );
5396 if( hkdf->block_number != 1 )
5397 {
5398 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5399 hkdf->output_block,
5400 hash_length );
5401 if( status != PSA_SUCCESS )
5402 return( status );
5403 }
5404 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5405 hkdf->info,
5406 hkdf->info_length );
5407 if( status != PSA_SUCCESS )
5408 return( status );
5409 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5410 &hkdf->block_number, 1 );
5411 if( status != PSA_SUCCESS )
5412 return( status );
5413 status = psa_hmac_finish_internal( &hkdf->hmac,
5414 hkdf->output_block,
5415 sizeof( hkdf->output_block ) );
5416 if( status != PSA_SUCCESS )
5417 return( status );
5418 }
5419
5420 return( PSA_SUCCESS );
5421}
John Durkop07cc04a2020-11-16 22:08:34 -08005422#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005423
John Durkop07cc04a2020-11-16 22:08:34 -08005424#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5425 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005426static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5427 psa_tls12_prf_key_derivation_t *tls12_prf,
5428 psa_algorithm_t alg )
5429{
5430 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005431 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005432 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5433 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005434
Janos Follath7742fee2019-06-17 12:58:10 +01005435 /* We can't be wanting more output after block 0xff, otherwise
5436 * the capacity check in psa_key_derivation_output_bytes() would have
5437 * prevented this call. It could happen only if the operation
5438 * object was corrupted or if this function is called directly
5439 * inside the library. */
5440 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005441 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005442
5443 /* We need a new block */
5444 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005445 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005446
5447 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5448 *
5449 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5450 *
5451 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5452 * HMAC_hash(secret, A(2) + seed) +
5453 * HMAC_hash(secret, A(3) + seed) + ...
5454 *
5455 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005456 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005457 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005458 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005459 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005460 * is currently extracted as `output_block` and where i is
5461 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005462 */
5463
Janos Follathea29bfb2019-06-19 12:21:20 +01005464 /* Save the hash context before using it, to preserve the hash state with
5465 * only the inner padding in it. We need this, because inner padding depends
5466 * on the key (secret in the RFC's terminology). */
5467 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5468 if( status != PSA_SUCCESS )
5469 goto cleanup;
5470
5471 /* Calculate A(i) where i = tls12_prf->block_number. */
5472 if( tls12_prf->block_number == 1 )
5473 {
5474 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5475 * the variable seed and in this instance means it in the context of the
5476 * P_hash function, where seed = label + seed.) */
5477 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5478 tls12_prf->label, tls12_prf->label_length );
5479 if( status != PSA_SUCCESS )
5480 goto cleanup;
5481 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5482 tls12_prf->seed, tls12_prf->seed_length );
5483 if( status != PSA_SUCCESS )
5484 goto cleanup;
5485 }
5486 else
5487 {
5488 /* A(i) = HMAC_hash(secret, A(i-1)) */
5489 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5490 tls12_prf->Ai, hash_length );
5491 if( status != PSA_SUCCESS )
5492 goto cleanup;
5493 }
5494
5495 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5496 tls12_prf->Ai, hash_length );
5497 if( status != PSA_SUCCESS )
5498 goto cleanup;
5499 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5500 if( status != PSA_SUCCESS )
5501 goto cleanup;
5502
5503 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5504 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5505 tls12_prf->Ai, hash_length );
5506 if( status != PSA_SUCCESS )
5507 goto cleanup;
5508 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5509 tls12_prf->label, tls12_prf->label_length );
5510 if( status != PSA_SUCCESS )
5511 goto cleanup;
5512 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5513 tls12_prf->seed, tls12_prf->seed_length );
5514 if( status != PSA_SUCCESS )
5515 goto cleanup;
5516 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5517 tls12_prf->output_block, hash_length );
5518 if( status != PSA_SUCCESS )
5519 goto cleanup;
5520 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5521 if( status != PSA_SUCCESS )
5522 goto cleanup;
5523
Janos Follath7742fee2019-06-17 12:58:10 +01005524
5525cleanup:
5526
Janos Follathea29bfb2019-06-19 12:21:20 +01005527 cleanup_status = psa_hash_abort( &backup );
5528 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5529 status = cleanup_status;
5530
5531 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005532}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005533
Janos Follath844eb0e2019-06-19 12:10:49 +01005534static psa_status_t psa_key_derivation_tls12_prf_read(
5535 psa_tls12_prf_key_derivation_t *tls12_prf,
5536 psa_algorithm_t alg,
5537 uint8_t *output,
5538 size_t output_length )
5539{
5540 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005541 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005542 psa_status_t status;
5543 uint8_t offset, length;
5544
5545 while( output_length != 0 )
5546 {
5547 /* Check if we have fully processed the current block. */
5548 if( tls12_prf->left_in_block == 0 )
5549 {
5550 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5551 alg );
5552 if( status != PSA_SUCCESS )
5553 return( status );
5554
5555 continue;
5556 }
5557
5558 if( tls12_prf->left_in_block > output_length )
5559 length = (uint8_t) output_length;
5560 else
5561 length = tls12_prf->left_in_block;
5562
5563 offset = hash_length - tls12_prf->left_in_block;
5564 memcpy( output, tls12_prf->output_block + offset, length );
5565 output += length;
5566 output_length -= length;
5567 tls12_prf->left_in_block -= length;
5568 }
5569
5570 return( PSA_SUCCESS );
5571}
John Durkop07cc04a2020-11-16 22:08:34 -08005572#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5573 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005574
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005575psa_status_t psa_key_derivation_output_bytes(
5576 psa_key_derivation_operation_t *operation,
5577 uint8_t *output,
5578 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005579{
5580 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005581 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005582
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005583 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005584 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005585 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005586 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005587 }
5588
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005589 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005590 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005591 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005592 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005593 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005594 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005595 goto exit;
5596 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005597 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005598 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005599 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005600 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005601 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5602 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005603 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005604 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005605 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005606 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005607 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005608
John Durkopd0321952020-10-29 21:37:36 -07005609#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005610 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005611 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005612 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005613 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005614 output, output_length );
5615 }
Janos Follathadbec812019-06-14 11:05:39 +01005616 else
John Durkop07cc04a2020-11-16 22:08:34 -08005617#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5618#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5619 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005620 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005621 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005622 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005623 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005624 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005625 output_length );
5626 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005627 else
John Durkop07cc04a2020-11-16 22:08:34 -08005628#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5629 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005630 {
5631 return( PSA_ERROR_BAD_STATE );
5632 }
5633
5634exit:
5635 if( status != PSA_SUCCESS )
5636 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005637 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005638 * This allows us to differentiate between exhausted operations and
5639 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5640 * operations. */
5641 psa_algorithm_t alg = operation->alg;
5642 psa_key_derivation_abort( operation );
5643 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005644 memset( output, '!', output_length );
5645 }
5646 return( status );
5647}
5648
Gilles Peskine08542d82018-07-19 17:05:42 +02005649#if defined(MBEDTLS_DES_C)
5650static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5651{
5652 if( data_size >= 8 )
5653 mbedtls_des_key_set_parity( data );
5654 if( data_size >= 16 )
5655 mbedtls_des_key_set_parity( data + 8 );
5656 if( data_size >= 24 )
5657 mbedtls_des_key_set_parity( data + 16 );
5658}
5659#endif /* MBEDTLS_DES_C */
5660
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005661static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005662 psa_key_slot_t *slot,
5663 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005664 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005665{
5666 uint8_t *data = NULL;
5667 size_t bytes = PSA_BITS_TO_BYTES( bits );
5668 psa_status_t status;
5669
Gilles Peskine8e338702019-07-30 20:06:31 +02005670 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005671 return( PSA_ERROR_INVALID_ARGUMENT );
5672 if( bits % 8 != 0 )
5673 return( PSA_ERROR_INVALID_ARGUMENT );
5674 data = mbedtls_calloc( 1, bytes );
5675 if( data == NULL )
5676 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5677
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005678 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005679 if( status != PSA_SUCCESS )
5680 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005681#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005682 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005683 psa_des_set_key_parity( data, bytes );
5684#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005685 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005686
5687exit:
5688 mbedtls_free( data );
5689 return( status );
5690}
5691
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005692psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005693 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005694 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005695{
5696 psa_status_t status;
5697 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005698 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005699
Ronald Cron81709fc2020-11-14 12:10:32 +01005700 *key = MBEDTLS_SVC_KEY_ID_INIT;
5701
Gilles Peskine0f84d622019-09-12 19:03:13 +02005702 /* Reject any attempt to create a zero-length key so that we don't
5703 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5704 if( psa_get_key_bits( attributes ) == 0 )
5705 return( PSA_ERROR_INVALID_ARGUMENT );
5706
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005707 if( ! operation->can_output_key )
5708 return( PSA_ERROR_NOT_PERMITTED );
5709
Ronald Cron81709fc2020-11-14 12:10:32 +01005710 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5711 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005712#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5713 if( driver != NULL )
5714 {
5715 /* Deriving a key in a secure element is not implemented yet. */
5716 status = PSA_ERROR_NOT_SUPPORTED;
5717 }
5718#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005719 if( status == PSA_SUCCESS )
5720 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005721 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005722 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005723 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005724 }
5725 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005726 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005727 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005728 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005729
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005730 return( status );
5731}
5732
Gilles Peskineeab56e42018-07-12 17:12:33 +02005733
5734
5735/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005736/* Key derivation */
5737/****************************************************************/
5738
John Durkop07cc04a2020-11-16 22:08:34 -08005739#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005740static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005741 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005742 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005743{
John Durkop07cc04a2020-11-16 22:08:34 -08005744 int is_kdf_alg_supported;
5745
Janos Follath5fe19732019-06-20 15:09:30 +01005746 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5747 * initialisers for this union leave some bytes unspecified.) */
5748 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5749
Gilles Peskine969c5d62019-01-16 15:53:06 +01005750 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005751#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005752 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5753 is_kdf_alg_supported = 1;
5754 else
5755#endif
5756#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5757 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5758 is_kdf_alg_supported = 1;
5759 else
5760#endif
5761#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5762 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5763 is_kdf_alg_supported = 1;
5764 else
5765#endif
5766 is_kdf_alg_supported = 0;
5767
5768 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005769 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005770 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005771 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005772 if( hash_size == 0 )
5773 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005774 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5775 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005776 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005777 {
5778 return( PSA_ERROR_NOT_SUPPORTED );
5779 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005780 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005781 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005782 }
John Durkop07cc04a2020-11-16 22:08:34 -08005783
5784 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005785}
John Durkop07cc04a2020-11-16 22:08:34 -08005786#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005787
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005788psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005789 psa_algorithm_t alg )
5790{
5791 psa_status_t status;
5792
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005793 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005794 return( PSA_ERROR_BAD_STATE );
5795
Gilles Peskine6843c292019-01-18 16:44:49 +01005796 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5797 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005798#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005799 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005800 {
5801 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005802 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005803 }
5804 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5805 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005806 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005807 }
John Durkop07cc04a2020-11-16 22:08:34 -08005808#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005809 else
5810 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005811
5812 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005813 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005814 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005815}
5816
John Durkopd0321952020-10-29 21:37:36 -07005817#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005818static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005819 psa_algorithm_t hash_alg,
5820 psa_key_derivation_step_t step,
5821 const uint8_t *data,
5822 size_t data_length )
5823{
5824 psa_status_t status;
5825 switch( step )
5826 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005827 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005828 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005829 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005830 status = psa_hmac_setup_internal( &hkdf->hmac,
5831 data, data_length,
5832 hash_alg );
5833 if( status != PSA_SUCCESS )
5834 return( status );
5835 hkdf->state = HKDF_STATE_STARTED;
5836 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005837 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005838 /* If no salt was provided, use an empty salt. */
5839 if( hkdf->state == HKDF_STATE_INIT )
5840 {
5841 status = psa_hmac_setup_internal( &hkdf->hmac,
5842 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005843 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005844 if( status != PSA_SUCCESS )
5845 return( status );
5846 hkdf->state = HKDF_STATE_STARTED;
5847 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005848 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005849 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005850 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5851 data, data_length );
5852 if( status != PSA_SUCCESS )
5853 return( status );
5854 status = psa_hmac_finish_internal( &hkdf->hmac,
5855 hkdf->prk,
5856 sizeof( hkdf->prk ) );
5857 if( status != PSA_SUCCESS )
5858 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005859 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005860 hkdf->block_number = 0;
5861 hkdf->state = HKDF_STATE_KEYED;
5862 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005863 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005864 if( hkdf->state == HKDF_STATE_OUTPUT )
5865 return( PSA_ERROR_BAD_STATE );
5866 if( hkdf->info_set )
5867 return( PSA_ERROR_BAD_STATE );
5868 hkdf->info_length = data_length;
5869 if( data_length != 0 )
5870 {
5871 hkdf->info = mbedtls_calloc( 1, data_length );
5872 if( hkdf->info == NULL )
5873 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5874 memcpy( hkdf->info, data, data_length );
5875 }
5876 hkdf->info_set = 1;
5877 return( PSA_SUCCESS );
5878 default:
5879 return( PSA_ERROR_INVALID_ARGUMENT );
5880 }
5881}
John Durkop07cc04a2020-11-16 22:08:34 -08005882#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005883
John Durkop07cc04a2020-11-16 22:08:34 -08005884#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5885 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005886static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5887 const uint8_t *data,
5888 size_t data_length )
5889{
5890 if( prf->state != TLS12_PRF_STATE_INIT )
5891 return( PSA_ERROR_BAD_STATE );
5892
Janos Follathd6dce9f2019-07-04 09:11:38 +01005893 if( data_length != 0 )
5894 {
5895 prf->seed = mbedtls_calloc( 1, data_length );
5896 if( prf->seed == NULL )
5897 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005898
Janos Follathd6dce9f2019-07-04 09:11:38 +01005899 memcpy( prf->seed, data, data_length );
5900 prf->seed_length = data_length;
5901 }
Janos Follathf08e2652019-06-13 09:05:41 +01005902
5903 prf->state = TLS12_PRF_STATE_SEED_SET;
5904
5905 return( PSA_SUCCESS );
5906}
5907
Janos Follath81550542019-06-13 14:26:34 +01005908static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5909 psa_algorithm_t hash_alg,
5910 const uint8_t *data,
5911 size_t data_length )
5912{
5913 psa_status_t status;
5914 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5915 return( PSA_ERROR_BAD_STATE );
5916
5917 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5918 if( status != PSA_SUCCESS )
5919 return( status );
5920
5921 prf->state = TLS12_PRF_STATE_KEY_SET;
5922
5923 return( PSA_SUCCESS );
5924}
5925
Janos Follath63028dd2019-06-13 09:15:47 +01005926static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5927 const uint8_t *data,
5928 size_t data_length )
5929{
5930 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5931 return( PSA_ERROR_BAD_STATE );
5932
Janos Follathd6dce9f2019-07-04 09:11:38 +01005933 if( data_length != 0 )
5934 {
5935 prf->label = mbedtls_calloc( 1, data_length );
5936 if( prf->label == NULL )
5937 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005938
Janos Follathd6dce9f2019-07-04 09:11:38 +01005939 memcpy( prf->label, data, data_length );
5940 prf->label_length = data_length;
5941 }
Janos Follath63028dd2019-06-13 09:15:47 +01005942
5943 prf->state = TLS12_PRF_STATE_LABEL_SET;
5944
5945 return( PSA_SUCCESS );
5946}
5947
Janos Follathb03233e2019-06-11 15:30:30 +01005948static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5949 psa_algorithm_t hash_alg,
5950 psa_key_derivation_step_t step,
5951 const uint8_t *data,
5952 size_t data_length )
5953{
Janos Follathb03233e2019-06-11 15:30:30 +01005954 switch( step )
5955 {
Janos Follathf08e2652019-06-13 09:05:41 +01005956 case PSA_KEY_DERIVATION_INPUT_SEED:
5957 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005958 case PSA_KEY_DERIVATION_INPUT_SECRET:
5959 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005960 case PSA_KEY_DERIVATION_INPUT_LABEL:
5961 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005962 default:
5963 return( PSA_ERROR_INVALID_ARGUMENT );
5964 }
5965}
John Durkop07cc04a2020-11-16 22:08:34 -08005966#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5967 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5968
5969#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5970static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5971 psa_tls12_prf_key_derivation_t *prf,
5972 psa_algorithm_t hash_alg,
5973 const uint8_t *data,
5974 size_t data_length )
5975{
5976 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005977 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005978 uint8_t *cur = pms;
5979
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005980 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005981 return( PSA_ERROR_INVALID_ARGUMENT );
5982
5983 /* Quoting RFC 4279, Section 2:
5984 *
5985 * The premaster secret is formed as follows: if the PSK is N octets
5986 * long, concatenate a uint16 with the value N, N zero octets, a second
5987 * uint16 with the value N, and the PSK itself.
5988 */
5989
5990 *cur++ = ( data_length >> 8 ) & 0xff;
5991 *cur++ = ( data_length >> 0 ) & 0xff;
5992 memset( cur, 0, data_length );
5993 cur += data_length;
5994 *cur++ = pms[0];
5995 *cur++ = pms[1];
5996 memcpy( cur, data, data_length );
5997 cur += data_length;
5998
5999 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
6000
6001 mbedtls_platform_zeroize( pms, sizeof( pms ) );
6002 return( status );
6003}
Janos Follath6660f0e2019-06-17 08:44:03 +01006004
6005static psa_status_t psa_tls12_prf_psk_to_ms_input(
6006 psa_tls12_prf_key_derivation_t *prf,
6007 psa_algorithm_t hash_alg,
6008 psa_key_derivation_step_t step,
6009 const uint8_t *data,
6010 size_t data_length )
6011{
6012 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01006013 {
Janos Follath6660f0e2019-06-17 08:44:03 +01006014 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
6015 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01006016 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006017
6018 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
6019}
John Durkop07cc04a2020-11-16 22:08:34 -08006020#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006021
Gilles Peskineb8965192019-09-24 16:21:10 +02006022/** Check whether the given key type is acceptable for the given
6023 * input step of a key derivation.
6024 *
6025 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6026 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6027 * Both secret and non-secret inputs can alternatively have the type
6028 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6029 * that the input was passed as a buffer rather than via a key object.
6030 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006031static int psa_key_derivation_check_input_type(
6032 psa_key_derivation_step_t step,
6033 psa_key_type_t key_type )
6034{
6035 switch( step )
6036 {
6037 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02006038 if( key_type == PSA_KEY_TYPE_DERIVE )
6039 return( PSA_SUCCESS );
6040 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006041 return( PSA_SUCCESS );
6042 break;
6043 case PSA_KEY_DERIVATION_INPUT_LABEL:
6044 case PSA_KEY_DERIVATION_INPUT_SALT:
6045 case PSA_KEY_DERIVATION_INPUT_INFO:
6046 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02006047 if( key_type == PSA_KEY_TYPE_RAW_DATA )
6048 return( PSA_SUCCESS );
6049 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006050 return( PSA_SUCCESS );
6051 break;
6052 }
6053 return( PSA_ERROR_INVALID_ARGUMENT );
6054}
6055
Janos Follathb80a94e2019-06-12 15:54:46 +01006056static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006057 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006058 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006059 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006060 const uint8_t *data,
6061 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006062{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006063 psa_status_t status;
6064 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6065
6066 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006067 if( status != PSA_SUCCESS )
6068 goto exit;
6069
John Durkopd0321952020-10-29 21:37:36 -07006070#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006071 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006072 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006073 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006074 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006075 step, data, data_length );
6076 }
John Durkop07cc04a2020-11-16 22:08:34 -08006077 else
6078#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6079#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6080 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006081 {
Janos Follathb03233e2019-06-11 15:30:30 +01006082 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6083 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6084 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006085 }
John Durkop07cc04a2020-11-16 22:08:34 -08006086 else
6087#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6088#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6089 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006090 {
6091 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6092 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6093 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006094 }
6095 else
John Durkop07cc04a2020-11-16 22:08:34 -08006096#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006097 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006098 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006099 return( PSA_ERROR_BAD_STATE );
6100 }
6101
Gilles Peskine224b0d62019-09-23 18:13:17 +02006102exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006103 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006104 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006105 return( status );
6106}
6107
Janos Follath51f4a0f2019-06-14 11:35:55 +01006108psa_status_t psa_key_derivation_input_bytes(
6109 psa_key_derivation_operation_t *operation,
6110 psa_key_derivation_step_t step,
6111 const uint8_t *data,
6112 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006113{
Gilles Peskineb8965192019-09-24 16:21:10 +02006114 return( psa_key_derivation_input_internal( operation, step,
6115 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006116 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006117}
6118
Janos Follath51f4a0f2019-06-14 11:35:55 +01006119psa_status_t psa_key_derivation_input_key(
6120 psa_key_derivation_operation_t *operation,
6121 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006122 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006123{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006124 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006125 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006126 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006127
Ronald Cron5c522922020-11-14 16:35:34 +01006128 status = psa_get_and_lock_transparent_key_slot_with_policy(
6129 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006130 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006131 {
6132 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006133 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006134 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006135
6136 /* Passing a key object as a SECRET input unlocks the permission
6137 * to output to a key object. */
6138 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6139 operation->can_output_key = 1;
6140
Ronald Cronf95a2b12020-10-22 15:24:49 +02006141 status = psa_key_derivation_input_internal( operation,
6142 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01006143 slot->key.data,
6144 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006145
Ronald Cron5c522922020-11-14 16:35:34 +01006146 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006147
Ronald Cron5c522922020-11-14 16:35:34 +01006148 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006149}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006150
6151
6152
6153/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006154/* Key agreement */
6155/****************************************************************/
6156
John Durkop6ba40d12020-11-10 08:50:04 -08006157#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006158static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6159 size_t peer_key_length,
6160 const mbedtls_ecp_keypair *our_key,
6161 uint8_t *shared_secret,
6162 size_t shared_secret_size,
6163 size_t *shared_secret_length )
6164{
Steven Cooremand4867872020-08-05 16:31:39 +02006165 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006166 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006167 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006168 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006169 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006170 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006171
Ronald Cron90857082020-11-25 14:58:33 +01006172 status = mbedtls_psa_ecp_load_representation(
6173 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6174 peer_key,
6175 peer_key_length,
6176 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006177 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006178 goto exit;
6179
Jaeden Amero97271b32019-01-10 19:38:51 +00006180 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006181 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006182 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006183 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006184 status = mbedtls_to_psa_error(
6185 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6186 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006187 goto exit;
6188
Jaeden Amero97271b32019-01-10 19:38:51 +00006189 status = mbedtls_to_psa_error(
6190 mbedtls_ecdh_calc_secret( &ecdh,
6191 shared_secret_length,
6192 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006193 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006194 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006195 if( status != PSA_SUCCESS )
6196 goto exit;
6197 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6198 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006199
6200exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006201 if( status != PSA_SUCCESS )
6202 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006203 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006204 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006205 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006206
Jaeden Amero97271b32019-01-10 19:38:51 +00006207 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006208}
John Durkop6ba40d12020-11-10 08:50:04 -08006209#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006210
Gilles Peskine01d718c2018-09-18 12:01:02 +02006211#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6212
Gilles Peskine0216fe12019-04-11 21:23:21 +02006213static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6214 psa_key_slot_t *private_key,
6215 const uint8_t *peer_key,
6216 size_t peer_key_length,
6217 uint8_t *shared_secret,
6218 size_t shared_secret_size,
6219 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006220{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006221 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006222 {
John Durkop6ba40d12020-11-10 08:50:04 -08006223#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006224 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006225 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006226 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006227 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01006228 psa_status_t status = mbedtls_psa_ecp_load_representation(
6229 private_key->attr.type,
6230 private_key->key.data,
6231 private_key->key.bytes,
6232 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006233 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006234 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006235 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006236 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006237 shared_secret, shared_secret_size,
6238 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006239 mbedtls_ecp_keypair_free( ecp );
6240 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006241 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006242#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006243 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006244 (void) private_key;
6245 (void) peer_key;
6246 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006247 (void) shared_secret;
6248 (void) shared_secret_size;
6249 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006250 return( PSA_ERROR_NOT_SUPPORTED );
6251 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006252}
6253
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006254/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006255 * to potentially free embedded data structures and wipe confidential data.
6256 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006257static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006258 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006259 psa_key_slot_t *private_key,
6260 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006261 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006262{
6263 psa_status_t status;
6264 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6265 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006266 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006267
6268 /* Step 1: run the secret agreement algorithm to generate the shared
6269 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006270 status = psa_key_agreement_raw_internal( ka_alg,
6271 private_key,
6272 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006273 shared_secret,
6274 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006275 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006276 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006277 goto exit;
6278
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006279 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006280 * the shared secret. A shared secret is permitted wherever a key
6281 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006282 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006283 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006284 shared_secret,
6285 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006286exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006287 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006288 return( status );
6289}
6290
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006291psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006292 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006293 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006294 const uint8_t *peer_key,
6295 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006296{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006297 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006298 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006299 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006300
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006301 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006302 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006303 status = psa_get_and_lock_transparent_key_slot_with_policy(
6304 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006305 if( status != PSA_SUCCESS )
6306 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006307 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006308 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006309 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006310 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006311 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006312 else
6313 {
6314 /* If a private key has been added as SECRET, we allow the derived
6315 * key material to be used as a key in PSA Crypto. */
6316 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6317 operation->can_output_key = 1;
6318 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006319
Ronald Cron5c522922020-11-14 16:35:34 +01006320 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006321
Ronald Cron5c522922020-11-14 16:35:34 +01006322 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006323}
6324
Gilles Peskinebe697d82019-05-16 18:00:41 +02006325psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006326 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006327 const uint8_t *peer_key,
6328 size_t peer_key_length,
6329 uint8_t *output,
6330 size_t output_size,
6331 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006332{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006334 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006335 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006336
6337 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6338 {
6339 status = PSA_ERROR_INVALID_ARGUMENT;
6340 goto exit;
6341 }
Ronald Cron5c522922020-11-14 16:35:34 +01006342 status = psa_get_and_lock_transparent_key_slot_with_policy(
6343 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006344 if( status != PSA_SUCCESS )
6345 goto exit;
6346
6347 status = psa_key_agreement_raw_internal( alg, slot,
6348 peer_key, peer_key_length,
6349 output, output_size,
6350 output_length );
6351
6352exit:
6353 if( status != PSA_SUCCESS )
6354 {
6355 /* If an error happens and is not handled properly, the output
6356 * may be used as a key to protect sensitive data. Arrange for such
6357 * a key to be random, which is likely to result in decryption or
6358 * verification errors. This is better than filling the buffer with
6359 * some constant data such as zeros, which would result in the data
6360 * being protected with a reproducible, easily knowable key.
6361 */
6362 psa_generate_random( output, output_size );
6363 *output_length = output_size;
6364 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006365
Ronald Cron5c522922020-11-14 16:35:34 +01006366 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006367
Ronald Cron5c522922020-11-14 16:35:34 +01006368 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006369}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006370
6371
Gilles Peskine30524eb2020-11-13 17:02:26 +01006372
Gilles Peskine86a440b2018-11-12 18:39:40 +01006373/****************************************************************/
6374/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006375/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006376
Gilles Peskine30524eb2020-11-13 17:02:26 +01006377/** Initialize the PSA random generator.
6378 */
6379static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6380{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006381#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6382 memset( rng, 0, sizeof( *rng ) );
6383#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6384
Gilles Peskine30524eb2020-11-13 17:02:26 +01006385 /* Set default configuration if
6386 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6387 if( rng->entropy_init == NULL )
6388 rng->entropy_init = mbedtls_entropy_init;
6389 if( rng->entropy_free == NULL )
6390 rng->entropy_free = mbedtls_entropy_free;
6391
6392 rng->entropy_init( &rng->entropy );
6393#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6394 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6395 /* The PSA entropy injection feature depends on using NV seed as an entropy
6396 * source. Add NV seed as an entropy source for PSA entropy injection. */
6397 mbedtls_entropy_add_source( &rng->entropy,
6398 mbedtls_nv_seed_poll, NULL,
6399 MBEDTLS_ENTROPY_BLOCK_SIZE,
6400 MBEDTLS_ENTROPY_SOURCE_STRONG );
6401#endif
6402
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006403 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006404#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006405}
6406
6407/** Deinitialize the PSA random generator.
6408 */
6409static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6410{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006411#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6412 memset( rng, 0, sizeof( *rng ) );
6413#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006414 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006415 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006416#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006417}
6418
6419/** Seed the PSA random generator.
6420 */
6421static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6422{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006423#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6424 /* Do nothing: the external RNG seeds itself. */
6425 (void) rng;
6426 return( PSA_SUCCESS );
6427#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006428 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006429 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6430 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006431 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006432#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006433}
6434
Gilles Peskinee59236f2018-01-27 23:32:46 +01006435psa_status_t psa_generate_random( uint8_t *output,
6436 size_t output_size )
6437{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006438 GUARD_MODULE_INITIALIZED;
6439
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006440#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6441
6442 size_t output_length = 0;
6443 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6444 output, output_size,
6445 &output_length );
6446 if( status != PSA_SUCCESS )
6447 return( status );
6448 /* Breaking up a request into smaller chunks is currently not supported
6449 * for the extrernal RNG interface. */
6450 if( output_length != output_size )
6451 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6452 return( PSA_SUCCESS );
6453
6454#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6455
Gilles Peskine71ddab92021-01-04 21:01:07 +01006456 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006457 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006458 size_t request_size =
6459 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6460 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6461 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006462 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6463 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006464 if( ret != 0 )
6465 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006466 output_size -= request_size;
6467 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006468 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006469 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006470#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006471}
6472
Gilles Peskine8814fc42020-12-14 15:33:44 +01006473/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006474 *
6475 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6476 * `psa_generate_random(...)`. The state parameter is ignored since the
6477 * PSA API doesn't support passing an explicit state.
6478 *
6479 * In the non-external case, psa_generate_random() calls an
6480 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6481 * and semantics as mbedtls_psa_get_random(). As an optimization,
6482 * instead of doing this back-and-forth between the PSA API and the
6483 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6484 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006485 */
6486#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6487int mbedtls_psa_get_random( void *p_rng,
6488 unsigned char *output,
6489 size_t output_size )
6490{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006491 /* This function takes a pointer to the RNG state because that's what
6492 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6493 * its own state internally and doesn't let the caller access that state.
6494 * So we just ignore the state parameter, and in practice we'll pass
6495 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006496 (void) p_rng;
6497 psa_status_t status = psa_generate_random( output, output_size );
6498 if( status == PSA_SUCCESS )
6499 return( 0 );
6500 else
6501 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6502}
6503#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6504
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006505#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6506#include "mbedtls/entropy_poll.h"
6507
Gilles Peskine7228da22019-07-15 11:06:15 +02006508psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006509 size_t seed_size )
6510{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006511 if( global_data.initialized )
6512 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006513
6514 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6515 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6516 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6517 return( PSA_ERROR_INVALID_ARGUMENT );
6518
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006519 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006520}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006521#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006522
John Durkop07cc04a2020-11-16 22:08:34 -08006523#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006524static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6525 size_t domain_parameters_size,
6526 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006527{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006528 size_t i;
6529 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006530
Gilles Peskinee56e8782019-04-26 17:34:02 +02006531 if( domain_parameters_size == 0 )
6532 {
6533 *exponent = 65537;
6534 return( PSA_SUCCESS );
6535 }
6536
6537 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6538 * support values that fit in a 32-bit integer, which is larger than
6539 * int on just about every platform anyway. */
6540 if( domain_parameters_size > sizeof( acc ) )
6541 return( PSA_ERROR_NOT_SUPPORTED );
6542 for( i = 0; i < domain_parameters_size; i++ )
6543 acc = ( acc << 8 ) | domain_parameters[i];
6544 if( acc > INT_MAX )
6545 return( PSA_ERROR_NOT_SUPPORTED );
6546 *exponent = acc;
6547 return( PSA_SUCCESS );
6548}
John Durkop07cc04a2020-11-16 22:08:34 -08006549#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006550
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006551static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006552 psa_key_slot_t *slot, size_t bits,
6553 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006554{
Gilles Peskine8e338702019-07-30 20:06:31 +02006555 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006556
Gilles Peskinee56e8782019-04-26 17:34:02 +02006557 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006558 return( PSA_ERROR_INVALID_ARGUMENT );
6559
Gilles Peskinee59236f2018-01-27 23:32:46 +01006560 if( key_type_is_raw_bytes( type ) )
6561 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006562 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006563
6564 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006565 if( status != PSA_SUCCESS )
6566 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006567
6568 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006569 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6570 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006571 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006572
Ronald Cronea0f8a62020-11-25 17:52:23 +01006573 status = psa_generate_random( slot->key.data,
6574 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006575 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006576 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006577
6578 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006579#if defined(MBEDTLS_DES_C)
6580 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006581 psa_des_set_key_parity( slot->key.data,
6582 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006583#endif /* MBEDTLS_DES_C */
6584 }
6585 else
6586
John Durkop07cc04a2020-11-16 22:08:34 -08006587#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006588 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006589 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006590 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006592 int exponent;
6593 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006594 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6595 return( PSA_ERROR_NOT_SUPPORTED );
6596 /* Accept only byte-aligned keys, for the same reasons as
6597 * in psa_import_rsa_key(). */
6598 if( bits % 8 != 0 )
6599 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006600 status = psa_read_rsa_exponent( domain_parameters,
6601 domain_parameters_size,
6602 &exponent );
6603 if( status != PSA_SUCCESS )
6604 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006605 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6606 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006607 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006608 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006609 (unsigned int) bits,
6610 exponent );
6611 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006612 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006613
6614 /* Make sure to always have an export representation available */
6615 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6616
Steven Cooreman75b74362020-07-28 14:30:13 +02006617 status = psa_allocate_buffer_to_slot( slot, bytes );
6618 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006619 {
6620 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006621 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006622 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006623
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01006624 status = mbedtls_psa_rsa_export_key( type,
6625 &rsa,
6626 slot->key.data,
6627 bytes,
6628 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006629 mbedtls_rsa_free( &rsa );
6630 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006631 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006632 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006633 }
6634 else
John Durkop07cc04a2020-11-16 22:08:34 -08006635#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006636
John Durkop9814fa22020-11-04 12:28:15 -08006637#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006638 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006639 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006640 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006641 mbedtls_ecp_group_id grp_id =
6642 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006643 const mbedtls_ecp_curve_info *curve_info =
6644 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006645 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006647 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006648 return( PSA_ERROR_NOT_SUPPORTED );
6649 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6650 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006651 mbedtls_ecp_keypair_init( &ecp );
6652 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006653 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006654 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006655 if( ret != 0 )
6656 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006657 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006658 return( mbedtls_to_psa_error( ret ) );
6659 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006660
6661
6662 /* Make sure to always have an export representation available */
6663 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006664 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6665 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006666 {
6667 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006668 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006669 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006670
6671 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006672 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006673
6674 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006675 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006676 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006677 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006678 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006679 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006680 }
6681 else
John Durkop9814fa22020-11-04 12:28:15 -08006682#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006683 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006684 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006685 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006686
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006687 return( PSA_SUCCESS );
6688}
Darryl Green0c6575a2018-11-07 16:05:30 +00006689
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006690psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006691 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006692{
6693 psa_status_t status;
6694 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006695 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006696
Ronald Cron81709fc2020-11-14 12:10:32 +01006697 *key = MBEDTLS_SVC_KEY_ID_INIT;
6698
Gilles Peskine0f84d622019-09-12 19:03:13 +02006699 /* Reject any attempt to create a zero-length key so that we don't
6700 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6701 if( psa_get_key_bits( attributes ) == 0 )
6702 return( PSA_ERROR_INVALID_ARGUMENT );
6703
Ronald Cron81709fc2020-11-14 12:10:32 +01006704 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6705 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006706 if( status != PSA_SUCCESS )
6707 goto exit;
6708
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006709 status = psa_driver_wrapper_generate_key( attributes,
6710 slot );
6711 if( status != PSA_ERROR_NOT_SUPPORTED ||
6712 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6713 goto exit;
6714
6715 status = psa_generate_key_internal(
6716 slot, attributes->core.bits,
6717 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006718
6719exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006720 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006721 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006722 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006723 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006724
Darryl Green0c6575a2018-11-07 16:05:30 +00006725 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006726}
6727
6728
Gilles Peskinee59236f2018-01-27 23:32:46 +01006729
6730/****************************************************************/
6731/* Module setup */
6732/****************************************************************/
6733
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006734#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006735psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6736 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6737 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6738{
6739 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6740 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006741 global_data.rng.entropy_init = entropy_init;
6742 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006743 return( PSA_SUCCESS );
6744}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006745#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006746
Gilles Peskinee59236f2018-01-27 23:32:46 +01006747void mbedtls_psa_crypto_free( void )
6748{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006749 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006750 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6751 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006752 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006753 }
6754 /* Wipe all remaining data, including configuration.
6755 * In particular, this sets all state indicator to the value
6756 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006757 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006758#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006759 /* Unregister all secure element drivers, so that we restart from
6760 * a pristine state. */
6761 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006762#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006763}
6764
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006765#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6766/** Recover a transaction that was interrupted by a power failure.
6767 *
6768 * This function is called during initialization, before psa_crypto_init()
6769 * returns. If this function returns a failure status, the initialization
6770 * fails.
6771 */
6772static psa_status_t psa_crypto_recover_transaction(
6773 const psa_crypto_transaction_t *transaction )
6774{
6775 switch( transaction->unknown.type )
6776 {
6777 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6778 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006779 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006780 * is implemented.
6781 * https://github.com/ARMmbed/mbed-crypto/issues/218
6782 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006783 default:
6784 /* We found an unsupported transaction in the storage.
6785 * We don't know what state the storage is in. Give up. */
6786 return( PSA_ERROR_STORAGE_FAILURE );
6787 }
6788}
6789#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6790
Gilles Peskinee59236f2018-01-27 23:32:46 +01006791psa_status_t psa_crypto_init( void )
6792{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006793 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006794
Gilles Peskinec6b69072018-11-20 21:42:52 +01006795 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006796 if( global_data.initialized != 0 )
6797 return( PSA_SUCCESS );
6798
Gilles Peskine30524eb2020-11-13 17:02:26 +01006799 /* Initialize and seed the random generator. */
6800 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006801 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006802 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006803 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006804 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006805 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006806
Gilles Peskine66fb1262018-12-10 16:29:04 +01006807 status = psa_initialize_key_slots( );
6808 if( status != PSA_SUCCESS )
6809 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006810
Gilles Peskined9348f22019-10-01 15:22:29 +02006811#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6812 status = psa_init_all_se_drivers( );
6813 if( status != PSA_SUCCESS )
6814 goto exit;
6815#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6816
Gilles Peskine4b734222019-07-24 15:56:31 +02006817#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006818 status = psa_crypto_load_transaction( );
6819 if( status == PSA_SUCCESS )
6820 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006821 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6822 if( status != PSA_SUCCESS )
6823 goto exit;
6824 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006825 }
6826 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6827 {
6828 /* There's no transaction to complete. It's all good. */
6829 status = PSA_SUCCESS;
6830 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006831#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006832
Gilles Peskinec6b69072018-11-20 21:42:52 +01006833 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006834 global_data.initialized = 1;
6835
6836exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006837 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006838 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006839 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006840}
6841
6842#endif /* MBEDTLS_PSA_CRYPTO_C */