blob: 79b0633fa1c2394e7759f0b8aa7b6d671c6a3fb8 [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 Peskinea5905292018-02-07 20:59:33 +0100138 /* If there's both a high-level code and low-level code, dispatch on
139 * the high-level code. */
140 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100141 {
142 case 0:
143 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100144
145 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
146 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
147 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
148 return( PSA_ERROR_NOT_SUPPORTED );
149 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
150 return( PSA_ERROR_HARDWARE_FAILURE );
151
152 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
153 return( PSA_ERROR_HARDWARE_FAILURE );
154
Gilles Peskine9a944802018-06-21 09:35:35 +0200155 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
156 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
157 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
158 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
159 case MBEDTLS_ERR_ASN1_INVALID_DATA:
160 return( PSA_ERROR_INVALID_ARGUMENT );
161 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
162 return( PSA_ERROR_INSUFFICIENT_MEMORY );
163 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
164 return( PSA_ERROR_BUFFER_TOO_SMALL );
165
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
169 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
170#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
172 return( PSA_ERROR_NOT_SUPPORTED );
173 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
174 return( PSA_ERROR_HARDWARE_FAILURE );
175
Jaeden Amero93e21112019-02-20 13:57:28 +0000176#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000177 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000178#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
179 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
180#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
182 return( PSA_ERROR_NOT_SUPPORTED );
183 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
186 case MBEDTLS_ERR_CCM_BAD_INPUT:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188 case MBEDTLS_ERR_CCM_AUTH_FAILED:
189 return( PSA_ERROR_INVALID_SIGNATURE );
190 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
191 return( PSA_ERROR_HARDWARE_FAILURE );
192
Gilles Peskine26869f22019-05-06 15:25:00 +0200193 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
194 return( PSA_ERROR_INVALID_ARGUMENT );
195
196 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
197 return( PSA_ERROR_BAD_STATE );
198 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
199 return( PSA_ERROR_INVALID_SIGNATURE );
200
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
202 return( PSA_ERROR_NOT_SUPPORTED );
203 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
204 return( PSA_ERROR_INVALID_ARGUMENT );
205 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
206 return( PSA_ERROR_INSUFFICIENT_MEMORY );
207 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
208 return( PSA_ERROR_INVALID_PADDING );
209 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200210 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
212 return( PSA_ERROR_INVALID_SIGNATURE );
213 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200214 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
216 return( PSA_ERROR_HARDWARE_FAILURE );
217
218 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
Gilles Peskine82e57d12020-11-13 21:31:17 +0100221#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
222 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100223 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
224 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
226 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
227 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
228 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
229 return( PSA_ERROR_NOT_SUPPORTED );
230 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
235 return( PSA_ERROR_NOT_SUPPORTED );
236 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
Gilles Peskinee59236f2018-01-27 23:32:46 +0100239 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
240 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
241 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
242 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100243
244 case MBEDTLS_ERR_GCM_AUTH_FAILED:
245 return( PSA_ERROR_INVALID_SIGNATURE );
246 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200247 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100248 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
249 return( PSA_ERROR_HARDWARE_FAILURE );
250
Gilles Peskine82e57d12020-11-13 21:31:17 +0100251#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
252 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100253 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
254 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100255 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
256 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
257 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
258 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
259 return( PSA_ERROR_NOT_SUPPORTED );
260 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
261 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
262#endif
263
Gilles Peskinea5905292018-02-07 20:59:33 +0100264 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
265 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
266 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
267 return( PSA_ERROR_HARDWARE_FAILURE );
268
269 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
270 return( PSA_ERROR_NOT_SUPPORTED );
271 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
272 return( PSA_ERROR_INVALID_ARGUMENT );
273 case MBEDTLS_ERR_MD_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
276 return( PSA_ERROR_STORAGE_FAILURE );
277 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
278 return( PSA_ERROR_HARDWARE_FAILURE );
279
Gilles Peskinef76aa772018-10-29 19:24:33 +0100280 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
281 return( PSA_ERROR_STORAGE_FAILURE );
282 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
287 return( PSA_ERROR_BUFFER_TOO_SMALL );
288 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
295 return( PSA_ERROR_INSUFFICIENT_MEMORY );
296
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100297 case MBEDTLS_ERR_PK_ALLOC_FAILED:
298 return( PSA_ERROR_INSUFFICIENT_MEMORY );
299 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
300 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
305 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
310 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
311 return( PSA_ERROR_NOT_PERMITTED );
312 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
313 return( PSA_ERROR_INVALID_ARGUMENT );
314 case MBEDTLS_ERR_PK_INVALID_ALG:
315 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
316 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
317 return( PSA_ERROR_NOT_SUPPORTED );
318 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
319 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322
Gilles Peskineff2d2002019-05-06 15:26:23 +0200323 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
326 return( PSA_ERROR_NOT_SUPPORTED );
327
Gilles Peskinea5905292018-02-07 20:59:33 +0100328 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
332 return( PSA_ERROR_INVALID_ARGUMENT );
333 case MBEDTLS_ERR_RSA_INVALID_PADDING:
334 return( PSA_ERROR_INVALID_PADDING );
335 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
338 return( PSA_ERROR_INVALID_ARGUMENT );
339 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
340 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200341 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100342 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
343 return( PSA_ERROR_INVALID_SIGNATURE );
344 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_RSA_RNG_FAILED:
347 return( PSA_ERROR_INSUFFICIENT_MEMORY );
348 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
349 return( PSA_ERROR_NOT_SUPPORTED );
350 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
351 return( PSA_ERROR_HARDWARE_FAILURE );
352
353 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
354 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
355 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
357
358 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
359 return( PSA_ERROR_INVALID_ARGUMENT );
360 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
361 return( PSA_ERROR_HARDWARE_FAILURE );
362
itayzafrir5c753392018-05-08 11:18:38 +0300363 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300364 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300365 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
367 return( PSA_ERROR_BUFFER_TOO_SMALL );
368 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
369 return( PSA_ERROR_NOT_SUPPORTED );
370 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
371 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
372 return( PSA_ERROR_INVALID_SIGNATURE );
373 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
374 return( PSA_ERROR_INSUFFICIENT_MEMORY );
375 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
376 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000377 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
378 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300379
Gilles Peskinee59236f2018-01-27 23:32:46 +0100380 default:
David Saadab4ecc272019-02-14 13:48:10 +0200381 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100382 }
383}
384
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200385
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200386
387
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388/****************************************************************/
389/* Key management */
390/****************************************************************/
391
Gilles Peskine73167e12019-07-12 23:44:37 +0200392#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
393static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
394{
Gilles Peskine8e338702019-07-30 20:06:31 +0200395 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200396}
397#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
398
John Durkop07cc04a2020-11-16 22:08:34 -0800399/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
400 * current test driver in key_management.c is using this function
401 * when accelerators are used for ECC key pair and public key.
402 * Once that dependency is resolved these guards can be removed.
403 */
John Durkop9814fa22020-11-04 12:28:15 -0800404#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800405 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
406 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
407 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100408mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100409 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410{
411 switch( curve )
412 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100413 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100414 switch( byte_length )
415 {
416 case PSA_BITS_TO_BYTES( 192 ):
417 return( MBEDTLS_ECP_DP_SECP192R1 );
418 case PSA_BITS_TO_BYTES( 224 ):
419 return( MBEDTLS_ECP_DP_SECP224R1 );
420 case PSA_BITS_TO_BYTES( 256 ):
421 return( MBEDTLS_ECP_DP_SECP256R1 );
422 case PSA_BITS_TO_BYTES( 384 ):
423 return( MBEDTLS_ECP_DP_SECP384R1 );
424 case PSA_BITS_TO_BYTES( 521 ):
425 return( MBEDTLS_ECP_DP_SECP521R1 );
426 default:
427 return( MBEDTLS_ECP_DP_NONE );
428 }
429 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100430
Paul Elliott8ff510a2020-06-02 17:19:28 +0100431 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 switch( byte_length )
433 {
434 case PSA_BITS_TO_BYTES( 256 ):
435 return( MBEDTLS_ECP_DP_BP256R1 );
436 case PSA_BITS_TO_BYTES( 384 ):
437 return( MBEDTLS_ECP_DP_BP384R1 );
438 case PSA_BITS_TO_BYTES( 512 ):
439 return( MBEDTLS_ECP_DP_BP512R1 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100444
Paul Elliott8ff510a2020-06-02 17:19:28 +0100445 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100446 switch( byte_length )
447 {
448 case PSA_BITS_TO_BYTES( 255 ):
449 return( MBEDTLS_ECP_DP_CURVE25519 );
450 case PSA_BITS_TO_BYTES( 448 ):
451 return( MBEDTLS_ECP_DP_CURVE448 );
452 default:
453 return( MBEDTLS_ECP_DP_NONE );
454 }
455 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100456
Paul Elliott8ff510a2020-06-02 17:19:28 +0100457 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 switch( byte_length )
459 {
460 case PSA_BITS_TO_BYTES( 192 ):
461 return( MBEDTLS_ECP_DP_SECP192K1 );
462 case PSA_BITS_TO_BYTES( 224 ):
463 return( MBEDTLS_ECP_DP_SECP224K1 );
464 case PSA_BITS_TO_BYTES( 256 ):
465 return( MBEDTLS_ECP_DP_SECP256K1 );
466 default:
467 return( MBEDTLS_ECP_DP_NONE );
468 }
469 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100470
Gilles Peskine12313cd2018-06-20 00:20:32 +0200471 default:
472 return( MBEDTLS_ECP_DP_NONE );
473 }
474}
John Durkop9814fa22020-11-04 12:28:15 -0800475#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800476 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
477 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
478 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200479
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200480static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
481 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482{
483 /* Check that the bit size is acceptable for the key type */
484 switch( type )
485 {
486 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200488 case PSA_KEY_TYPE_DERIVE:
489 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490#if defined(MBEDTLS_AES_C)
491 case PSA_KEY_TYPE_AES:
492 if( bits != 128 && bits != 192 && bits != 256 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
496#if defined(MBEDTLS_CAMELLIA_C)
497 case PSA_KEY_TYPE_CAMELLIA:
498 if( bits != 128 && bits != 192 && bits != 256 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
500 break;
501#endif
502#if defined(MBEDTLS_DES_C)
503 case PSA_KEY_TYPE_DES:
504 if( bits != 64 && bits != 128 && bits != 192 )
505 return( PSA_ERROR_INVALID_ARGUMENT );
506 break;
507#endif
508#if defined(MBEDTLS_ARC4_C)
509 case PSA_KEY_TYPE_ARC4:
510 if( bits < 8 || bits > 2048 )
511 return( PSA_ERROR_INVALID_ARGUMENT );
512 break;
513#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200514#if defined(MBEDTLS_CHACHA20_C)
515 case PSA_KEY_TYPE_CHACHA20:
516 if( bits != 256 )
517 return( PSA_ERROR_INVALID_ARGUMENT );
518 break;
519#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520 default:
521 return( PSA_ERROR_NOT_SUPPORTED );
522 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200523 if( bits % 8 != 0 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200525
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526 return( PSA_SUCCESS );
527}
528
John Durkop0e005192020-10-31 22:06:54 -0700529#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
530 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
531 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800532 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
533 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
534 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200535
Gilles Peskine86a440b2018-11-12 18:39:40 +0100536/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
537 * that are not a multiple of 8) well. For example, there is only
538 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
539 * way to return the exact bit size of a key.
540 * To keep things simple, reject non-byte-aligned key sizes. */
541static psa_status_t psa_check_rsa_key_byte_aligned(
542 const mbedtls_rsa_context *rsa )
543{
544 mbedtls_mpi n;
545 psa_status_t status;
546 mbedtls_mpi_init( &n );
547 status = mbedtls_to_psa_error(
548 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
549 if( status == PSA_SUCCESS )
550 {
551 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
552 status = PSA_ERROR_NOT_SUPPORTED;
553 }
554 mbedtls_mpi_free( &n );
555 return( status );
556}
557
Steven Cooreman7f391872020-07-30 14:57:44 +0200558/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200559 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200560 * \param[in] type The type of key contained in \p data.
561 * \param[in] data The buffer from which to load the representation.
562 * \param[in] data_length The size in bytes of \p data.
563 * \param[out] p_rsa Returns a pointer to an RSA context on success.
564 * The caller is responsible for freeing both the
565 * contents of the context and the context itself
566 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200567 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200568static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
569 const uint8_t *data,
570 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200571 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200572{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000573 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200574 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000575 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200576 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000577
578 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200579 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000580 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200581 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200582 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000583 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200584 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000585 if( status != PSA_SUCCESS )
586 goto exit;
587
588 /* We have something that the pkparse module recognizes. If it is a
589 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200590 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200591 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000592 status = PSA_ERROR_INVALID_ARGUMENT;
593 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200594 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000595
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000596 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
597 * supports non-byte-aligned key sizes, but not well. For example,
598 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200599 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000600 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
601 {
602 status = PSA_ERROR_NOT_SUPPORTED;
603 goto exit;
604 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200605 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200606 if( status != PSA_SUCCESS )
607 goto exit;
608
Steven Cooremana2371e52020-07-28 14:30:39 +0200609 /* Copy out the pointer to the RSA context, and reset the PK context
610 * such that pk_free doesn't free the RSA context we just grabbed. */
611 *p_rsa = mbedtls_pk_rsa( ctx );
612 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000613
614exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200615 mbedtls_pk_free( &ctx );
616 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200617}
618
John Durkop6ba40d12020-11-10 08:50:04 -0800619#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
620 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
621 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
622 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
623 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
624 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
625
626#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
627 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
628
Steven Cooremana01795d2020-07-24 22:48:15 +0200629/** Export an RSA key to export representation
630 *
631 * \param[in] type The type of key (public/private) to export
632 * \param[in] rsa The internal RSA representation from which to export
633 * \param[out] data The buffer to export to
634 * \param[in] data_size The length of the buffer to export to
635 * \param[out] data_length The amount of bytes written to \p data
636 */
637static psa_status_t psa_export_rsa_key( psa_key_type_t type,
638 mbedtls_rsa_context *rsa,
639 uint8_t *data,
640 size_t data_size,
641 size_t *data_length )
642{
643#if defined(MBEDTLS_PK_WRITE_C)
644 int ret;
645 mbedtls_pk_context pk;
646 uint8_t *pos = data + data_size;
647
648 mbedtls_pk_init( &pk );
649 pk.pk_info = &mbedtls_rsa_info;
650 pk.pk_ctx = rsa;
651
652 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200653 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
654 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200655 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
656 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
657 else
658 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
659
660 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200661 {
662 /* Clean up in case pk_write failed halfway through. */
663 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200664 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200665 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200666
667 /* The mbedtls_pk_xxx functions write to the end of the buffer.
668 * Move the data to the beginning and erase remaining data
669 * at the original location. */
670 if( 2 * (size_t) ret <= data_size )
671 {
672 memcpy( data, data + data_size - ret, ret );
673 memset( data + data_size - ret, 0, ret );
674 }
675 else if( (size_t) ret < data_size )
676 {
677 memmove( data, data + data_size - ret, ret );
678 memset( data + ret, 0, data_size - ret );
679 }
680
681 *data_length = ret;
682 return( PSA_SUCCESS );
683#else
684 (void) type;
685 (void) rsa;
686 (void) data;
687 (void) data_size;
688 (void) data_length;
689 return( PSA_ERROR_NOT_SUPPORTED );
690#endif /* MBEDTLS_PK_WRITE_C */
691}
692
693/** Import an RSA key from import representation to a slot
694 *
695 * \param[in,out] slot The slot where to store the export representation to
696 * \param[in] data The buffer containing the import representation
697 * \param[in] data_length The amount of bytes in \p data
698 */
699static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
700 const uint8_t *data,
701 size_t data_length )
702{
703 psa_status_t status;
704 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200705 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200706
Steven Cooremana01795d2020-07-24 22:48:15 +0200707 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200708 status = psa_load_rsa_representation( slot->attr.type,
709 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200710 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200711 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200712 if( status != PSA_SUCCESS )
713 goto exit;
714
715 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200716 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200717
Steven Cooreman75b74362020-07-28 14:30:13 +0200718 /* Re-export the data to PSA export format, such that we can store export
719 * representation in the key slot. Export representation in case of RSA is
720 * the smallest representation that's allowed as input, so a straight-up
721 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200722 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200723 if( output == NULL )
724 {
725 status = PSA_ERROR_INSUFFICIENT_MEMORY;
726 goto exit;
727 }
728
Steven Cooremana01795d2020-07-24 22:48:15 +0200729 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200730 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200731 output,
732 data_length,
733 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200734exit:
735 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200736 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200737 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200738
739 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000740 if( status != PSA_SUCCESS )
741 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200742 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000743 return( status );
744 }
745
Steven Cooremana01795d2020-07-24 22:48:15 +0200746 /* On success, store the allocated export-formatted key. */
747 slot->data.key.data = output;
748 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000749
750 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200751}
John Durkop6ba40d12020-11-10 08:50:04 -0800752
753#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800754 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200755
John Durkop9814fa22020-11-04 12:28:15 -0800756#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800757 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
758 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
759 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
760 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200761/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200762 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200763 * \param[in] type The type of key contained in \p data.
764 * \param[in] data The buffer from which to load the representation.
765 * \param[in] data_length The size in bytes of \p data.
766 * \param[out] p_ecp Returns a pointer to an ECP context on success.
767 * The caller is responsible for freeing both the
768 * contents of the context and the context itself
769 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200770 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200771static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
772 const uint8_t *data,
773 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200774 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100775{
776 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200778 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200779 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100780
Steven Cooreman4fed4552020-08-03 14:46:03 +0200781 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
782 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100783 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200784 /* A Weierstrass public key is represented as:
785 * - The byte 0x04;
786 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
787 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200788 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200789 */
790 if( ( data_length & 1 ) == 0 )
791 return( PSA_ERROR_INVALID_ARGUMENT );
792 curve_size = data_length / 2;
793
794 /* Montgomery public keys are represented in compressed format, meaning
795 * their curve_size is equal to the amount of input. */
796
797 /* Private keys are represented in uncompressed private random integer
798 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100799 }
800
Steven Cooreman6d839f02020-07-30 11:36:45 +0200801 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200802 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200803 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200804 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200805 mbedtls_ecp_keypair_init( ecp );
806
Gilles Peskine4cd32772019-12-02 20:49:42 +0100807 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200808 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
809 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100810 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200811 {
812 status = PSA_ERROR_INVALID_ARGUMENT;
813 goto exit;
814 }
815
Steven Cooremanacda8342020-07-24 23:09:52 +0200816 status = mbedtls_to_psa_error(
817 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
818 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200819 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200820
Steven Cooreman6d839f02020-07-30 11:36:45 +0200821 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200822 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200823 {
824 /* Load the public value. */
825 status = mbedtls_to_psa_error(
826 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200827 data,
828 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 if( status != PSA_SUCCESS )
830 goto exit;
831
832 /* Check that the point is on the curve. */
833 status = mbedtls_to_psa_error(
834 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
835 if( status != PSA_SUCCESS )
836 goto exit;
837 }
838 else
839 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200840 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200841 status = mbedtls_to_psa_error(
842 mbedtls_ecp_read_key( ecp->grp.id,
843 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844 data,
845 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200846 if( status != PSA_SUCCESS )
847 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200848 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200849
850 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200851exit:
852 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200853 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200854 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200855 mbedtls_free( ecp );
856 }
857
Steven Cooreman29149862020-08-05 15:43:42 +0200858 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100859}
John Durkop6ba40d12020-11-10 08:50:04 -0800860#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
861 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
862 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
863 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
864 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000865
John Durkop6ba40d12020-11-10 08:50:04 -0800866#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
867 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200868/** Export an ECP key to export representation
869 *
870 * \param[in] type The type of key (public/private) to export
871 * \param[in] ecp The internal ECP representation from which to export
872 * \param[out] data The buffer to export to
873 * \param[in] data_size The length of the buffer to export to
874 * \param[out] data_length The amount of bytes written to \p data
875 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200876static psa_status_t psa_export_ecp_key( psa_key_type_t type,
877 mbedtls_ecp_keypair *ecp,
878 uint8_t *data,
879 size_t data_size,
880 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200881{
Steven Cooremanacda8342020-07-24 23:09:52 +0200882 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000883
Steven Cooremanacda8342020-07-24 23:09:52 +0200884 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200885 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200886 /* Check whether the public part is loaded */
887 if( mbedtls_ecp_is_zero( &ecp->Q ) )
888 {
889 /* Calculate the public key */
890 status = mbedtls_to_psa_error(
891 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100892 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200893 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200894 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 }
896
Steven Cooreman4fed4552020-08-03 14:46:03 +0200897 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200898 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
899 MBEDTLS_ECP_PF_UNCOMPRESSED,
900 data_length,
901 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200902 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200903 if( status != PSA_SUCCESS )
904 memset( data, 0, data_size );
905
Steven Cooreman29149862020-08-05 15:43:42 +0200906 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200907 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200908 else
909 {
Steven Cooreman29149862020-08-05 15:43:42 +0200910 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200911 return( PSA_ERROR_BUFFER_TOO_SMALL );
912
913 status = mbedtls_to_psa_error(
914 mbedtls_ecp_write_key( ecp,
915 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200916 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200917 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200918 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200919 else
920 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200921
922 return( status );
923 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200924}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200925
Steven Cooreman4fed4552020-08-03 14:46:03 +0200926/** Import an ECP key from import representation to a slot
927 *
928 * \param[in,out] slot The slot where to store the export representation to
929 * \param[in] data The buffer containing the import representation
930 * \param[in] data_length The amount of bytes in \p data
931 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200932static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
933 const uint8_t *data,
934 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100935{
Steven Cooremanacda8342020-07-24 23:09:52 +0200936 psa_status_t status;
937 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200938 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100939
Steven Cooremanacda8342020-07-24 23:09:52 +0200940 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200941 status = psa_load_ecp_representation( slot->attr.type,
942 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200943 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200944 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100945 if( status != PSA_SUCCESS )
946 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100947
Steven Cooremanacda8342020-07-24 23:09:52 +0200948 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200949 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200950 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200951 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200952
Steven Cooremanacda8342020-07-24 23:09:52 +0200953 /* Re-export the data to PSA export format. There is currently no support
954 * for other input formats then the export format, so this is a 1-1
955 * copy operation. */
956 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200957 if( output == NULL )
958 {
959 status = PSA_ERROR_INSUFFICIENT_MEMORY;
960 goto exit;
961 }
962
963 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200964 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200965 output,
966 data_length,
967 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100968exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200969 /* Always free the PK object (will also free contained ECP context) */
970 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200971 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200972
973 /* Free the allocated buffer only on error. */
974 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100975 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200976 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200977 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100978 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200979
980 /* On success, store the allocated export-formatted key. */
981 slot->data.key.data = output;
982 slot->data.key.bytes = data_length;
983
984 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100985}
John Durkop9814fa22020-11-04 12:28:15 -0800986#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
987 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100988
Gilles Peskineb46bef22019-07-30 21:32:04 +0200989/** Return the size of the key in the given slot, in bits.
990 *
991 * \param[in] slot A key slot.
992 *
993 * \return The key size in bits, read from the metadata in the slot.
994 */
995static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
996{
997 return( slot->attr.bits );
998}
999
Steven Cooreman75b74362020-07-28 14:30:13 +02001000/** Try to allocate a buffer to an empty key slot.
1001 *
1002 * \param[in,out] slot Key slot to attach buffer to.
1003 * \param[in] buffer_length Requested size of the buffer.
1004 *
1005 * \retval #PSA_SUCCESS
1006 * The buffer has been successfully allocated.
1007 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1008 * Not enough memory was available for allocation.
1009 * \retval #PSA_ERROR_ALREADY_EXISTS
1010 * Trying to allocate a buffer to a non-empty key slot.
1011 */
1012static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
1013 size_t buffer_length )
1014{
1015 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001016 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001017
1018 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
1019 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001020 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001021
1022 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001023 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001024}
1025
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001026psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1027 const uint8_t* data,
1028 size_t data_length )
1029{
1030 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1031 data_length );
1032 if( status != PSA_SUCCESS )
1033 return( status );
1034
1035 memcpy( slot->data.key.data, data, data_length );
1036 return( PSA_SUCCESS );
1037}
1038
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001039/** Import key data into a slot.
1040 *
1041 * `slot->type` must have been set previously.
1042 * This function assumes that the slot does not contain any key material yet.
1043 * On failure, the slot content is unchanged.
1044 *
1045 * Persistent storage is not affected.
1046 *
1047 * \param[in,out] slot The key slot to import data into.
1048 * Its `type` field must have previously been set to
1049 * the desired key type.
1050 * It must not contain any key material yet.
1051 * \param[in] data Buffer containing the key material to parse and import.
1052 * \param data_length Size of \p data in bytes.
1053 *
1054 * \retval #PSA_SUCCESS
1055 * \retval #PSA_ERROR_INVALID_ARGUMENT
1056 * \retval #PSA_ERROR_NOT_SUPPORTED
1057 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1058 */
1059static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1060 const uint8_t *data,
1061 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001063 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001064 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001065
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001066 /* zero-length keys are never supported. */
1067 if( data_length == 0 )
1068 return( PSA_ERROR_NOT_SUPPORTED );
1069
Gilles Peskine8e338702019-07-30 20:06:31 +02001070 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001071 {
Steven Cooreman04524762020-10-13 17:43:44 +02001072 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001073
Steven Cooreman75b74362020-07-28 14:30:13 +02001074 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1075 if( data_length > SIZE_MAX / 8 )
1076 return( PSA_ERROR_NOT_SUPPORTED );
1077
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001078 /* Enforce a size limit, and in particular ensure that the bit
1079 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001080 if( bit_size > PSA_MAX_KEY_BITS )
1081 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001082
1083 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001084 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001085 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001086
1087 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001088 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001089 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001090 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001091
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001092 /* Write the actual key size to the slot.
1093 * psa_start_key_creation() wrote the size declared by the
1094 * caller, which may be 0 (meaning unspecified) or wrong. */
1095 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001096
1097 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001099 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001100 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001101 /* Try validation through accelerators first. */
1102 bit_size = slot->attr.bits;
1103 psa_key_attributes_t attributes = {
1104 .core = slot->attr
1105 };
1106 status = psa_driver_wrapper_validate_key( &attributes,
1107 data,
1108 data_length,
1109 &bit_size );
1110 if( status == PSA_SUCCESS )
1111 {
1112 /* Key has been validated successfully by an accelerator.
1113 * Copy key material into slot. */
1114 status = psa_copy_key_material_into_slot( slot, data, data_length );
1115 if( status != PSA_SUCCESS )
1116 return( status );
1117
1118 slot->attr.bits = (psa_key_bits_t) bit_size;
1119 return( PSA_SUCCESS );
1120 }
1121 else if( status != PSA_ERROR_NOT_SUPPORTED )
1122 return( status );
1123
1124 /* Key format is not supported by any accelerator, try software fallback
1125 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001126#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1127 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001128 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1129 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001130 return( psa_import_ecp_key( slot, data, data_length ) );
1131 }
John Durkop9814fa22020-11-04 12:28:15 -08001132#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1133 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001134#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1135 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001136 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001137 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001138 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001139 }
John Durkop9814fa22020-11-04 12:28:15 -08001140#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1141 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001142
1143 /* Fell through the fallback as well, so have nothing else to try. */
1144 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001145 }
1146 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001147 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001148 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001149 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001150 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001151}
1152
Gilles Peskinef603c712019-01-19 13:40:11 +01001153/** Calculate the intersection of two algorithm usage policies.
1154 *
1155 * Return 0 (which allows no operation) on incompatibility.
1156 */
1157static psa_algorithm_t psa_key_policy_algorithm_intersection(
1158 psa_algorithm_t alg1,
1159 psa_algorithm_t alg2 )
1160{
Gilles Peskine549ea862019-05-22 11:45:59 +02001161 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001162 if( alg1 == alg2 )
1163 return( alg1 );
1164 /* If the policies are from the same hash-and-sign family, check
1165 * if one is a wildcard. If so the other has the specific algorithm. */
1166 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1167 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1168 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1169 {
1170 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1171 return( alg2 );
1172 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1173 return( alg1 );
1174 }
1175 /* If the policies are incompatible, allow nothing. */
1176 return( 0 );
1177}
1178
Gilles Peskined6f371b2019-05-10 19:33:38 +02001179static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1180 psa_algorithm_t requested_alg )
1181{
Gilles Peskine549ea862019-05-22 11:45:59 +02001182 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001183 if( requested_alg == policy_alg )
1184 return( 1 );
1185 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001186 * and requested_alg is the same hash-and-sign family with any hash,
1187 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001188 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1189 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1190 {
1191 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1192 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1193 }
Steven Cooremance48e852020-10-05 16:02:45 +02001194 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001195 * a key derivation with that key agreement should also be allowed. This
1196 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001197 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1198 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1199 {
1200 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1201 policy_alg );
1202 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001203 /* If it isn't permitted, it's forbidden. */
1204 return( 0 );
1205}
1206
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001207/** Test whether a policy permits an algorithm.
1208 *
1209 * The caller must test usage flags separately.
1210 */
1211static int psa_key_policy_permits( const psa_key_policy_t *policy,
1212 psa_algorithm_t alg )
1213{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001214 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1215 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001216}
1217
Gilles Peskinef603c712019-01-19 13:40:11 +01001218/** Restrict a key policy based on a constraint.
1219 *
1220 * \param[in,out] policy The policy to restrict.
1221 * \param[in] constraint The policy constraint to apply.
1222 *
1223 * \retval #PSA_SUCCESS
1224 * \c *policy contains the intersection of the original value of
1225 * \c *policy and \c *constraint.
1226 * \retval #PSA_ERROR_INVALID_ARGUMENT
1227 * \c *policy and \c *constraint are incompatible.
1228 * \c *policy is unchanged.
1229 */
1230static psa_status_t psa_restrict_key_policy(
1231 psa_key_policy_t *policy,
1232 const psa_key_policy_t *constraint )
1233{
1234 psa_algorithm_t intersection_alg =
1235 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001236 psa_algorithm_t intersection_alg2 =
1237 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001238 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1239 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001240 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1241 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001242 policy->usage &= constraint->usage;
1243 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001244 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001245 return( PSA_SUCCESS );
1246}
1247
Ronald Cron5c522922020-11-14 16:35:34 +01001248/** Get the description of a key given its identifier and policy constraints
1249 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001250 *
Ronald Cron5c522922020-11-14 16:35:34 +01001251 * The key must have allow all the usage flags set in \p usage. If \p alg is
1252 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001253 *
Ronald Cron5c522922020-11-14 16:35:34 +01001254 * In case of a persistent key, the function loads the description of the key
1255 * into a key slot if not already done.
1256 *
1257 * On success, the returned key slot is locked. It is the responsibility of
1258 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001259 */
Ronald Cron5c522922020-11-14 16:35:34 +01001260static psa_status_t psa_get_and_lock_key_slot_with_policy(
1261 mbedtls_svc_key_id_t key,
1262 psa_key_slot_t **p_slot,
1263 psa_key_usage_t usage,
1264 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001265{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1267 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001268
Ronald Cron5c522922020-11-14 16:35:34 +01001269 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001270 if( status != PSA_SUCCESS )
1271 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001272 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001273
1274 /* Enforce that usage policy for the key slot contains all the flags
1275 * required by the usage parameter. There is one exception: public
1276 * keys can always be exported, so we treat public key objects as
1277 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001278 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001279 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001280
1281 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001282 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001283 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001284
1285 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001286 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001287 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001288
Darryl Green06fd18d2018-07-16 11:21:11 +01001289 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001290
1291error:
1292 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001293 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001294
1295 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001296}
Darryl Green940d72c2018-07-13 13:18:51 +01001297
Ronald Cron5c522922020-11-14 16:35:34 +01001298/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001299 *
1300 * A transparent key is a key for which the key material is directly
1301 * available, as opposed to a key in a secure element.
1302 *
Ronald Cron5c522922020-11-14 16:35:34 +01001303 * This is a temporary function to use instead of
1304 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1305 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001306 *
Ronald Cron5c522922020-11-14 16:35:34 +01001307 * On success, the returned key slot is locked. It is the responsibility of the
1308 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001309 */
1310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001311static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1312 mbedtls_svc_key_id_t key,
1313 psa_key_slot_t **p_slot,
1314 psa_key_usage_t usage,
1315 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001316{
Ronald Cron5c522922020-11-14 16:35:34 +01001317 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1318 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001319 if( status != PSA_SUCCESS )
1320 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001321
Gilles Peskineadad8132019-07-25 11:31:23 +02001322 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001323 {
Ronald Cron5c522922020-11-14 16:35:34 +01001324 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001325 *p_slot = NULL;
1326 return( PSA_ERROR_NOT_SUPPORTED );
1327 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001328
Gilles Peskine28f8f302019-07-24 13:30:31 +02001329 return( PSA_SUCCESS );
1330}
1331#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1332/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001333#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1334 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001335#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1336
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001337/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001338static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001339{
Gilles Peskine73167e12019-07-12 23:44:37 +02001340#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1341 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001342 {
1343 /* No key material to clean. */
1344 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001345 else
1346#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001347 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001348 /* Data pointer will always be either a valid pointer or NULL in an
1349 * initialized slot, so we can just free it. */
1350 if( slot->data.key.data != NULL )
1351 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001352 mbedtls_free( slot->data.key.data );
1353 slot->data.key.data = NULL;
1354 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001355 }
Darryl Green40225ba2018-11-15 14:48:15 +00001356
1357 return( PSA_SUCCESS );
1358}
1359
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001360/** Completely wipe a slot in memory, including its policy.
1361 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001362psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001363{
1364 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001365
1366 /*
1367 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001368 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001369 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1370 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001371 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001372 */
Ronald Cron5c522922020-11-14 16:35:34 +01001373 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001374 {
1375#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001376 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001377#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001378 status = PSA_ERROR_CORRUPTION_DETECTED;
1379 }
1380
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001381 /* Multipart operations may still be using the key. This is safe
1382 * because all multipart operation objects are independent from
1383 * the key slot: if they need to access the key after the setup
1384 * phase, they have a copy of the key. Note that this means that
1385 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001386 /* At this point, key material and other type-specific content has
1387 * been wiped. Clear remaining metadata. We can call memset and not
1388 * zeroize because the metadata is not particularly sensitive. */
1389 memset( slot, 0, sizeof( *slot ) );
1390 return( status );
1391}
1392
Ronald Croncf56a0a2020-08-04 09:51:30 +02001393psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001394{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001395 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001396 psa_status_t status; /* status of the last operation */
1397 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001398#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1399 psa_se_drv_table_entry_t *driver;
1400#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001401
Ronald Croncf56a0a2020-08-04 09:51:30 +02001402 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001403 return( PSA_SUCCESS );
1404
Ronald Cronf2911112020-10-29 17:51:10 +01001405 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001406 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001407 * key, this will load the key description from persistent memory if not
1408 * done yet. We cannot avoid this loading as without it we don't know if
1409 * the key is operated by an SE or not and this information is needed by
1410 * the current implementation.
1411 */
Ronald Cron5c522922020-11-14 16:35:34 +01001412 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001413 if( status != PSA_SUCCESS )
1414 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001415
Ronald Cronf2911112020-10-29 17:51:10 +01001416 /*
1417 * If the key slot containing the key description is under access by the
1418 * library (apart from the present access), the key cannot be destroyed
1419 * yet. For the time being, just return in error. Eventually (to be
1420 * implemented), the key should be destroyed when all accesses have
1421 * stopped.
1422 */
Ronald Cron5c522922020-11-14 16:35:34 +01001423 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001424 {
Ronald Cron5c522922020-11-14 16:35:34 +01001425 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001426 return( PSA_ERROR_GENERIC_ERROR );
1427 }
1428
Gilles Peskine354f7672019-07-12 23:46:38 +02001429#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001430 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001431 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001432 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001433 /* For a key in a secure element, we need to do three things:
1434 * remove the key file in internal storage, destroy the
1435 * key inside the secure element, and update the driver's
1436 * persistent data. Start a transaction that will encompass these
1437 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001438 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001439 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001440 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001441 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001442 status = psa_crypto_save_transaction( );
1443 if( status != PSA_SUCCESS )
1444 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001445 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001446 /* We should still try to destroy the key in the secure
1447 * element and the key metadata in storage. This is especially
1448 * important if the error is that the storage is full.
1449 * But how to do it exactly without risking an inconsistent
1450 * state after a reset?
1451 * https://github.com/ARMmbed/mbed-crypto/issues/215
1452 */
1453 overall_status = status;
1454 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001455 }
1456
Gilles Peskine354f7672019-07-12 23:46:38 +02001457 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001458 if( overall_status == PSA_SUCCESS )
1459 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001460 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001461#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1462
Darryl Greend49a4992018-06-18 17:27:26 +01001463#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001464 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001465 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001466 status = psa_destroy_persistent_key( slot->attr.id );
1467 if( overall_status == PSA_SUCCESS )
1468 overall_status = status;
1469
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001470 /* TODO: other slots may have a copy of the same key. We should
1471 * invalidate them.
1472 * https://github.com/ARMmbed/mbed-crypto/issues/214
1473 */
Darryl Greend49a4992018-06-18 17:27:26 +01001474 }
1475#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001476
Gilles Peskinefc762652019-07-22 19:30:34 +02001477#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1478 if( driver != NULL )
1479 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001480 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001481 if( overall_status == PSA_SUCCESS )
1482 overall_status = status;
1483 status = psa_crypto_stop_transaction( );
1484 if( overall_status == PSA_SUCCESS )
1485 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001486 }
1487#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1488
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001489#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1490exit:
1491#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001492 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001493 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1494 if( overall_status == PSA_SUCCESS )
1495 overall_status = status;
1496 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001497}
1498
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001499void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001500{
Gilles Peskineb699f072019-04-26 16:06:02 +02001501 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001502 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001503}
1504
Gilles Peskineb699f072019-04-26 16:06:02 +02001505psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1506 psa_key_type_t type,
1507 const uint8_t *data,
1508 size_t data_length )
1509{
1510 uint8_t *copy = NULL;
1511
1512 if( data_length != 0 )
1513 {
1514 copy = mbedtls_calloc( 1, data_length );
1515 if( copy == NULL )
1516 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1517 memcpy( copy, data, data_length );
1518 }
1519 /* After this point, this function is guaranteed to succeed, so it
1520 * can start modifying `*attributes`. */
1521
1522 if( attributes->domain_parameters != NULL )
1523 {
1524 mbedtls_free( attributes->domain_parameters );
1525 attributes->domain_parameters = NULL;
1526 attributes->domain_parameters_size = 0;
1527 }
1528
1529 attributes->domain_parameters = copy;
1530 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001531 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001532 return( PSA_SUCCESS );
1533}
1534
1535psa_status_t psa_get_key_domain_parameters(
1536 const psa_key_attributes_t *attributes,
1537 uint8_t *data, size_t data_size, size_t *data_length )
1538{
1539 if( attributes->domain_parameters_size > data_size )
1540 return( PSA_ERROR_BUFFER_TOO_SMALL );
1541 *data_length = attributes->domain_parameters_size;
1542 if( attributes->domain_parameters_size != 0 )
1543 memcpy( data, attributes->domain_parameters,
1544 attributes->domain_parameters_size );
1545 return( PSA_SUCCESS );
1546}
1547
John Durkop07cc04a2020-11-16 22:08:34 -08001548#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001549 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001550static psa_status_t psa_get_rsa_public_exponent(
1551 const mbedtls_rsa_context *rsa,
1552 psa_key_attributes_t *attributes )
1553{
1554 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001555 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001556 uint8_t *buffer = NULL;
1557 size_t buflen;
1558 mbedtls_mpi_init( &mpi );
1559
1560 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1561 if( ret != 0 )
1562 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001563 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1564 {
1565 /* It's the default value, which is reported as an empty string,
1566 * so there's nothing to do. */
1567 goto exit;
1568 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001569
1570 buflen = mbedtls_mpi_size( &mpi );
1571 buffer = mbedtls_calloc( 1, buflen );
1572 if( buffer == NULL )
1573 {
1574 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1575 goto exit;
1576 }
1577 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1578 if( ret != 0 )
1579 goto exit;
1580 attributes->domain_parameters = buffer;
1581 attributes->domain_parameters_size = buflen;
1582
1583exit:
1584 mbedtls_mpi_free( &mpi );
1585 if( ret != 0 )
1586 mbedtls_free( buffer );
1587 return( mbedtls_to_psa_error( ret ) );
1588}
John Durkop07cc04a2020-11-16 22:08:34 -08001589#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001590 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001591
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001592/** Retrieve all the publicly-accessible attributes of a key.
1593 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001594psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001595 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001596{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001597 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001598 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001599 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001600
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001601 psa_reset_key_attributes( attributes );
1602
Ronald Cron5c522922020-11-14 16:35:34 +01001603 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001604 if( status != PSA_SUCCESS )
1605 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001606
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001607 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001608 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1609 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1610
1611#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1612 if( psa_key_slot_is_external( slot ) )
1613 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1614#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001615
Gilles Peskine8e338702019-07-30 20:06:31 +02001616 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001617 {
John Durkop07cc04a2020-11-16 22:08:34 -08001618#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001619 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001620 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001621 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001622#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001623 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001624 * is not yet implemented.
1625 * https://github.com/ARMmbed/mbed-crypto/issues/216
1626 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001627 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001628 break;
1629#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001630 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001631 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001632
Steven Cooreman4fed4552020-08-03 14:46:03 +02001633 status = psa_load_rsa_representation( slot->attr.type,
1634 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001635 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001636 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001637 if( status != PSA_SUCCESS )
1638 break;
1639
Steven Cooremana2371e52020-07-28 14:30:39 +02001640 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001641 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001642 mbedtls_rsa_free( rsa );
1643 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001644 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001645 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001646#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001647 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001648 default:
1649 /* Nothing else to do. */
1650 break;
1651 }
1652
1653 if( status != PSA_SUCCESS )
1654 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001655
Ronald Cron5c522922020-11-14 16:35:34 +01001656 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001657
Ronald Cron5c522922020-11-14 16:35:34 +01001658 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001659}
1660
Gilles Peskinec8000c02019-08-02 20:15:51 +02001661#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1662psa_status_t psa_get_key_slot_number(
1663 const psa_key_attributes_t *attributes,
1664 psa_key_slot_number_t *slot_number )
1665{
1666 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1667 {
1668 *slot_number = attributes->slot_number;
1669 return( PSA_SUCCESS );
1670 }
1671 else
1672 return( PSA_ERROR_INVALID_ARGUMENT );
1673}
1674#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1675
Steven Cooremana01795d2020-07-24 22:48:15 +02001676static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1677 uint8_t *data,
1678 size_t data_size,
1679 size_t *data_length )
1680{
1681 if( slot->data.key.bytes > data_size )
1682 return( PSA_ERROR_BUFFER_TOO_SMALL );
1683 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1684 memset( data + slot->data.key.bytes, 0,
1685 data_size - slot->data.key.bytes );
1686 *data_length = slot->data.key.bytes;
1687 return( PSA_SUCCESS );
1688}
1689
Gilles Peskinef603c712019-01-19 13:40:11 +01001690static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1691 uint8_t *data,
1692 size_t data_size,
1693 size_t *data_length,
1694 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001695{
Gilles Peskine5d309672019-07-12 23:47:28 +02001696#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1697 const psa_drv_se_t *drv;
1698 psa_drv_se_context_t *drv_context;
1699#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1700
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001701 *data_length = 0;
1702
Gilles Peskine8e338702019-07-30 20:06:31 +02001703 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001704 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001705
Gilles Peskinef9168942019-09-12 19:20:29 +02001706 /* Reject a zero-length output buffer now, since this can never be a
1707 * valid key representation. This way we know that data must be a valid
1708 * pointer and we can do things like memset(data, ..., data_size). */
1709 if( data_size == 0 )
1710 return( PSA_ERROR_BUFFER_TOO_SMALL );
1711
Gilles Peskine5d309672019-07-12 23:47:28 +02001712#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001713 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001714 {
1715 psa_drv_se_export_key_t method;
1716 if( drv->key_management == NULL )
1717 return( PSA_ERROR_NOT_SUPPORTED );
1718 method = ( export_public_key ?
1719 drv->key_management->p_export_public :
1720 drv->key_management->p_export );
1721 if( method == NULL )
1722 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001723 return( method( drv_context,
1724 slot->data.se.slot_number,
1725 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001726 }
1727#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1728
Gilles Peskine8e338702019-07-30 20:06:31 +02001729 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001730 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001731 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001732 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001733 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1734 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001735 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001736 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001737 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001738 /* Exporting public -> public */
1739 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1740 }
1741 else if( !export_public_key )
1742 {
1743 /* Exporting private -> private */
1744 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1745 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001746
Steven Cooreman560c28a2020-07-24 23:20:24 +02001747 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001748 * so conversion is needed. Try the accelerators first. */
1749 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1750 data,
1751 data_size,
1752 data_length );
1753
1754 if( status != PSA_ERROR_NOT_SUPPORTED ||
1755 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1756 return( status );
1757
Steven Cooreman560c28a2020-07-24 23:20:24 +02001758 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1759 {
John Durkop0e005192020-10-31 22:06:54 -07001760#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1761 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001762 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001763 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001764 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001765 slot->data.key.data,
1766 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001767 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001768 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001769 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001770
Steven Cooreman560c28a2020-07-24 23:20:24 +02001771 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001772 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001773 data,
1774 data_size,
1775 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001776
Steven Cooremana2371e52020-07-28 14:30:39 +02001777 mbedtls_rsa_free( rsa );
1778 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001779
Steven Cooreman560c28a2020-07-24 23:20:24 +02001780 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001781#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001782 /* We don't know how to convert a private RSA key to public. */
1783 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001784#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1785 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001786 }
1787 else
Moran Pekera998bc62018-04-16 18:16:20 +03001788 {
John Durkop6ba40d12020-11-10 08:50:04 -08001789#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1790 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001791 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001792 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001793 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001794 slot->data.key.data,
1795 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001796 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001797 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001798 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001799
1800 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1801 PSA_KEY_TYPE_ECC_GET_FAMILY(
1802 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001803 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001804 data,
1805 data_size,
1806 data_length );
1807
Steven Cooremana2371e52020-07-28 14:30:39 +02001808 mbedtls_ecp_keypair_free( ecp );
1809 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001810 return( status );
1811#else
1812 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001813 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001814#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1815 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001816 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001817 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001818 else
1819 {
1820 /* This shouldn't happen in the reference implementation, but
1821 it is valid for a special-purpose implementation to omit
1822 support for exporting certain key types. */
1823 return( PSA_ERROR_NOT_SUPPORTED );
1824 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001825}
1826
Ronald Croncf56a0a2020-08-04 09:51:30 +02001827psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001828 uint8_t *data,
1829 size_t data_size,
1830 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001831{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001833 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001834 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001835
1836 /* Set the key to empty now, so that even when there are errors, we always
1837 * set data_length to a value between 0 and data_size. On error, setting
1838 * the key to empty is a good choice because an empty key representation is
1839 * unlikely to be accepted anywhere. */
1840 *data_length = 0;
1841
1842 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001843 * which don't require any flag, but
1844 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1845 */
1846 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1847 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001848 if( status != PSA_SUCCESS )
1849 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001850
1851 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001852 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001853
Ronald Cron5c522922020-11-14 16:35:34 +01001854 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001855}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001856
Ronald Croncf56a0a2020-08-04 09:51:30 +02001857psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001858 uint8_t *data,
1859 size_t data_size,
1860 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001861{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001862 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001863 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001864 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001865
1866 /* Set the key to empty now, so that even when there are errors, we always
1867 * set data_length to a value between 0 and data_size. On error, setting
1868 * the key to empty is a good choice because an empty key representation is
1869 * unlikely to be accepted anywhere. */
1870 *data_length = 0;
1871
1872 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001873 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001874 if( status != PSA_SUCCESS )
1875 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001876
1877 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001878 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001879
Ronald Cron5c522922020-11-14 16:35:34 +01001880 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001881}
1882
Gilles Peskine91e8c332019-08-02 19:19:39 +02001883#if defined(static_assert)
1884static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1885 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001886static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001887 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001888static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001889 "One or more key attribute flag is listed as both internal-only and external-only" );
1890#endif
1891
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001892/** Validate that a key policy is internally well-formed.
1893 *
1894 * This function only rejects invalid policies. It does not validate the
1895 * consistency of the policy with respect to other attributes of the key
1896 * such as the key type.
1897 */
1898static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001899{
1900 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001901 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001902 PSA_KEY_USAGE_ENCRYPT |
1903 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001904 PSA_KEY_USAGE_SIGN_HASH |
1905 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001906 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1907 return( PSA_ERROR_INVALID_ARGUMENT );
1908
Gilles Peskine4747d192019-04-17 15:05:45 +02001909 return( PSA_SUCCESS );
1910}
1911
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001912/** Validate the internal consistency of key attributes.
1913 *
1914 * This function only rejects invalid attribute values. If does not
1915 * validate the consistency of the attributes with any key data that may
1916 * be involved in the creation of the key.
1917 *
1918 * Call this function early in the key creation process.
1919 *
1920 * \param[in] attributes Key attributes for the new key.
1921 * \param[out] p_drv On any return, the driver for the key, if any.
1922 * NULL for a transparent key.
1923 *
1924 */
1925static psa_status_t psa_validate_key_attributes(
1926 const psa_key_attributes_t *attributes,
1927 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001928{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001929 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001930 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001931 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001932
Ronald Cron54b90082020-10-29 15:26:43 +01001933 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001934 if( status != PSA_SUCCESS )
1935 return( status );
1936
Ronald Crond2ed4812020-07-17 16:11:30 +02001937 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001938 if( status != PSA_SUCCESS )
1939 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001940
Ronald Cron65f38a32020-10-23 17:11:13 +02001941 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1942 {
1943 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1944 return( PSA_ERROR_INVALID_ARGUMENT );
1945 }
1946 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001947 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001948 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001949 if( status != PSA_SUCCESS )
1950 return( status );
1951 }
1952
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001953 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001954 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001955 return( status );
1956
1957 /* Refuse to create overly large keys.
1958 * Note that this doesn't trigger on import if the attributes don't
1959 * explicitly specify a size (so psa_get_key_bits returns 0), so
1960 * psa_import_key() needs its own checks. */
1961 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1962 return( PSA_ERROR_NOT_SUPPORTED );
1963
Gilles Peskine91e8c332019-08-02 19:19:39 +02001964 /* Reject invalid flags. These should not be reachable through the API. */
1965 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1966 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1967 return( PSA_ERROR_INVALID_ARGUMENT );
1968
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001969 return( PSA_SUCCESS );
1970}
1971
Gilles Peskine4747d192019-04-17 15:05:45 +02001972/** Prepare a key slot to receive key material.
1973 *
1974 * This function allocates a key slot and sets its metadata.
1975 *
1976 * If this function fails, call psa_fail_key_creation().
1977 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001978 * This function is intended to be used as follows:
1979 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001980 * it with the specified attributes, and in case of a volatile key assign it
1981 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001982 * -# Populate the slot with the key material.
1983 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1984 * In case of failure at any step, stop the sequence and call
1985 * psa_fail_key_creation().
1986 *
Ronald Cron5c522922020-11-14 16:35:34 +01001987 * On success, the key slot is locked. It is the responsibility of the caller
1988 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001989 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001990 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001991 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001992 * \param[out] p_slot On success, a pointer to the prepared slot.
1993 * \param[out] p_drv On any return, the driver for the key, if any.
1994 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001995 *
1996 * \retval #PSA_SUCCESS
1997 * The key slot is ready to receive key material.
1998 * \return If this function fails, the key slot is an invalid state.
1999 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002000 */
2001static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02002002 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02002003 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002004 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002005 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02002006{
2007 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02002008 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02002009 psa_key_slot_t *slot;
2010
Gilles Peskinedf179142019-07-15 22:02:14 +02002011 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02002012 *p_drv = NULL;
2013
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002014 status = psa_validate_key_attributes( attributes, p_drv );
2015 if( status != PSA_SUCCESS )
2016 return( status );
2017
Ronald Cronc4d1b512020-07-31 11:26:37 +02002018 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02002019 if( status != PSA_SUCCESS )
2020 return( status );
2021 slot = *p_slot;
2022
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002023 /* We're storing the declared bit-size of the key. It's up to each
2024 * creation mechanism to verify that this information is correct.
2025 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002026 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002027 * is optional (import, copy). In case of a volatile key, assign it the
2028 * volatile key identifier associated to the slot returned to contain its
2029 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002030
2031 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002032 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2033 {
2034#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2035 slot->attr.id = volatile_key_id;
2036#else
2037 slot->attr.id.key_id = volatile_key_id;
2038#endif
2039 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002040
Gilles Peskine91e8c332019-08-02 19:19:39 +02002041 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002042 * external-only flags, query `attributes`. Thanks to the check
2043 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002044 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002045 * may have set. */
2046 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002047
Gilles Peskinecbaff462019-07-12 23:46:04 +02002048#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002049 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002050 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002051 * create the key file in internal storage, create the
2052 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002053 * persistent data. This is done by starting a transaction that will
2054 * encompass these three actions.
2055 * For registering a volatile key, we just need to find an appropriate
2056 * slot number inside the SE. Since the key is designated volatile, creating
2057 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002058 /* The first thing to do is to find a slot number for the new key.
2059 * We save the slot number in persistent storage as part of the
2060 * transaction data. It will be needed to recover if the power
2061 * fails during the key creation process, to clean up on the secure
2062 * element side after restarting. Obtaining a slot number from the
2063 * secure element driver updates its persistent state, but we do not yet
2064 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002065 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002066 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002067 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002068 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002069 &slot->data.se.slot_number );
2070 if( status != PSA_SUCCESS )
2071 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002072
2073 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002074 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002075 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2076 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2077 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2078 psa_crypto_transaction.key.id = slot->attr.id;
2079 status = psa_crypto_save_transaction( );
2080 if( status != PSA_SUCCESS )
2081 {
2082 (void) psa_crypto_stop_transaction( );
2083 return( status );
2084 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002085 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002086 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002087
2088 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2089 {
2090 /* Key registration only makes sense with a secure element. */
2091 return( PSA_ERROR_INVALID_ARGUMENT );
2092 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002093#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2094
Ronald Cronc4d1b512020-07-31 11:26:37 +02002095 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002096}
Gilles Peskine4747d192019-04-17 15:05:45 +02002097
2098/** Finalize the creation of a key once its key material has been set.
2099 *
2100 * This entails writing the key to persistent storage.
2101 *
2102 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002103 * See the documentation of psa_start_key_creation() for the intended use
2104 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002105 *
Ronald Cron5c522922020-11-14 16:35:34 +01002106 * If the finalization succeeds, the function unlocks the key slot (it was
2107 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2108 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002109 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002110 * \param[in,out] slot Pointer to the slot with key material.
2111 * \param[in] driver The secure element driver for the key,
2112 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002113 * \param[out] key On success, identifier of the key. Note that the
2114 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002115 *
2116 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002117 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002118 * \return If this function fails, the key slot is an invalid state.
2119 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002120 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002121static psa_status_t psa_finish_key_creation(
2122 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002123 psa_se_drv_table_entry_t *driver,
2124 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002125{
2126 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002127 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002128 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002129
2130#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002131 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002132 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002133#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2134 if( driver != NULL )
2135 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002136 psa_se_key_data_storage_t data;
2137#if defined(static_assert)
2138 static_assert( sizeof( slot->data.se.slot_number ) ==
2139 sizeof( data.slot_number ),
2140 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002141#endif
2142 memcpy( &data.slot_number, &slot->data.se.slot_number,
2143 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002144 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002145 (uint8_t*) &data,
2146 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002147 }
2148 else
2149#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2150 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002151 /* Key material is saved in export representation in the slot, so
2152 * just pass the slot buffer for storage. */
2153 status = psa_save_persistent_key( &slot->attr,
2154 slot->data.key.data,
2155 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002156 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002157 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002158#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2159
Gilles Peskinecbaff462019-07-12 23:46:04 +02002160#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002161 /* Finish the transaction for a key creation. This does not
2162 * happen when registering an existing key. Detect this case
2163 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002164 * creation of a persistent key in a secure element requires a transaction,
2165 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002166 if( driver != NULL &&
2167 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002168 {
2169 status = psa_save_se_persistent_data( driver );
2170 if( status != PSA_SUCCESS )
2171 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002172 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002173 return( status );
2174 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002175 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002176 }
2177#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2178
Ronald Cron50972942020-11-14 11:28:25 +01002179 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002180 {
2181 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002182 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002183 if( status != PSA_SUCCESS )
2184 *key = MBEDTLS_SVC_KEY_ID_INIT;
2185 }
Ronald Cron50972942020-11-14 11:28:25 +01002186
Gilles Peskine4747d192019-04-17 15:05:45 +02002187 return( status );
2188}
2189
2190/** Abort the creation of a key.
2191 *
2192 * You may call this function after calling psa_start_key_creation(),
2193 * or after psa_finish_key_creation() fails. In other circumstances, this
2194 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002195 * See the documentation of psa_start_key_creation() for the intended use
2196 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002197 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002198 * \param[in,out] slot Pointer to the slot with key material.
2199 * \param[in] driver The secure element driver for the key,
2200 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002201 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002202static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002203 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002204{
Gilles Peskine011e4282019-06-26 18:34:38 +02002205 (void) driver;
2206
Gilles Peskine4747d192019-04-17 15:05:45 +02002207 if( slot == NULL )
2208 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002209
2210#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002211 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002212 * element, and the failure happened later (when saving metadata
2213 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002214 * element.
2215 * https://github.com/ARMmbed/mbed-crypto/issues/217
2216 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002217
Gilles Peskined7729582019-08-05 15:55:54 +02002218 /* Abort the ongoing transaction if any (there may not be one if
2219 * the creation process failed before starting one, or if the
2220 * key creation is a registration of a key in a secure element).
2221 * Earlier functions must already have done what it takes to undo any
2222 * partial creation. All that's left is to update the transaction data
2223 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002224 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002225#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2226
Gilles Peskine4747d192019-04-17 15:05:45 +02002227 psa_wipe_key_slot( slot );
2228}
2229
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002230/** Validate optional attributes during key creation.
2231 *
2232 * Some key attributes are optional during key creation. If they are
2233 * specified in the attributes structure, check that they are consistent
2234 * with the data in the slot.
2235 *
2236 * This function should be called near the end of key creation, after
2237 * the slot in memory is fully populated but before saving persistent data.
2238 */
2239static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002240 const psa_key_slot_t *slot,
2241 const psa_key_attributes_t *attributes )
2242{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002243 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002244 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002245 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002246 return( PSA_ERROR_INVALID_ARGUMENT );
2247 }
2248
2249 if( attributes->domain_parameters_size != 0 )
2250 {
John Durkop0e005192020-10-31 22:06:54 -07002251#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2252 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002253 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002254 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002255 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002256 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002258
Steven Cooreman7f391872020-07-30 14:57:44 +02002259 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002260 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002261 slot->data.key.data,
2262 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002263 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002264 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002265 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002266
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002267 mbedtls_mpi_init( &actual );
2268 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002269 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002270 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002271 mbedtls_rsa_free( rsa );
2272 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002273 if( ret != 0 )
2274 goto rsa_exit;
2275 ret = mbedtls_mpi_read_binary( &required,
2276 attributes->domain_parameters,
2277 attributes->domain_parameters_size );
2278 if( ret != 0 )
2279 goto rsa_exit;
2280 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2281 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2282 rsa_exit:
2283 mbedtls_mpi_free( &actual );
2284 mbedtls_mpi_free( &required );
2285 if( ret != 0)
2286 return( mbedtls_to_psa_error( ret ) );
2287 }
2288 else
John Durkop9814fa22020-11-04 12:28:15 -08002289#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2290 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002291 {
2292 return( PSA_ERROR_INVALID_ARGUMENT );
2293 }
2294 }
2295
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002296 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002297 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002298 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002299 return( PSA_ERROR_INVALID_ARGUMENT );
2300 }
2301
2302 return( PSA_SUCCESS );
2303}
2304
Gilles Peskine4747d192019-04-17 15:05:45 +02002305psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002306 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002307 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002308 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002309{
2310 psa_status_t status;
2311 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002312 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002313
Ronald Cron81709fc2020-11-14 12:10:32 +01002314 *key = MBEDTLS_SVC_KEY_ID_INIT;
2315
Gilles Peskine0f84d622019-09-12 19:03:13 +02002316 /* Reject zero-length symmetric keys (including raw data key objects).
2317 * This also rejects any key which might be encoded as an empty string,
2318 * which is never valid. */
2319 if( data_length == 0 )
2320 return( PSA_ERROR_INVALID_ARGUMENT );
2321
Gilles Peskinedf179142019-07-15 22:02:14 +02002322 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002323 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002324 if( status != PSA_SUCCESS )
2325 goto exit;
2326
Gilles Peskine5d309672019-07-12 23:47:28 +02002327#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2328 if( driver != NULL )
2329 {
2330 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002331 /* The driver should set the number of key bits, however in
2332 * case it doesn't, we initialize bits to an invalid value. */
2333 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002334 if( drv->key_management == NULL ||
2335 drv->key_management->p_import == NULL )
2336 {
2337 status = PSA_ERROR_NOT_SUPPORTED;
2338 goto exit;
2339 }
2340 status = drv->key_management->p_import(
2341 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002342 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002343 &bits );
2344 if( status != PSA_SUCCESS )
2345 goto exit;
2346 if( bits > PSA_MAX_KEY_BITS )
2347 {
2348 status = PSA_ERROR_NOT_SUPPORTED;
2349 goto exit;
2350 }
2351 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002352 }
2353 else
2354#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2355 {
2356 status = psa_import_key_into_slot( slot, data, data_length );
2357 if( status != PSA_SUCCESS )
2358 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002359 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002360 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002361 if( status != PSA_SUCCESS )
2362 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002363
Ronald Cron81709fc2020-11-14 12:10:32 +01002364 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002365exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002366 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002367 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002368
Gilles Peskine4747d192019-04-17 15:05:45 +02002369 return( status );
2370}
2371
Gilles Peskined7729582019-08-05 15:55:54 +02002372#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2373psa_status_t mbedtls_psa_register_se_key(
2374 const psa_key_attributes_t *attributes )
2375{
2376 psa_status_t status;
2377 psa_key_slot_t *slot = NULL;
2378 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002379 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002380
2381 /* Leaving attributes unspecified is not currently supported.
2382 * It could make sense to query the key type and size from the
2383 * secure element, but not all secure elements support this
2384 * and the driver HAL doesn't currently support it. */
2385 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2386 return( PSA_ERROR_NOT_SUPPORTED );
2387 if( psa_get_key_bits( attributes ) == 0 )
2388 return( PSA_ERROR_NOT_SUPPORTED );
2389
2390 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002391 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002392 if( status != PSA_SUCCESS )
2393 goto exit;
2394
Ronald Cron81709fc2020-11-14 12:10:32 +01002395 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002396
2397exit:
2398 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002399 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002400
Gilles Peskined7729582019-08-05 15:55:54 +02002401 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002402 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002403 return( status );
2404}
2405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2406
Gilles Peskinef603c712019-01-19 13:40:11 +01002407static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002408 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002409{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002410 psa_status_t status = psa_copy_key_material_into_slot( target,
2411 source->data.key.data,
2412 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002413 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002414 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002415
Steven Cooreman398aee52020-10-13 14:35:45 +02002416 target->attr.type = source->attr.type;
2417 target->attr.bits = source->attr.bits;
2418
2419 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002420}
2421
Ronald Croncf56a0a2020-08-04 09:51:30 +02002422psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002423 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002424 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002425{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002427 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002428 psa_key_slot_t *source_slot = NULL;
2429 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002430 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002431 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002432
Ronald Cron81709fc2020-11-14 12:10:32 +01002433 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2434
Ronald Cron5c522922020-11-14 16:35:34 +01002435 status = psa_get_and_lock_transparent_key_slot_with_policy(
2436 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002437 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002438 goto exit;
2439
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002440 status = psa_validate_optional_attributes( source_slot,
2441 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002442 if( status != PSA_SUCCESS )
2443 goto exit;
2444
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002445 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002446 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002447 if( status != PSA_SUCCESS )
2448 goto exit;
2449
Ronald Cron81709fc2020-11-14 12:10:32 +01002450 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2451 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002452 if( status != PSA_SUCCESS )
2453 goto exit;
2454
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002455#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2456 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002457 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002458 /* Copying to a secure element is not implemented yet. */
2459 status = PSA_ERROR_NOT_SUPPORTED;
2460 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002461 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002462#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002463
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002464 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002465 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002466 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002467
Ronald Cron81709fc2020-11-14 12:10:32 +01002468 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002469exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002470 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002471 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002472
Ronald Cron5c522922020-11-14 16:35:34 +01002473 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002474
Ronald Cron5c522922020-11-14 16:35:34 +01002475 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002476}
2477
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002478
2479
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002480/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002481/* Message digests */
2482/****************************************************************/
2483
John Durkop07cc04a2020-11-16 22:08:34 -08002484#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002485 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2486 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002487 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002488static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002489{
2490 switch( alg )
2491 {
2492#if defined(MBEDTLS_MD2_C)
2493 case PSA_ALG_MD2:
2494 return( &mbedtls_md2_info );
2495#endif
2496#if defined(MBEDTLS_MD4_C)
2497 case PSA_ALG_MD4:
2498 return( &mbedtls_md4_info );
2499#endif
2500#if defined(MBEDTLS_MD5_C)
2501 case PSA_ALG_MD5:
2502 return( &mbedtls_md5_info );
2503#endif
2504#if defined(MBEDTLS_RIPEMD160_C)
2505 case PSA_ALG_RIPEMD160:
2506 return( &mbedtls_ripemd160_info );
2507#endif
2508#if defined(MBEDTLS_SHA1_C)
2509 case PSA_ALG_SHA_1:
2510 return( &mbedtls_sha1_info );
2511#endif
2512#if defined(MBEDTLS_SHA256_C)
2513 case PSA_ALG_SHA_224:
2514 return( &mbedtls_sha224_info );
2515 case PSA_ALG_SHA_256:
2516 return( &mbedtls_sha256_info );
2517#endif
2518#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002519#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002520 case PSA_ALG_SHA_384:
2521 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002522#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002523 case PSA_ALG_SHA_512:
2524 return( &mbedtls_sha512_info );
2525#endif
2526 default:
2527 return( NULL );
2528 }
2529}
John Durkop07cc04a2020-11-16 22:08:34 -08002530#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002531 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2532 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2533 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002534
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002535psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2536{
2537 switch( operation->alg )
2538 {
Gilles Peskine81736312018-06-26 15:04:31 +02002539 case 0:
2540 /* The object has (apparently) been initialized but it is not
2541 * in use. It's ok to call abort on such an object, and there's
2542 * nothing to do. */
2543 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002544#if defined(MBEDTLS_MD2_C)
2545 case PSA_ALG_MD2:
2546 mbedtls_md2_free( &operation->ctx.md2 );
2547 break;
2548#endif
2549#if defined(MBEDTLS_MD4_C)
2550 case PSA_ALG_MD4:
2551 mbedtls_md4_free( &operation->ctx.md4 );
2552 break;
2553#endif
2554#if defined(MBEDTLS_MD5_C)
2555 case PSA_ALG_MD5:
2556 mbedtls_md5_free( &operation->ctx.md5 );
2557 break;
2558#endif
2559#if defined(MBEDTLS_RIPEMD160_C)
2560 case PSA_ALG_RIPEMD160:
2561 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2562 break;
2563#endif
2564#if defined(MBEDTLS_SHA1_C)
2565 case PSA_ALG_SHA_1:
2566 mbedtls_sha1_free( &operation->ctx.sha1 );
2567 break;
2568#endif
2569#if defined(MBEDTLS_SHA256_C)
2570 case PSA_ALG_SHA_224:
2571 case PSA_ALG_SHA_256:
2572 mbedtls_sha256_free( &operation->ctx.sha256 );
2573 break;
2574#endif
2575#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002576#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002577 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002578#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002579 case PSA_ALG_SHA_512:
2580 mbedtls_sha512_free( &operation->ctx.sha512 );
2581 break;
2582#endif
2583 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002584 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002585 }
2586 operation->alg = 0;
2587 return( PSA_SUCCESS );
2588}
2589
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002590psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002591 psa_algorithm_t alg )
2592{
Janos Follath24eed8d2019-11-22 13:21:35 +00002593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002594
2595 /* A context must be freshly initialized before it can be set up. */
2596 if( operation->alg != 0 )
2597 {
2598 return( PSA_ERROR_BAD_STATE );
2599 }
2600
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002601 switch( alg )
2602 {
2603#if defined(MBEDTLS_MD2_C)
2604 case PSA_ALG_MD2:
2605 mbedtls_md2_init( &operation->ctx.md2 );
2606 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2607 break;
2608#endif
2609#if defined(MBEDTLS_MD4_C)
2610 case PSA_ALG_MD4:
2611 mbedtls_md4_init( &operation->ctx.md4 );
2612 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2613 break;
2614#endif
2615#if defined(MBEDTLS_MD5_C)
2616 case PSA_ALG_MD5:
2617 mbedtls_md5_init( &operation->ctx.md5 );
2618 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2619 break;
2620#endif
2621#if defined(MBEDTLS_RIPEMD160_C)
2622 case PSA_ALG_RIPEMD160:
2623 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2624 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2625 break;
2626#endif
2627#if defined(MBEDTLS_SHA1_C)
2628 case PSA_ALG_SHA_1:
2629 mbedtls_sha1_init( &operation->ctx.sha1 );
2630 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2631 break;
2632#endif
2633#if defined(MBEDTLS_SHA256_C)
2634 case PSA_ALG_SHA_224:
2635 mbedtls_sha256_init( &operation->ctx.sha256 );
2636 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2637 break;
2638 case PSA_ALG_SHA_256:
2639 mbedtls_sha256_init( &operation->ctx.sha256 );
2640 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2641 break;
2642#endif
2643#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002644#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002645 case PSA_ALG_SHA_384:
2646 mbedtls_sha512_init( &operation->ctx.sha512 );
2647 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2648 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002649#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002650 case PSA_ALG_SHA_512:
2651 mbedtls_sha512_init( &operation->ctx.sha512 );
2652 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2653 break;
2654#endif
2655 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002656 return( PSA_ALG_IS_HASH( alg ) ?
2657 PSA_ERROR_NOT_SUPPORTED :
2658 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002659 }
2660 if( ret == 0 )
2661 operation->alg = alg;
2662 else
2663 psa_hash_abort( operation );
2664 return( mbedtls_to_psa_error( ret ) );
2665}
2666
2667psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2668 const uint8_t *input,
2669 size_t input_length )
2670{
Janos Follath24eed8d2019-11-22 13:21:35 +00002671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002672
2673 /* Don't require hash implementations to behave correctly on a
2674 * zero-length input, which may have an invalid pointer. */
2675 if( input_length == 0 )
2676 return( PSA_SUCCESS );
2677
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002678 switch( operation->alg )
2679 {
2680#if defined(MBEDTLS_MD2_C)
2681 case PSA_ALG_MD2:
2682 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2683 input, input_length );
2684 break;
2685#endif
2686#if defined(MBEDTLS_MD4_C)
2687 case PSA_ALG_MD4:
2688 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2689 input, input_length );
2690 break;
2691#endif
2692#if defined(MBEDTLS_MD5_C)
2693 case PSA_ALG_MD5:
2694 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2695 input, input_length );
2696 break;
2697#endif
2698#if defined(MBEDTLS_RIPEMD160_C)
2699 case PSA_ALG_RIPEMD160:
2700 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2701 input, input_length );
2702 break;
2703#endif
2704#if defined(MBEDTLS_SHA1_C)
2705 case PSA_ALG_SHA_1:
2706 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2707 input, input_length );
2708 break;
2709#endif
2710#if defined(MBEDTLS_SHA256_C)
2711 case PSA_ALG_SHA_224:
2712 case PSA_ALG_SHA_256:
2713 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2714 input, input_length );
2715 break;
2716#endif
2717#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002718#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002719 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002720#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002721 case PSA_ALG_SHA_512:
2722 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2723 input, input_length );
2724 break;
2725#endif
2726 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002727 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002728 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002729
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002730 if( ret != 0 )
2731 psa_hash_abort( operation );
2732 return( mbedtls_to_psa_error( ret ) );
2733}
2734
2735psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2736 uint8_t *hash,
2737 size_t hash_size,
2738 size_t *hash_length )
2739{
itayzafrir40835d42018-08-02 13:14:17 +03002740 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002741 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002742 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002743
2744 /* Fill the output buffer with something that isn't a valid hash
2745 * (barring an attack on the hash and deliberately-crafted input),
2746 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002747 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002748 /* If hash_size is 0 then hash may be NULL and then the
2749 * call to memset would have undefined behavior. */
2750 if( hash_size != 0 )
2751 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002752
2753 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002754 {
2755 status = PSA_ERROR_BUFFER_TOO_SMALL;
2756 goto exit;
2757 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002758
2759 switch( operation->alg )
2760 {
2761#if defined(MBEDTLS_MD2_C)
2762 case PSA_ALG_MD2:
2763 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2764 break;
2765#endif
2766#if defined(MBEDTLS_MD4_C)
2767 case PSA_ALG_MD4:
2768 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2769 break;
2770#endif
2771#if defined(MBEDTLS_MD5_C)
2772 case PSA_ALG_MD5:
2773 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2774 break;
2775#endif
2776#if defined(MBEDTLS_RIPEMD160_C)
2777 case PSA_ALG_RIPEMD160:
2778 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2779 break;
2780#endif
2781#if defined(MBEDTLS_SHA1_C)
2782 case PSA_ALG_SHA_1:
2783 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2784 break;
2785#endif
2786#if defined(MBEDTLS_SHA256_C)
2787 case PSA_ALG_SHA_224:
2788 case PSA_ALG_SHA_256:
2789 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2790 break;
2791#endif
2792#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002793#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002794 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002795#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002796 case PSA_ALG_SHA_512:
2797 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2798 break;
2799#endif
2800 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002801 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002802 }
itayzafrir40835d42018-08-02 13:14:17 +03002803 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002804
itayzafrir40835d42018-08-02 13:14:17 +03002805exit:
2806 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002807 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002808 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002809 return( psa_hash_abort( operation ) );
2810 }
2811 else
2812 {
2813 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002814 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002815 }
2816}
2817
Gilles Peskine2d277862018-06-18 15:41:12 +02002818psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2819 const uint8_t *hash,
2820 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002821{
2822 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2823 size_t actual_hash_length;
2824 psa_status_t status = psa_hash_finish( operation,
2825 actual_hash, sizeof( actual_hash ),
2826 &actual_hash_length );
2827 if( status != PSA_SUCCESS )
2828 return( status );
2829 if( actual_hash_length != hash_length )
2830 return( PSA_ERROR_INVALID_SIGNATURE );
2831 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2832 return( PSA_ERROR_INVALID_SIGNATURE );
2833 return( PSA_SUCCESS );
2834}
2835
Gilles Peskine0a749c82019-11-28 19:33:58 +01002836psa_status_t psa_hash_compute( psa_algorithm_t alg,
2837 const uint8_t *input, size_t input_length,
2838 uint8_t *hash, size_t hash_size,
2839 size_t *hash_length )
2840{
2841 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2842 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2843
2844 *hash_length = hash_size;
2845 status = psa_hash_setup( &operation, alg );
2846 if( status != PSA_SUCCESS )
2847 goto exit;
2848 status = psa_hash_update( &operation, input, input_length );
2849 if( status != PSA_SUCCESS )
2850 goto exit;
2851 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2852 if( status != PSA_SUCCESS )
2853 goto exit;
2854
2855exit:
2856 if( status == PSA_SUCCESS )
2857 status = psa_hash_abort( &operation );
2858 else
2859 psa_hash_abort( &operation );
2860 return( status );
2861}
2862
2863psa_status_t psa_hash_compare( psa_algorithm_t alg,
2864 const uint8_t *input, size_t input_length,
2865 const uint8_t *hash, size_t hash_length )
2866{
2867 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2869
2870 status = psa_hash_setup( &operation, alg );
2871 if( status != PSA_SUCCESS )
2872 goto exit;
2873 status = psa_hash_update( &operation, input, input_length );
2874 if( status != PSA_SUCCESS )
2875 goto exit;
2876 status = psa_hash_verify( &operation, hash, hash_length );
2877 if( status != PSA_SUCCESS )
2878 goto exit;
2879
2880exit:
2881 if( status == PSA_SUCCESS )
2882 status = psa_hash_abort( &operation );
2883 else
2884 psa_hash_abort( &operation );
2885 return( status );
2886}
2887
Gilles Peskineeb35d782019-01-22 17:56:16 +01002888psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2889 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002890{
2891 if( target_operation->alg != 0 )
2892 return( PSA_ERROR_BAD_STATE );
2893
2894 switch( source_operation->alg )
2895 {
2896 case 0:
2897 return( PSA_ERROR_BAD_STATE );
2898#if defined(MBEDTLS_MD2_C)
2899 case PSA_ALG_MD2:
2900 mbedtls_md2_clone( &target_operation->ctx.md2,
2901 &source_operation->ctx.md2 );
2902 break;
2903#endif
2904#if defined(MBEDTLS_MD4_C)
2905 case PSA_ALG_MD4:
2906 mbedtls_md4_clone( &target_operation->ctx.md4,
2907 &source_operation->ctx.md4 );
2908 break;
2909#endif
2910#if defined(MBEDTLS_MD5_C)
2911 case PSA_ALG_MD5:
2912 mbedtls_md5_clone( &target_operation->ctx.md5,
2913 &source_operation->ctx.md5 );
2914 break;
2915#endif
2916#if defined(MBEDTLS_RIPEMD160_C)
2917 case PSA_ALG_RIPEMD160:
2918 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2919 &source_operation->ctx.ripemd160 );
2920 break;
2921#endif
2922#if defined(MBEDTLS_SHA1_C)
2923 case PSA_ALG_SHA_1:
2924 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2925 &source_operation->ctx.sha1 );
2926 break;
2927#endif
2928#if defined(MBEDTLS_SHA256_C)
2929 case PSA_ALG_SHA_224:
2930 case PSA_ALG_SHA_256:
2931 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2932 &source_operation->ctx.sha256 );
2933 break;
2934#endif
2935#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002936#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002937 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002938#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002939 case PSA_ALG_SHA_512:
2940 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2941 &source_operation->ctx.sha512 );
2942 break;
2943#endif
2944 default:
2945 return( PSA_ERROR_NOT_SUPPORTED );
2946 }
2947
2948 target_operation->alg = source_operation->alg;
2949 return( PSA_SUCCESS );
2950}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002951
2952
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002953/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954/* MAC */
2955/****************************************************************/
2956
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002957static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002958 psa_algorithm_t alg,
2959 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002960 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002961 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002963 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002964 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002966 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002967 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002968
Gilles Peskine8c9def32018-02-08 10:02:12 +01002969 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2970 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002971 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002973 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002974 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002975 mode = MBEDTLS_MODE_STREAM;
2976 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002977 case PSA_ALG_CTR:
2978 mode = MBEDTLS_MODE_CTR;
2979 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002980 case PSA_ALG_CFB:
2981 mode = MBEDTLS_MODE_CFB;
2982 break;
2983 case PSA_ALG_OFB:
2984 mode = MBEDTLS_MODE_OFB;
2985 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002986 case PSA_ALG_ECB_NO_PADDING:
2987 mode = MBEDTLS_MODE_ECB;
2988 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002989 case PSA_ALG_CBC_NO_PADDING:
2990 mode = MBEDTLS_MODE_CBC;
2991 break;
2992 case PSA_ALG_CBC_PKCS7:
2993 mode = MBEDTLS_MODE_CBC;
2994 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002995 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002996 mode = MBEDTLS_MODE_CCM;
2997 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002998 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002999 mode = MBEDTLS_MODE_GCM;
3000 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003001 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3002 mode = MBEDTLS_MODE_CHACHAPOLY;
3003 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003004 default:
3005 return( NULL );
3006 }
3007 }
3008 else if( alg == PSA_ALG_CMAC )
3009 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010 else
3011 return( NULL );
3012
3013 switch( key_type )
3014 {
3015 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003016 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003017 break;
3018 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003019 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3020 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003022 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003023 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003024 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003025 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3026 * but two-key Triple-DES is functionally three-key Triple-DES
3027 * with K1=K3, so that's how we present it to mbedtls. */
3028 if( key_bits == 128 )
3029 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030 break;
3031 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003032 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033 break;
3034 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003035 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003036 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003037 case PSA_KEY_TYPE_CHACHA20:
3038 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3039 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003040 default:
3041 return( NULL );
3042 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003043 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003044 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003045
Jaeden Amero23bbb752018-06-26 14:16:54 +01003046 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3047 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003048}
3049
John Durkop6ba40d12020-11-10 08:50:04 -08003050#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003051static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003052{
Gilles Peskine2d277862018-06-18 15:41:12 +02003053 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003054 {
3055 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003056 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003057 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003058 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003059 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003060 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003061 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003062 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003063 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003064 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003065 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003066 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003067 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003068 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003069 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003070 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003071 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003072 return( 128 );
3073 default:
3074 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003075 }
3076}
John Durkop07cc04a2020-11-16 22:08:34 -08003077#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003078
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003079/* Initialize the MAC operation structure. Once this function has been
3080 * called, psa_mac_abort can run and will do the right thing. */
3081static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3082 psa_algorithm_t alg )
3083{
3084 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3085
3086 operation->alg = alg;
3087 operation->key_set = 0;
3088 operation->iv_set = 0;
3089 operation->iv_required = 0;
3090 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003091 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003092
3093#if defined(MBEDTLS_CMAC_C)
3094 if( alg == PSA_ALG_CMAC )
3095 {
3096 operation->iv_required = 0;
3097 mbedtls_cipher_init( &operation->ctx.cmac );
3098 status = PSA_SUCCESS;
3099 }
3100 else
3101#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003102#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003103 if( PSA_ALG_IS_HMAC( operation->alg ) )
3104 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003105 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3106 operation->ctx.hmac.hash_ctx.alg = 0;
3107 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003108 }
3109 else
John Durkopd0321952020-10-29 21:37:36 -07003110#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003111 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003112 if( ! PSA_ALG_IS_MAC( alg ) )
3113 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003114 }
3115
3116 if( status != PSA_SUCCESS )
3117 memset( operation, 0, sizeof( *operation ) );
3118 return( status );
3119}
3120
John Durkop6ba40d12020-11-10 08:50:04 -08003121#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003122static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3123{
Gilles Peskine3f108122018-12-07 18:14:53 +01003124 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003125 return( psa_hash_abort( &hmac->hash_ctx ) );
3126}
John Durkop6ba40d12020-11-10 08:50:04 -08003127#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003128
Gilles Peskine8c9def32018-02-08 10:02:12 +01003129psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3130{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003131 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003132 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003133 /* The object has (apparently) been initialized but it is not
3134 * in use. It's ok to call abort on such an object, and there's
3135 * nothing to do. */
3136 return( PSA_SUCCESS );
3137 }
3138 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003139#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003140 if( operation->alg == PSA_ALG_CMAC )
3141 {
3142 mbedtls_cipher_free( &operation->ctx.cmac );
3143 }
3144 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003145#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003146#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003147 if( PSA_ALG_IS_HMAC( operation->alg ) )
3148 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003149 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003150 }
3151 else
John Durkopd0321952020-10-29 21:37:36 -07003152#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003153 {
3154 /* Sanity check (shouldn't happen: operation->alg should
3155 * always have been initialized to a valid value). */
3156 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003157 }
Moran Peker41deec42018-04-04 15:43:05 +03003158
Gilles Peskine8c9def32018-02-08 10:02:12 +01003159 operation->alg = 0;
3160 operation->key_set = 0;
3161 operation->iv_set = 0;
3162 operation->iv_required = 0;
3163 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003164 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003165
Gilles Peskine8c9def32018-02-08 10:02:12 +01003166 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003167
3168bad_state:
3169 /* If abort is called on an uninitialized object, we can't trust
3170 * anything. Wipe the object in case it contains confidential data.
3171 * This may result in a memory leak if a pointer gets overwritten,
3172 * but it's too late to do anything about this. */
3173 memset( operation, 0, sizeof( *operation ) );
3174 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003175}
3176
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003177#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003178static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003179 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003180 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003181 const mbedtls_cipher_info_t *cipher_info )
3182{
Janos Follath24eed8d2019-11-22 13:21:35 +00003183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003184
3185 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003186
3187 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3188 if( ret != 0 )
3189 return( ret );
3190
3191 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003192 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003193 key_bits );
3194 return( ret );
3195}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003196#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003197
John Durkop6ba40d12020-11-10 08:50:04 -08003198#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003199static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3200 const uint8_t *key,
3201 size_t key_length,
3202 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003203{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003204 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003205 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003206 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003207 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003208 psa_status_t status;
3209
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003210 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3211 * overflow below. This should never trigger if the hash algorithm
3212 * is implemented correctly. */
3213 /* The size checks against the ipad and opad buffers cannot be written
3214 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3215 * because that triggers -Wlogical-op on GCC 7.3. */
3216 if( block_size > sizeof( ipad ) )
3217 return( PSA_ERROR_NOT_SUPPORTED );
3218 if( block_size > sizeof( hmac->opad ) )
3219 return( PSA_ERROR_NOT_SUPPORTED );
3220 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003221 return( PSA_ERROR_NOT_SUPPORTED );
3222
Gilles Peskined223b522018-06-11 18:12:58 +02003223 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003224 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003225 status = psa_hash_compute( hash_alg, key, key_length,
3226 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003227 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003228 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003229 }
Gilles Peskine96889972018-07-12 17:07:03 +02003230 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3231 * but it is permitted. It is common when HMAC is used in HKDF, for
3232 * example. Don't call `memcpy` in the 0-length because `key` could be
3233 * an invalid pointer which would make the behavior undefined. */
3234 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003235 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003236
Gilles Peskined223b522018-06-11 18:12:58 +02003237 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3238 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003239 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003240 ipad[i] ^= 0x36;
3241 memset( ipad + key_length, 0x36, block_size - key_length );
3242
3243 /* Copy the key material from ipad to opad, flipping the requisite bits,
3244 * and filling the rest of opad with the requisite constant. */
3245 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003246 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3247 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003248
Gilles Peskine01126fa2018-07-12 17:04:55 +02003249 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003250 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003251 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003252
Gilles Peskine01126fa2018-07-12 17:04:55 +02003253 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003254
3255cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003256 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003257
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003258 return( status );
3259}
John Durkop6ba40d12020-11-10 08:50:04 -08003260#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003261
Gilles Peskine89167cb2018-07-08 20:12:23 +02003262static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003263 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003264 psa_algorithm_t alg,
3265 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003266{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003267 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003268 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003269 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003270 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003271 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003272 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003273 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003274 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003275
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003276 /* A context must be freshly initialized before it can be set up. */
3277 if( operation->alg != 0 )
3278 {
3279 return( PSA_ERROR_BAD_STATE );
3280 }
3281
Gilles Peskined911eb72018-08-14 15:18:45 +02003282 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003283 if( status != PSA_SUCCESS )
3284 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003285 if( is_sign )
3286 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003287
Ronald Cron5c522922020-11-14 16:35:34 +01003288 status = psa_get_and_lock_transparent_key_slot_with_policy(
3289 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003290 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003291 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003292 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003293
Gilles Peskine8c9def32018-02-08 10:02:12 +01003294#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003295 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003296 {
3297 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003298 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003299 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003301 if( cipher_info == NULL )
3302 {
3303 status = PSA_ERROR_NOT_SUPPORTED;
3304 goto exit;
3305 }
3306 operation->mac_size = cipher_info->block_size;
3307 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3308 status = mbedtls_to_psa_error( ret );
3309 }
3310 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003311#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003312#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003313 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003314 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003315 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003316 if( hash_alg == 0 )
3317 {
3318 status = PSA_ERROR_NOT_SUPPORTED;
3319 goto exit;
3320 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003321
3322 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3323 /* Sanity check. This shouldn't fail on a valid configuration. */
3324 if( operation->mac_size == 0 ||
3325 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3326 {
3327 status = PSA_ERROR_NOT_SUPPORTED;
3328 goto exit;
3329 }
3330
Gilles Peskine8e338702019-07-30 20:06:31 +02003331 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003332 {
3333 status = PSA_ERROR_INVALID_ARGUMENT;
3334 goto exit;
3335 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003336
Gilles Peskine01126fa2018-07-12 17:04:55 +02003337 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003338 slot->data.key.data,
3339 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003340 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003341 }
3342 else
John Durkopd0321952020-10-29 21:37:36 -07003343#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003344 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003345 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003346 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003347 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003348
Gilles Peskined911eb72018-08-14 15:18:45 +02003349 if( truncated == 0 )
3350 {
3351 /* The "normal" case: untruncated algorithm. Nothing to do. */
3352 }
3353 else if( truncated < 4 )
3354 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003355 /* A very short MAC is too short for security since it can be
3356 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3357 * so we make this our minimum, even though 32 bits is still
3358 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003359 status = PSA_ERROR_NOT_SUPPORTED;
3360 }
3361 else if( truncated > operation->mac_size )
3362 {
3363 /* It's impossible to "truncate" to a larger length. */
3364 status = PSA_ERROR_INVALID_ARGUMENT;
3365 }
3366 else
3367 operation->mac_size = truncated;
3368
Gilles Peskinefbfac682018-07-08 20:51:54 +02003369exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003370 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003371 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003372 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003373 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003374 else
3375 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003376 operation->key_set = 1;
3377 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003378
Ronald Cron5c522922020-11-14 16:35:34 +01003379 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003380
Ronald Cron5c522922020-11-14 16:35:34 +01003381 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003382}
3383
Gilles Peskine89167cb2018-07-08 20:12:23 +02003384psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003385 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003386 psa_algorithm_t alg )
3387{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003388 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003389}
3390
3391psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003392 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003393 psa_algorithm_t alg )
3394{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003395 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003396}
3397
Gilles Peskine8c9def32018-02-08 10:02:12 +01003398psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3399 const uint8_t *input,
3400 size_t input_length )
3401{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003402 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003403 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003404 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003405 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003406 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003407 operation->has_input = 1;
3408
Gilles Peskine8c9def32018-02-08 10:02:12 +01003409#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003410 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003411 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003412 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3413 input, input_length );
3414 status = mbedtls_to_psa_error( ret );
3415 }
3416 else
3417#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003418#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003419 if( PSA_ALG_IS_HMAC( operation->alg ) )
3420 {
3421 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3422 input_length );
3423 }
3424 else
John Durkopd0321952020-10-29 21:37:36 -07003425#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003426 {
3427 /* This shouldn't happen if `operation` was initialized by
3428 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003429 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003430 }
3431
Gilles Peskinefbfac682018-07-08 20:51:54 +02003432 if( status != PSA_SUCCESS )
3433 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003434 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003435}
3436
John Durkop6ba40d12020-11-10 08:50:04 -08003437#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003438static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3439 uint8_t *mac,
3440 size_t mac_size )
3441{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003442 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003443 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3444 size_t hash_size = 0;
3445 size_t block_size = psa_get_hash_block_size( hash_alg );
3446 psa_status_t status;
3447
Gilles Peskine01126fa2018-07-12 17:04:55 +02003448 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3449 if( status != PSA_SUCCESS )
3450 return( status );
3451 /* From here on, tmp needs to be wiped. */
3452
3453 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3454 if( status != PSA_SUCCESS )
3455 goto exit;
3456
3457 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3458 if( status != PSA_SUCCESS )
3459 goto exit;
3460
3461 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3462 if( status != PSA_SUCCESS )
3463 goto exit;
3464
Gilles Peskined911eb72018-08-14 15:18:45 +02003465 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3466 if( status != PSA_SUCCESS )
3467 goto exit;
3468
3469 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003470
3471exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003472 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003473 return( status );
3474}
John Durkop6ba40d12020-11-10 08:50:04 -08003475#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003476
mohammad16036df908f2018-04-02 08:34:15 -07003477static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003478 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003479 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003480{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003481 if( ! operation->key_set )
3482 return( PSA_ERROR_BAD_STATE );
3483 if( operation->iv_required && ! operation->iv_set )
3484 return( PSA_ERROR_BAD_STATE );
3485
Gilles Peskine8c9def32018-02-08 10:02:12 +01003486 if( mac_size < operation->mac_size )
3487 return( PSA_ERROR_BUFFER_TOO_SMALL );
3488
Gilles Peskine8c9def32018-02-08 10:02:12 +01003489#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003490 if( operation->alg == PSA_ALG_CMAC )
3491 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003492 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3493 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3494 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003495 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003496 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003497 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003498 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003499 else
3500#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003501#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003502 if( PSA_ALG_IS_HMAC( operation->alg ) )
3503 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003504 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003505 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003506 }
3507 else
John Durkopd0321952020-10-29 21:37:36 -07003508#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003509 {
3510 /* This shouldn't happen if `operation` was initialized by
3511 * a setup function. */
3512 return( PSA_ERROR_BAD_STATE );
3513 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003514}
3515
Gilles Peskineacd4be32018-07-08 19:56:25 +02003516psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3517 uint8_t *mac,
3518 size_t mac_size,
3519 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003520{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003521 psa_status_t status;
3522
Jaeden Amero252ef282019-02-15 14:05:35 +00003523 if( operation->alg == 0 )
3524 {
3525 return( PSA_ERROR_BAD_STATE );
3526 }
3527
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003528 /* Fill the output buffer with something that isn't a valid mac
3529 * (barring an attack on the mac and deliberately-crafted input),
3530 * in case the caller doesn't check the return status properly. */
3531 *mac_length = mac_size;
3532 /* If mac_size is 0 then mac may be NULL and then the
3533 * call to memset would have undefined behavior. */
3534 if( mac_size != 0 )
3535 memset( mac, '!', mac_size );
3536
Gilles Peskine89167cb2018-07-08 20:12:23 +02003537 if( ! operation->is_sign )
3538 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003539 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003540 }
mohammad16036df908f2018-04-02 08:34:15 -07003541
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003542 status = psa_mac_finish_internal( operation, mac, mac_size );
3543
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003544 if( status == PSA_SUCCESS )
3545 {
3546 status = psa_mac_abort( operation );
3547 if( status == PSA_SUCCESS )
3548 *mac_length = operation->mac_size;
3549 else
3550 memset( mac, '!', mac_size );
3551 }
3552 else
3553 psa_mac_abort( operation );
3554 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003555}
3556
Gilles Peskineacd4be32018-07-08 19:56:25 +02003557psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3558 const uint8_t *mac,
3559 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003560{
Gilles Peskine828ed142018-06-18 23:25:51 +02003561 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003562 psa_status_t status;
3563
Jaeden Amero252ef282019-02-15 14:05:35 +00003564 if( operation->alg == 0 )
3565 {
3566 return( PSA_ERROR_BAD_STATE );
3567 }
3568
Gilles Peskine89167cb2018-07-08 20:12:23 +02003569 if( operation->is_sign )
3570 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003571 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003572 }
3573 if( operation->mac_size != mac_length )
3574 {
3575 status = PSA_ERROR_INVALID_SIGNATURE;
3576 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003577 }
mohammad16036df908f2018-04-02 08:34:15 -07003578
3579 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003580 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003581 if( status != PSA_SUCCESS )
3582 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003583
3584 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3585 status = PSA_ERROR_INVALID_SIGNATURE;
3586
3587cleanup:
3588 if( status == PSA_SUCCESS )
3589 status = psa_mac_abort( operation );
3590 else
3591 psa_mac_abort( operation );
3592
Gilles Peskine3f108122018-12-07 18:14:53 +01003593 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003594
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003595 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003596}
3597
3598
Gilles Peskine20035e32018-02-03 22:44:14 +01003599
Gilles Peskine20035e32018-02-03 22:44:14 +01003600/****************************************************************/
3601/* Asymmetric cryptography */
3602/****************************************************************/
3603
John Durkop6ba40d12020-11-10 08:50:04 -08003604#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3605 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003606/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003607 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003608static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3609 size_t hash_length,
3610 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003611{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003612 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003613 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003614 *md_alg = mbedtls_md_get_type( md_info );
3615
3616 /* The Mbed TLS RSA module uses an unsigned int for hash length
3617 * parameters. Validate that it fits so that we don't risk an
3618 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003619#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003620 if( hash_length > UINT_MAX )
3621 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003622#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003623
John Durkop0e005192020-10-31 22:06:54 -07003624#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003625 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3626 * must be correct. */
3627 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3628 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003629 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003630 if( md_info == NULL )
3631 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003632 if( mbedtls_md_get_size( md_info ) != hash_length )
3633 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003634 }
John Durkop0e005192020-10-31 22:06:54 -07003635#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003636
John Durkop0e005192020-10-31 22:06:54 -07003637#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003638 /* PSS requires a hash internally. */
3639 if( PSA_ALG_IS_RSA_PSS( alg ) )
3640 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003641 if( md_info == NULL )
3642 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003643 }
John Durkop0e005192020-10-31 22:06:54 -07003644#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003645
Gilles Peskine61b91d42018-06-08 16:09:36 +02003646 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003647}
3648
Gilles Peskine2b450e32018-06-27 15:42:46 +02003649static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3650 psa_algorithm_t alg,
3651 const uint8_t *hash,
3652 size_t hash_length,
3653 uint8_t *signature,
3654 size_t signature_size,
3655 size_t *signature_length )
3656{
3657 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003659 mbedtls_md_type_t md_alg;
3660
3661 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3662 if( status != PSA_SUCCESS )
3663 return( status );
3664
Gilles Peskine630a18a2018-06-29 17:49:35 +02003665 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003666 return( PSA_ERROR_BUFFER_TOO_SMALL );
3667
John Durkop0e005192020-10-31 22:06:54 -07003668#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003669 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3670 {
3671 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3672 MBEDTLS_MD_NONE );
3673 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003674 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003675 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003676 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003677 md_alg,
3678 (unsigned int) hash_length,
3679 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003680 signature );
3681 }
3682 else
John Durkop0e005192020-10-31 22:06:54 -07003683#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3684#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003685 if( PSA_ALG_IS_RSA_PSS( alg ) )
3686 {
3687 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3688 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003689 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003690 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003691 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003692 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003693 (unsigned int) hash_length,
3694 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003695 signature );
3696 }
3697 else
John Durkop0e005192020-10-31 22:06:54 -07003698#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003699 {
3700 return( PSA_ERROR_INVALID_ARGUMENT );
3701 }
3702
3703 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003704 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003705 return( mbedtls_to_psa_error( ret ) );
3706}
3707
3708static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3709 psa_algorithm_t alg,
3710 const uint8_t *hash,
3711 size_t hash_length,
3712 const uint8_t *signature,
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 Peskine89cc74f2019-09-12 22:08:23 +02003723 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3724 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003725
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_verify( 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_PUBLIC,
3735 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003736 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003737 hash,
3738 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_verify( 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_PUBLIC,
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 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003760
3761 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3762 * the rest of the signature is invalid". This has little use in
3763 * practice and PSA doesn't report this distinction. */
3764 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3765 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003766 return( mbedtls_to_psa_error( ret ) );
3767}
John Durkop6ba40d12020-11-10 08:50:04 -08003768#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3769 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003770
John Durkop6ba40d12020-11-10 08:50:04 -08003771#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3772 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003773/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3774 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3775 * (even though these functions don't modify it). */
3776static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3777 psa_algorithm_t alg,
3778 const uint8_t *hash,
3779 size_t hash_length,
3780 uint8_t *signature,
3781 size_t signature_size,
3782 size_t *signature_length )
3783{
Janos Follath24eed8d2019-11-22 13:21:35 +00003784 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003785 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003786 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003787 mbedtls_mpi_init( &r );
3788 mbedtls_mpi_init( &s );
3789
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003790 if( signature_size < 2 * curve_bytes )
3791 {
3792 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3793 goto cleanup;
3794 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003795
John Durkop0ea39e02020-10-13 19:58:20 -07003796#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003797 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3798 {
3799 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3800 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3801 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003802 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3803 &ecp->d, hash,
3804 hash_length, md_alg,
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 Peskinea81d85b2018-06-26 16:10:23 +02003807 }
3808 else
John Durkop0ea39e02020-10-13 19:58:20 -07003809#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003810 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003811 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003812 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3813 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003814 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003815 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003816 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003817
3818 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3819 signature,
3820 curve_bytes ) );
3821 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3822 signature + curve_bytes,
3823 curve_bytes ) );
3824
3825cleanup:
3826 mbedtls_mpi_free( &r );
3827 mbedtls_mpi_free( &s );
3828 if( ret == 0 )
3829 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003830 return( mbedtls_to_psa_error( ret ) );
3831}
3832
3833static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3834 const uint8_t *hash,
3835 size_t hash_length,
3836 const uint8_t *signature,
3837 size_t signature_length )
3838{
Janos Follath24eed8d2019-11-22 13:21:35 +00003839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003840 mbedtls_mpi r, s;
3841 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3842 mbedtls_mpi_init( &r );
3843 mbedtls_mpi_init( &s );
3844
3845 if( signature_length != 2 * curve_bytes )
3846 return( PSA_ERROR_INVALID_SIGNATURE );
3847
3848 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3849 signature,
3850 curve_bytes ) );
3851 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3852 signature + curve_bytes,
3853 curve_bytes ) );
3854
Steven Cooremanacda8342020-07-24 23:09:52 +02003855 /* Check whether the public part is loaded. If not, load it. */
3856 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3857 {
3858 MBEDTLS_MPI_CHK(
3859 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003860 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003861 }
3862
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003863 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3864 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003865
3866cleanup:
3867 mbedtls_mpi_free( &r );
3868 mbedtls_mpi_free( &s );
3869 return( mbedtls_to_psa_error( ret ) );
3870}
John Durkop6ba40d12020-11-10 08:50:04 -08003871#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3872 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003873
Ronald Croncf56a0a2020-08-04 09:51:30 +02003874psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003875 psa_algorithm_t alg,
3876 const uint8_t *hash,
3877 size_t hash_length,
3878 uint8_t *signature,
3879 size_t signature_size,
3880 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003881{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003882 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003883 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003884 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003885
3886 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003887 /* Immediately reject a zero-length signature buffer. This guarantees
3888 * that signature must be a valid pointer. (On the other hand, the hash
3889 * buffer can in principle be empty since it doesn't actually have
3890 * to be a hash.) */
3891 if( signature_size == 0 )
3892 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003893
Ronald Cron5c522922020-11-14 16:35:34 +01003894 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3895 PSA_KEY_USAGE_SIGN_HASH,
3896 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003897 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003898 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003899 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003900 {
3901 status = PSA_ERROR_INVALID_ARGUMENT;
3902 goto exit;
3903 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003904
Steven Cooremancd84cb42020-07-16 20:28:36 +02003905 /* Try any of the available accelerators first */
3906 status = psa_driver_wrapper_sign_hash( slot,
3907 alg,
3908 hash,
3909 hash_length,
3910 signature,
3911 signature_size,
3912 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003913 if( status != PSA_ERROR_NOT_SUPPORTED ||
3914 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003915 goto exit;
3916
Steven Cooreman7a250572020-07-17 16:43:05 +02003917 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003918#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3919 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003920 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003921 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003922 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003923
Steven Cooreman4fed4552020-08-03 14:46:03 +02003924 status = psa_load_rsa_representation( slot->attr.type,
3925 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003926 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003927 &rsa );
3928 if( status != PSA_SUCCESS )
3929 goto exit;
3930
Steven Cooremana2371e52020-07-28 14:30:39 +02003931 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003932 alg,
3933 hash, hash_length,
3934 signature, signature_size,
3935 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003936
Steven Cooremana2371e52020-07-28 14:30:39 +02003937 mbedtls_rsa_free( rsa );
3938 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003939 }
3940 else
John Durkop6ba40d12020-11-10 08:50:04 -08003941#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3942 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003943 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003944 {
John Durkop9814fa22020-11-04 12:28:15 -08003945#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3946 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003947 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003948#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003949 PSA_ALG_IS_ECDSA( alg )
3950#else
3951 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3952#endif
3953 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003954 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003955 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003956 status = psa_load_ecp_representation( slot->attr.type,
3957 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003958 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003959 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003960 if( status != PSA_SUCCESS )
3961 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003962 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003963 alg,
3964 hash, hash_length,
3965 signature, signature_size,
3966 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003967 mbedtls_ecp_keypair_free( ecp );
3968 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003969 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003970 else
John Durkop9814fa22020-11-04 12:28:15 -08003971#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3972 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003973 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003974 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003975 }
itayzafrir5c753392018-05-08 11:18:38 +03003976 }
3977 else
itayzafrir5c753392018-05-08 11:18:38 +03003978 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003979 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003980 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003981
3982exit:
3983 /* Fill the unused part of the output buffer (the whole buffer on error,
3984 * the trailing part on success) with something that isn't a valid mac
3985 * (barring an attack on the mac and deliberately-crafted input),
3986 * in case the caller doesn't check the return status properly. */
3987 if( status == PSA_SUCCESS )
3988 memset( signature + *signature_length, '!',
3989 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003990 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003991 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003992 /* If signature_size is 0 then we have nothing to do. We must not call
3993 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003994
Ronald Cron5c522922020-11-14 16:35:34 +01003995 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003996
Ronald Cron5c522922020-11-14 16:35:34 +01003997 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003998}
3999
Ronald Croncf56a0a2020-08-04 09:51:30 +02004000psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004001 psa_algorithm_t alg,
4002 const uint8_t *hash,
4003 size_t hash_length,
4004 const uint8_t *signature,
4005 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004006{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004007 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004008 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004009 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004010
Ronald Cron5c522922020-11-14 16:35:34 +01004011 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4012 PSA_KEY_USAGE_VERIFY_HASH,
4013 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004014 if( status != PSA_SUCCESS )
4015 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004016
Steven Cooreman55ae2172020-07-17 19:46:15 +02004017 /* Try any of the available accelerators first */
4018 status = psa_driver_wrapper_verify_hash( slot,
4019 alg,
4020 hash,
4021 hash_length,
4022 signature,
4023 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004024 if( status != PSA_ERROR_NOT_SUPPORTED ||
4025 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004026 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004027
John Durkop6ba40d12020-11-10 08:50:04 -08004028#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4029 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004030 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004031 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004032 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004033
Steven Cooreman4fed4552020-08-03 14:46:03 +02004034 status = psa_load_rsa_representation( slot->attr.type,
4035 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004036 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004037 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004038 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004039 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004040
Steven Cooremana2371e52020-07-28 14:30:39 +02004041 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004042 alg,
4043 hash, hash_length,
4044 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004045 mbedtls_rsa_free( rsa );
4046 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004047 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004048 }
4049 else
John Durkop6ba40d12020-11-10 08:50:04 -08004050#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4051 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004052 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004053 {
John Durkop9814fa22020-11-04 12:28:15 -08004054#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4055 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004056 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004057 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004058 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004059 status = psa_load_ecp_representation( slot->attr.type,
4060 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004061 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004062 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004063 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004064 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004065 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004066 hash, hash_length,
4067 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004068 mbedtls_ecp_keypair_free( ecp );
4069 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004070 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004071 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004072 else
John Durkop9814fa22020-11-04 12:28:15 -08004073#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4074 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004075 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004076 status = PSA_ERROR_INVALID_ARGUMENT;
4077 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004078 }
itayzafrir5c753392018-05-08 11:18:38 +03004079 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004080 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004081 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004082 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004083 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004084
4085exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004086 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004087
Ronald Cron5c522922020-11-14 16:35:34 +01004088 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004089}
4090
John Durkop0e005192020-10-31 22:06:54 -07004091#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004092static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4093 mbedtls_rsa_context *rsa )
4094{
4095 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4096 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4097 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4098 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4099}
John Durkop0e005192020-10-31 22:06:54 -07004100#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004101
Ronald Croncf56a0a2020-08-04 09:51:30 +02004102psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004103 psa_algorithm_t alg,
4104 const uint8_t *input,
4105 size_t input_length,
4106 const uint8_t *salt,
4107 size_t salt_length,
4108 uint8_t *output,
4109 size_t output_size,
4110 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004111{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004113 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004114 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004115
Darryl Green5cc689a2018-07-24 15:34:10 +01004116 (void) input;
4117 (void) input_length;
4118 (void) salt;
4119 (void) output;
4120 (void) output_size;
4121
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004122 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004123
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004124 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4125 return( PSA_ERROR_INVALID_ARGUMENT );
4126
Ronald Cron5c522922020-11-14 16:35:34 +01004127 status = psa_get_and_lock_transparent_key_slot_with_policy(
4128 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004129 if( status != PSA_SUCCESS )
4130 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004131 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4132 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004133 {
4134 status = PSA_ERROR_INVALID_ARGUMENT;
4135 goto exit;
4136 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004137
John Durkop6ba40d12020-11-10 08:50:04 -08004138#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4139 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004140 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004141 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004142 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004143 status = psa_load_rsa_representation( slot->attr.type,
4144 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004145 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004146 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004147 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004148 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004149
4150 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004151 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004152 status = PSA_ERROR_BUFFER_TOO_SMALL;
4153 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004154 }
John Durkop0e005192020-10-31 22:06:54 -07004155#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004156 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004157 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004158 status = mbedtls_to_psa_error(
4159 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004160 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004161 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004162 MBEDTLS_RSA_PUBLIC,
4163 input_length,
4164 input,
4165 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004166 }
4167 else
John Durkop0e005192020-10-31 22:06:54 -07004168#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4169#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004170 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004171 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004172 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004173 status = mbedtls_to_psa_error(
4174 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004175 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004176 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004177 MBEDTLS_RSA_PUBLIC,
4178 salt, salt_length,
4179 input_length,
4180 input,
4181 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004182 }
4183 else
John Durkop0e005192020-10-31 22:06:54 -07004184#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004185 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004186 status = PSA_ERROR_INVALID_ARGUMENT;
4187 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004188 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004189rsa_exit:
4190 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004191 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004192
Steven Cooremana2371e52020-07-28 14:30:39 +02004193 mbedtls_rsa_free( rsa );
4194 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004195 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004196 else
John Durkop9814fa22020-11-04 12:28:15 -08004197#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004198 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004200 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004201 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004202
4203exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004204 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004205
Ronald Cron5c522922020-11-14 16:35:34 +01004206 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004207}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004208
Ronald Croncf56a0a2020-08-04 09:51:30 +02004209psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004210 psa_algorithm_t alg,
4211 const uint8_t *input,
4212 size_t input_length,
4213 const uint8_t *salt,
4214 size_t salt_length,
4215 uint8_t *output,
4216 size_t output_size,
4217 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004218{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004219 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004220 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004221 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004222
Darryl Green5cc689a2018-07-24 15:34:10 +01004223 (void) input;
4224 (void) input_length;
4225 (void) salt;
4226 (void) output;
4227 (void) output_size;
4228
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004229 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004230
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004231 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4232 return( PSA_ERROR_INVALID_ARGUMENT );
4233
Ronald Cron5c522922020-11-14 16:35:34 +01004234 status = psa_get_and_lock_transparent_key_slot_with_policy(
4235 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004236 if( status != PSA_SUCCESS )
4237 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004238 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004239 {
4240 status = PSA_ERROR_INVALID_ARGUMENT;
4241 goto exit;
4242 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004243
John Durkop6ba40d12020-11-10 08:50:04 -08004244#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4245 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004246 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004247 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004248 mbedtls_rsa_context *rsa = NULL;
4249 status = psa_load_rsa_representation( slot->attr.type,
4250 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004251 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004252 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004253 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004254 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004255
Steven Cooremana2371e52020-07-28 14:30:39 +02004256 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004257 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004258 status = PSA_ERROR_INVALID_ARGUMENT;
4259 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004260 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004261
John Durkop0e005192020-10-31 22:06:54 -07004262#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004263 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004264 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004265 status = mbedtls_to_psa_error(
4266 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004267 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004268 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004269 MBEDTLS_RSA_PRIVATE,
4270 output_length,
4271 input,
4272 output,
4273 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004274 }
4275 else
John Durkop0e005192020-10-31 22:06:54 -07004276#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4277#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004278 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004279 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004280 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004281 status = mbedtls_to_psa_error(
4282 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004283 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004284 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004285 MBEDTLS_RSA_PRIVATE,
4286 salt, salt_length,
4287 output_length,
4288 input,
4289 output,
4290 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004291 }
4292 else
John Durkop0e005192020-10-31 22:06:54 -07004293#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004294 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004295 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004296 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004297
Steven Cooreman4fed4552020-08-03 14:46:03 +02004298rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004299 mbedtls_rsa_free( rsa );
4300 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004301 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004302 else
John Durkop6ba40d12020-11-10 08:50:04 -08004303#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4304 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004305 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004306 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004307 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004308
4309exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004310 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004311
Ronald Cron5c522922020-11-14 16:35:34 +01004312 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004313}
Gilles Peskine20035e32018-02-03 22:44:14 +01004314
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004315
4316
mohammad1603503973b2018-03-12 15:59:30 +02004317/****************************************************************/
4318/* Symmetric cryptography */
4319/****************************************************************/
4320
Gilles Peskinee553c652018-06-04 16:22:46 +02004321static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004322 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004323 psa_algorithm_t alg,
4324 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004325{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004327 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004328 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004329 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004330 size_t key_bits;
4331 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004332 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4333 PSA_KEY_USAGE_ENCRYPT :
4334 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004335
Steven Cooremand3feccd2020-09-01 15:56:14 +02004336 /* A context must be freshly initialized before it can be set up. */
4337 if( operation->alg != 0 )
4338 return( PSA_ERROR_BAD_STATE );
4339
Steven Cooremana07b9972020-09-10 14:54:14 +02004340 /* The requested algorithm must be one that can be processed by cipher. */
4341 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004342 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004343
Steven Cooremanef8575e2020-09-11 11:44:50 +02004344 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004345 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004346 if( status != PSA_SUCCESS )
4347 goto exit;
4348
4349 /* Initialize the operation struct members, except for alg. The alg member
4350 * is used to indicate to psa_cipher_abort that there are resources to free,
4351 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004352 operation->key_set = 0;
4353 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004354 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004355 operation->iv_size = 0;
4356 operation->block_size = 0;
4357 if( alg == PSA_ALG_ECB_NO_PADDING )
4358 operation->iv_required = 0;
4359 else
4360 operation->iv_required = 1;
4361
Steven Cooremana07b9972020-09-10 14:54:14 +02004362 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004363 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004364 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004365 slot,
4366 alg );
4367 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004368 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004369 slot,
4370 alg );
4371
Steven Cooremanef8575e2020-09-11 11:44:50 +02004372 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004373 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004374 /* Once the driver context is initialised, it needs to be freed using
4375 * psa_cipher_abort. Indicate this through setting alg. */
4376 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004377 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004378
Steven Cooremand3feccd2020-09-01 15:56:14 +02004379 if( status != PSA_ERROR_NOT_SUPPORTED ||
4380 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4381 goto exit;
4382
Steven Cooremana07b9972020-09-10 14:54:14 +02004383 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004384 * available for the given algorithm & key. */
4385 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004386
Steven Cooremana07b9972020-09-10 14:54:14 +02004387 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004388 * psa_cipher_abort. Indicate there is something to be freed through setting
4389 * alg, and indicate the operation is being done using mbedtls crypto through
4390 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004391 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004392 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004393
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004394 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004395 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004396 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004397 {
4398 status = PSA_ERROR_NOT_SUPPORTED;
4399 goto exit;
4400 }
mohammad1603503973b2018-03-12 15:59:30 +02004401
mohammad1603503973b2018-03-12 15:59:30 +02004402 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004403 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004404 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004405
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004406#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004407 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004408 {
4409 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004410 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004411 memcpy( keys, slot->data.key.data, 16 );
4412 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004413 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4414 keys,
4415 192, cipher_operation );
4416 }
4417 else
4418#endif
4419 {
4420 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004421 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004422 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004423 }
Moran Peker41deec42018-04-04 15:43:05 +03004424 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004425 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004426
mohammad16038481e742018-03-18 13:57:31 +02004427#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004428 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004429 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004430 case PSA_ALG_CBC_NO_PADDING:
4431 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4432 MBEDTLS_PADDING_NONE );
4433 break;
4434 case PSA_ALG_CBC_PKCS7:
4435 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4436 MBEDTLS_PADDING_PKCS7 );
4437 break;
4438 default:
4439 /* The algorithm doesn't involve padding. */
4440 ret = 0;
4441 break;
4442 }
4443 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004444 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004445#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4446
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004447 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004448 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004449 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4450 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004451 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004452 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004453 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004454#if defined(MBEDTLS_CHACHA20_C)
4455 else
4456 if( alg == PSA_ALG_CHACHA20 )
4457 operation->iv_size = 12;
4458#endif
mohammad1603503973b2018-03-12 15:59:30 +02004459
Steven Cooreman7df02922020-09-09 15:28:49 +02004460 status = PSA_SUCCESS;
4461
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004462exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004463 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004464 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004465 if( status == PSA_SUCCESS )
4466 {
4467 /* Update operation flags for both driver and software implementations */
4468 operation->key_set = 1;
4469 }
4470 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004471 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004472
Ronald Cron5c522922020-11-14 16:35:34 +01004473 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004474
Ronald Cron5c522922020-11-14 16:35:34 +01004475 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004476}
4477
Gilles Peskinefe119512018-07-08 21:39:34 +02004478psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004479 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004480 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004481{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004482 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004483}
4484
Gilles Peskinefe119512018-07-08 21:39:34 +02004485psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004486 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004487 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004488{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004489 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004490}
4491
Gilles Peskinefe119512018-07-08 21:39:34 +02004492psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004493 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004494 size_t iv_size,
4495 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004496{
itayzafrir534bd7c2018-08-02 13:56:32 +03004497 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004499 if( operation->iv_set || ! operation->iv_required )
4500 {
4501 return( PSA_ERROR_BAD_STATE );
4502 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004503
Steven Cooremanef8575e2020-09-11 11:44:50 +02004504 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004505 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004506 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004507 iv,
4508 iv_size,
4509 iv_length );
4510 goto exit;
4511 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004512
Moran Peker41deec42018-04-04 15:43:05 +03004513 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004514 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004515 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004516 goto exit;
4517 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004518 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004519 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004520 if( ret != 0 )
4521 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004522 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004523 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004524 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004525
mohammad16038481e742018-03-18 13:57:31 +02004526 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004527 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004528
Moran Peker395db872018-05-31 14:07:14 +03004529exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004530 if( status == PSA_SUCCESS )
4531 operation->iv_set = 1;
4532 else
Moran Peker395db872018-05-31 14:07:14 +03004533 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004534 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004535}
4536
Gilles Peskinefe119512018-07-08 21:39:34 +02004537psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004538 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004539 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004540{
itayzafrir534bd7c2018-08-02 13:56:32 +03004541 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004543 if( operation->iv_set || ! operation->iv_required )
4544 {
4545 return( PSA_ERROR_BAD_STATE );
4546 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004547
Steven Cooremanef8575e2020-09-11 11:44:50 +02004548 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004549 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004550 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004551 iv,
4552 iv_length );
4553 goto exit;
4554 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004555
Moran Pekera28258c2018-05-29 16:25:04 +03004556 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004557 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004558 status = PSA_ERROR_INVALID_ARGUMENT;
4559 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004560 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004561 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4562 status = mbedtls_to_psa_error( ret );
4563exit:
4564 if( status == PSA_SUCCESS )
4565 operation->iv_set = 1;
4566 else
mohammad1603503973b2018-03-12 15:59:30 +02004567 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004568 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004569}
4570
Steven Cooreman177deba2020-09-07 17:14:14 +02004571/* Process input for which the algorithm is set to ECB mode. This requires
4572 * manual processing, since the PSA API is defined as being able to process
4573 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4574 * underlying mbedtls_cipher_update only takes full blocks. */
4575static psa_status_t psa_cipher_update_ecb_internal(
4576 mbedtls_cipher_context_t *ctx,
4577 const uint8_t *input,
4578 size_t input_length,
4579 uint8_t *output,
4580 size_t output_size,
4581 size_t *output_length )
4582{
4583 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4584 size_t block_size = ctx->cipher_info->block_size;
4585 size_t internal_output_length = 0;
4586 *output_length = 0;
4587
4588 if( input_length == 0 )
4589 {
4590 status = PSA_SUCCESS;
4591 goto exit;
4592 }
4593
4594 if( ctx->unprocessed_len > 0 )
4595 {
4596 /* Fill up to block size, and run the block if there's a full one. */
4597 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4598
4599 if( input_length < bytes_to_copy )
4600 bytes_to_copy = input_length;
4601
4602 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4603 input, bytes_to_copy );
4604 input_length -= bytes_to_copy;
4605 input += bytes_to_copy;
4606 ctx->unprocessed_len += bytes_to_copy;
4607
4608 if( ctx->unprocessed_len == block_size )
4609 {
4610 status = mbedtls_to_psa_error(
4611 mbedtls_cipher_update( ctx,
4612 ctx->unprocessed_data,
4613 block_size,
4614 output, &internal_output_length ) );
4615
4616 if( status != PSA_SUCCESS )
4617 goto exit;
4618
4619 output += internal_output_length;
4620 output_size -= internal_output_length;
4621 *output_length += internal_output_length;
4622 ctx->unprocessed_len = 0;
4623 }
4624 }
4625
4626 while( input_length >= block_size )
4627 {
4628 /* Run all full blocks we have, one by one */
4629 status = mbedtls_to_psa_error(
4630 mbedtls_cipher_update( ctx, input,
4631 block_size,
4632 output, &internal_output_length ) );
4633
4634 if( status != PSA_SUCCESS )
4635 goto exit;
4636
4637 input_length -= block_size;
4638 input += block_size;
4639
4640 output += internal_output_length;
4641 output_size -= internal_output_length;
4642 *output_length += internal_output_length;
4643 }
4644
4645 if( input_length > 0 )
4646 {
4647 /* Save unprocessed bytes for later processing */
4648 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4649 input, input_length );
4650 ctx->unprocessed_len += input_length;
4651 }
4652
4653 status = PSA_SUCCESS;
4654
4655exit:
4656 return( status );
4657}
4658
Gilles Peskinee553c652018-06-04 16:22:46 +02004659psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4660 const uint8_t *input,
4661 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004662 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004663 size_t output_size,
4664 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004665{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004667 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004668 if( operation->alg == 0 )
4669 {
4670 return( PSA_ERROR_BAD_STATE );
4671 }
4672 if( operation->iv_required && ! operation->iv_set )
4673 {
4674 return( PSA_ERROR_BAD_STATE );
4675 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004676
Steven Cooremanef8575e2020-09-11 11:44:50 +02004677 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004678 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004679 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004680 input,
4681 input_length,
4682 output,
4683 output_size,
4684 output_length );
4685 goto exit;
4686 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004687
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004688 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004689 {
4690 /* Take the unprocessed partial block left over from previous
4691 * update calls, if any, plus the input to this call. Remove
4692 * the last partial block, if any. You get the data that will be
4693 * output in this call. */
4694 expected_output_size =
4695 ( operation->ctx.cipher.unprocessed_len + input_length )
4696 / operation->block_size * operation->block_size;
4697 }
4698 else
4699 {
4700 expected_output_size = input_length;
4701 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004702
Gilles Peskine89d789c2018-06-04 17:17:16 +02004703 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004704 {
4705 status = PSA_ERROR_BUFFER_TOO_SMALL;
4706 goto exit;
4707 }
mohammad160382759612018-03-12 18:16:40 +02004708
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004709 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4710 {
4711 /* mbedtls_cipher_update has an API inconsistency: it will only
4712 * process a single block at a time in ECB mode. Abstract away that
4713 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004714 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4715 input,
4716 input_length,
4717 output,
4718 output_size,
4719 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004720 }
4721 else
4722 {
4723 status = mbedtls_to_psa_error(
4724 mbedtls_cipher_update( &operation->ctx.cipher, input,
4725 input_length, output, output_length ) );
4726 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004727exit:
4728 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004729 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004730 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004731}
4732
Gilles Peskinee553c652018-06-04 16:22:46 +02004733psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4734 uint8_t *output,
4735 size_t output_size,
4736 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004737{
David Saadab4ecc272019-02-14 13:48:10 +02004738 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004739 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004740 if( operation->alg == 0 )
4741 {
4742 return( PSA_ERROR_BAD_STATE );
4743 }
4744 if( operation->iv_required && ! operation->iv_set )
4745 {
4746 return( PSA_ERROR_BAD_STATE );
4747 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004748
Steven Cooremanef8575e2020-09-11 11:44:50 +02004749 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004750 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004751 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004752 output,
4753 output_size,
4754 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004755 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004756 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004757
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004758 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004759 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004760 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004761 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004762 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004763 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004764 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004765 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004766 }
4767
Steven Cooremanef8575e2020-09-11 11:44:50 +02004768 status = mbedtls_to_psa_error(
4769 mbedtls_cipher_finish( &operation->ctx.cipher,
4770 temp_output_buffer,
4771 output_length ) );
4772 if( status != PSA_SUCCESS )
4773 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004774
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004775 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004776 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004777 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004778 memcpy( output, temp_output_buffer, *output_length );
4779 else
Janos Follath315b51c2018-07-09 16:04:51 +01004780 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004781
Steven Cooremanef8575e2020-09-11 11:44:50 +02004782exit:
4783 if( operation->mbedtls_in_use == 1 )
4784 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004785
Steven Cooremanef8575e2020-09-11 11:44:50 +02004786 if( status == PSA_SUCCESS )
4787 return( psa_cipher_abort( operation ) );
4788 else
4789 {
4790 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004791 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004792
Steven Cooremanef8575e2020-09-11 11:44:50 +02004793 return( status );
4794 }
mohammad1603503973b2018-03-12 15:59:30 +02004795}
4796
Gilles Peskinee553c652018-06-04 16:22:46 +02004797psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4798{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004799 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004800 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004801 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004802 * in use. It's ok to call abort on such an object, and there's
4803 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004804 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004805 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004806
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004807 /* Sanity check (shouldn't happen: operation->alg should
4808 * always have been initialized to a valid value). */
4809 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4810 return( PSA_ERROR_BAD_STATE );
4811
Steven Cooremanef8575e2020-09-11 11:44:50 +02004812 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004813 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004814 else
4815 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004816
Moran Peker41deec42018-04-04 15:43:05 +03004817 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004818 operation->key_set = 0;
4819 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004820 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004821 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004822 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004823 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004824
Moran Peker395db872018-05-31 14:07:14 +03004825 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004826}
4827
Gilles Peskinea0655c32018-04-30 17:06:50 +02004828
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004829
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004830
mohammad16035955c982018-04-26 00:53:03 +03004831/****************************************************************/
4832/* AEAD */
4833/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004834
Gilles Peskineedf9a652018-08-17 18:11:56 +02004835typedef struct
4836{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004837 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004838 const mbedtls_cipher_info_t *cipher_info;
4839 union
4840 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004841 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004842#if defined(MBEDTLS_CCM_C)
4843 mbedtls_ccm_context ccm;
4844#endif /* MBEDTLS_CCM_C */
4845#if defined(MBEDTLS_GCM_C)
4846 mbedtls_gcm_context gcm;
4847#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004848#if defined(MBEDTLS_CHACHAPOLY_C)
4849 mbedtls_chachapoly_context chachapoly;
4850#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004851 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004852 psa_algorithm_t core_alg;
4853 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004854 uint8_t tag_length;
4855} aead_operation_t;
4856
Ronald Cronf95a2b12020-10-22 15:24:49 +02004857#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4858
Gilles Peskine30a9e412019-01-14 18:36:12 +01004859static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004860{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004861 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004862 {
4863#if defined(MBEDTLS_CCM_C)
4864 case PSA_ALG_CCM:
4865 mbedtls_ccm_free( &operation->ctx.ccm );
4866 break;
4867#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004868#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004869 case PSA_ALG_GCM:
4870 mbedtls_gcm_free( &operation->ctx.gcm );
4871 break;
4872#endif /* MBEDTLS_GCM_C */
4873 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004874
Ronald Cron5c522922020-11-14 16:35:34 +01004875 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004876}
4877
4878static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004879 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004880 psa_key_usage_t usage,
4881 psa_algorithm_t alg )
4882{
4883 psa_status_t status;
4884 size_t key_bits;
4885 mbedtls_cipher_id_t cipher_id;
4886
Ronald Cron5c522922020-11-14 16:35:34 +01004887 status = psa_get_and_lock_transparent_key_slot_with_policy(
4888 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004889 if( status != PSA_SUCCESS )
4890 return( status );
4891
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004892 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004893
4894 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004895 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004896 &cipher_id );
4897 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004898 {
4899 status = PSA_ERROR_NOT_SUPPORTED;
4900 goto cleanup;
4901 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004902
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004903 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004904 {
4905#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004906 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4907 operation->core_alg = PSA_ALG_CCM;
4908 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004909 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4910 * The call to mbedtls_ccm_encrypt_and_tag or
4911 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004912 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004913 {
4914 status = PSA_ERROR_INVALID_ARGUMENT;
4915 goto cleanup;
4916 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004917 mbedtls_ccm_init( &operation->ctx.ccm );
4918 status = mbedtls_to_psa_error(
4919 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004920 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004921 (unsigned int) key_bits ) );
4922 if( status != 0 )
4923 goto cleanup;
4924 break;
4925#endif /* MBEDTLS_CCM_C */
4926
4927#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004928 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4929 operation->core_alg = PSA_ALG_GCM;
4930 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004931 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4932 * The call to mbedtls_gcm_crypt_and_tag or
4933 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004934 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004935 {
4936 status = PSA_ERROR_INVALID_ARGUMENT;
4937 goto cleanup;
4938 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004939 mbedtls_gcm_init( &operation->ctx.gcm );
4940 status = mbedtls_to_psa_error(
4941 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004942 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004943 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004944 if( status != 0 )
4945 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004946 break;
4947#endif /* MBEDTLS_GCM_C */
4948
Gilles Peskine26869f22019-05-06 15:25:00 +02004949#if defined(MBEDTLS_CHACHAPOLY_C)
4950 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4951 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4952 operation->full_tag_length = 16;
4953 /* We only support the default tag length. */
4954 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004955 {
4956 status = PSA_ERROR_NOT_SUPPORTED;
4957 goto cleanup;
4958 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004959 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4960 status = mbedtls_to_psa_error(
4961 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004962 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004963 if( status != 0 )
4964 goto cleanup;
4965 break;
4966#endif /* MBEDTLS_CHACHAPOLY_C */
4967
Gilles Peskineedf9a652018-08-17 18:11:56 +02004968 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004969 status = PSA_ERROR_NOT_SUPPORTED;
4970 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004971 }
4972
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004973 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4974 {
4975 status = PSA_ERROR_INVALID_ARGUMENT;
4976 goto cleanup;
4977 }
4978 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004979
Gilles Peskineedf9a652018-08-17 18:11:56 +02004980 return( PSA_SUCCESS );
4981
4982cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004983 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004984 return( status );
4985}
4986
Ronald Croncf56a0a2020-08-04 09:51:30 +02004987psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004988 psa_algorithm_t alg,
4989 const uint8_t *nonce,
4990 size_t nonce_length,
4991 const uint8_t *additional_data,
4992 size_t additional_data_length,
4993 const uint8_t *plaintext,
4994 size_t plaintext_length,
4995 uint8_t *ciphertext,
4996 size_t ciphertext_size,
4997 size_t *ciphertext_length )
4998{
mohammad16035955c982018-04-26 00:53:03 +03004999 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005000 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03005001 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005002
mohammad1603f08a5502018-06-03 15:05:47 +03005003 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005004
Ronald Croncf56a0a2020-08-04 09:51:30 +02005005 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005006 if( status != PSA_SUCCESS )
5007 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005008
Gilles Peskineedf9a652018-08-17 18:11:56 +02005009 /* For all currently supported modes, the tag is at the end of the
5010 * ciphertext. */
5011 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5012 {
5013 status = PSA_ERROR_BUFFER_TOO_SMALL;
5014 goto exit;
5015 }
5016 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005017
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005018#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005019 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005020 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005021 status = mbedtls_to_psa_error(
5022 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5023 MBEDTLS_GCM_ENCRYPT,
5024 plaintext_length,
5025 nonce, nonce_length,
5026 additional_data, additional_data_length,
5027 plaintext, ciphertext,
5028 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005029 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005030 else
5031#endif /* MBEDTLS_GCM_C */
5032#if defined(MBEDTLS_CCM_C)
5033 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005034 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005035 status = mbedtls_to_psa_error(
5036 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5037 plaintext_length,
5038 nonce, nonce_length,
5039 additional_data,
5040 additional_data_length,
5041 plaintext, ciphertext,
5042 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005043 }
mohammad16035c8845f2018-05-09 05:40:09 -07005044 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005045#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005046#if defined(MBEDTLS_CHACHAPOLY_C)
5047 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5048 {
5049 if( nonce_length != 12 || operation.tag_length != 16 )
5050 {
5051 status = PSA_ERROR_NOT_SUPPORTED;
5052 goto exit;
5053 }
5054 status = mbedtls_to_psa_error(
5055 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5056 plaintext_length,
5057 nonce,
5058 additional_data,
5059 additional_data_length,
5060 plaintext,
5061 ciphertext,
5062 tag ) );
5063 }
5064 else
5065#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005066 {
mohammad1603554faad2018-06-03 15:07:38 +03005067 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005068 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005069
Gilles Peskineedf9a652018-08-17 18:11:56 +02005070 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5071 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005072
Gilles Peskineedf9a652018-08-17 18:11:56 +02005073exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005074 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005075 if( status == PSA_SUCCESS )
5076 *ciphertext_length = plaintext_length + operation.tag_length;
5077 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005078}
5079
Gilles Peskineee652a32018-06-01 19:23:52 +02005080/* Locate the tag in a ciphertext buffer containing the encrypted data
5081 * followed by the tag. Return the length of the part preceding the tag in
5082 * *plaintext_length. This is the size of the plaintext in modes where
5083 * the encrypted data has the same size as the plaintext, such as
5084 * CCM and GCM. */
5085static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5086 const uint8_t *ciphertext,
5087 size_t ciphertext_length,
5088 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005089 const uint8_t **p_tag )
5090{
5091 size_t payload_length;
5092 if( tag_length > ciphertext_length )
5093 return( PSA_ERROR_INVALID_ARGUMENT );
5094 payload_length = ciphertext_length - tag_length;
5095 if( payload_length > plaintext_size )
5096 return( PSA_ERROR_BUFFER_TOO_SMALL );
5097 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005098 return( PSA_SUCCESS );
5099}
5100
Ronald Croncf56a0a2020-08-04 09:51:30 +02005101psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005102 psa_algorithm_t alg,
5103 const uint8_t *nonce,
5104 size_t nonce_length,
5105 const uint8_t *additional_data,
5106 size_t additional_data_length,
5107 const uint8_t *ciphertext,
5108 size_t ciphertext_length,
5109 uint8_t *plaintext,
5110 size_t plaintext_size,
5111 size_t *plaintext_length )
5112{
mohammad16035955c982018-04-26 00:53:03 +03005113 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005114 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005115 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005116
Gilles Peskineee652a32018-06-01 19:23:52 +02005117 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005118
Ronald Croncf56a0a2020-08-04 09:51:30 +02005119 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005120 if( status != PSA_SUCCESS )
5121 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005122
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005123 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5124 ciphertext, ciphertext_length,
5125 plaintext_size, &tag );
5126 if( status != PSA_SUCCESS )
5127 goto exit;
5128
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005129#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005130 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005131 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005132 status = mbedtls_to_psa_error(
5133 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5134 ciphertext_length - operation.tag_length,
5135 nonce, nonce_length,
5136 additional_data,
5137 additional_data_length,
5138 tag, operation.tag_length,
5139 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005140 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005141 else
5142#endif /* MBEDTLS_GCM_C */
5143#if defined(MBEDTLS_CCM_C)
5144 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005145 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005146 status = mbedtls_to_psa_error(
5147 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5148 ciphertext_length - operation.tag_length,
5149 nonce, nonce_length,
5150 additional_data,
5151 additional_data_length,
5152 ciphertext, plaintext,
5153 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005154 }
mohammad160339574652018-06-01 04:39:53 -07005155 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005156#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005157#if defined(MBEDTLS_CHACHAPOLY_C)
5158 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5159 {
5160 if( nonce_length != 12 || operation.tag_length != 16 )
5161 {
5162 status = PSA_ERROR_NOT_SUPPORTED;
5163 goto exit;
5164 }
5165 status = mbedtls_to_psa_error(
5166 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5167 ciphertext_length - operation.tag_length,
5168 nonce,
5169 additional_data,
5170 additional_data_length,
5171 tag,
5172 ciphertext,
5173 plaintext ) );
5174 }
5175 else
5176#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005177 {
mohammad1603554faad2018-06-03 15:07:38 +03005178 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005179 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005180
Gilles Peskineedf9a652018-08-17 18:11:56 +02005181 if( status != PSA_SUCCESS && plaintext_size != 0 )
5182 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005183
Gilles Peskineedf9a652018-08-17 18:11:56 +02005184exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005185 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005186 if( status == PSA_SUCCESS )
5187 *plaintext_length = ciphertext_length - operation.tag_length;
5188 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005189}
5190
Gilles Peskinea0655c32018-04-30 17:06:50 +02005191
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005192
Gilles Peskine20035e32018-02-03 22:44:14 +01005193/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005194/* Generators */
5195/****************************************************************/
5196
John Durkop07cc04a2020-11-16 22:08:34 -08005197#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5198 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5199 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5200#define AT_LEAST_ONE_BUILTIN_KDF
5201#endif
5202
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005203#define HKDF_STATE_INIT 0 /* no input yet */
5204#define HKDF_STATE_STARTED 1 /* got salt */
5205#define HKDF_STATE_KEYED 2 /* got key */
5206#define HKDF_STATE_OUTPUT 3 /* output started */
5207
Gilles Peskinecbe66502019-05-16 16:59:18 +02005208static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005210{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5212 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005213 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005215}
5216
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005218{
5219 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005220 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005221 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005222 {
5223 /* The object has (apparently) been initialized but it is not
5224 * in use. It's ok to call abort on such an object, and there's
5225 * nothing to do. */
5226 }
5227 else
John Durkopd0321952020-10-29 21:37:36 -07005228#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005229 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005230 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005231 mbedtls_free( operation->ctx.hkdf.info );
5232 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005233 }
John Durkop07cc04a2020-11-16 22:08:34 -08005234 else
5235#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5236#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5237 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5238 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005240 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005241 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005242 if( operation->ctx.tls12_prf.seed != NULL )
5243 {
5244 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5245 operation->ctx.tls12_prf.seed_length );
5246 mbedtls_free( operation->ctx.tls12_prf.seed );
5247 }
5248
5249 if( operation->ctx.tls12_prf.label != NULL )
5250 {
5251 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5252 operation->ctx.tls12_prf.label_length );
5253 mbedtls_free( operation->ctx.tls12_prf.label );
5254 }
5255
5256 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5257
5258 /* We leave the fields Ai and output_block to be erased safely by the
5259 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005260 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005261 else
John Durkop07cc04a2020-11-16 22:08:34 -08005262#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5263 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005264 {
5265 status = PSA_ERROR_BAD_STATE;
5266 }
Janos Follath083036a2019-06-11 10:22:26 +01005267 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005268 return( status );
5269}
5270
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005272 size_t *capacity)
5273{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005275 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005277 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005278 }
5279
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005281 return( PSA_SUCCESS );
5282}
5283
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005285 size_t capacity )
5286{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005288 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005290 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005291 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005292 return( PSA_SUCCESS );
5293}
5294
John Durkopd0321952020-10-29 21:37:36 -07005295#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005296/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005297 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005298static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005299 psa_algorithm_t hash_alg,
5300 uint8_t *output,
5301 size_t output_length )
5302{
5303 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5304 psa_status_t status;
5305
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005306 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5307 return( PSA_ERROR_BAD_STATE );
5308 hkdf->state = HKDF_STATE_OUTPUT;
5309
Gilles Peskinebef7f142018-07-12 17:22:21 +02005310 while( output_length != 0 )
5311 {
5312 /* Copy what remains of the current block */
5313 uint8_t n = hash_length - hkdf->offset_in_block;
5314 if( n > output_length )
5315 n = (uint8_t) output_length;
5316 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5317 output += n;
5318 output_length -= n;
5319 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005320 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005321 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005322 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005323 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005324 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005325 * object was corrupted or if this function is called directly
5326 * inside the library. */
5327 if( hkdf->block_number == 0xff )
5328 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005329
5330 /* We need a new block */
5331 ++hkdf->block_number;
5332 hkdf->offset_in_block = 0;
5333 status = psa_hmac_setup_internal( &hkdf->hmac,
5334 hkdf->prk, hash_length,
5335 hash_alg );
5336 if( status != PSA_SUCCESS )
5337 return( status );
5338 if( hkdf->block_number != 1 )
5339 {
5340 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5341 hkdf->output_block,
5342 hash_length );
5343 if( status != PSA_SUCCESS )
5344 return( status );
5345 }
5346 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5347 hkdf->info,
5348 hkdf->info_length );
5349 if( status != PSA_SUCCESS )
5350 return( status );
5351 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5352 &hkdf->block_number, 1 );
5353 if( status != PSA_SUCCESS )
5354 return( status );
5355 status = psa_hmac_finish_internal( &hkdf->hmac,
5356 hkdf->output_block,
5357 sizeof( hkdf->output_block ) );
5358 if( status != PSA_SUCCESS )
5359 return( status );
5360 }
5361
5362 return( PSA_SUCCESS );
5363}
John Durkop07cc04a2020-11-16 22:08:34 -08005364#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005365
John Durkop07cc04a2020-11-16 22:08:34 -08005366#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5367 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005368static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5369 psa_tls12_prf_key_derivation_t *tls12_prf,
5370 psa_algorithm_t alg )
5371{
5372 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5373 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005374 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5375 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005376
Janos Follath7742fee2019-06-17 12:58:10 +01005377 /* We can't be wanting more output after block 0xff, otherwise
5378 * the capacity check in psa_key_derivation_output_bytes() would have
5379 * prevented this call. It could happen only if the operation
5380 * object was corrupted or if this function is called directly
5381 * inside the library. */
5382 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005383 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005384
5385 /* We need a new block */
5386 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005387 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005388
5389 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5390 *
5391 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5392 *
5393 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5394 * HMAC_hash(secret, A(2) + seed) +
5395 * HMAC_hash(secret, A(3) + seed) + ...
5396 *
5397 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005398 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005399 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005400 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005401 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005402 * is currently extracted as `output_block` and where i is
5403 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005404 */
5405
Janos Follathea29bfb2019-06-19 12:21:20 +01005406 /* Save the hash context before using it, to preserve the hash state with
5407 * only the inner padding in it. We need this, because inner padding depends
5408 * on the key (secret in the RFC's terminology). */
5409 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5410 if( status != PSA_SUCCESS )
5411 goto cleanup;
5412
5413 /* Calculate A(i) where i = tls12_prf->block_number. */
5414 if( tls12_prf->block_number == 1 )
5415 {
5416 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5417 * the variable seed and in this instance means it in the context of the
5418 * P_hash function, where seed = label + seed.) */
5419 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5420 tls12_prf->label, tls12_prf->label_length );
5421 if( status != PSA_SUCCESS )
5422 goto cleanup;
5423 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5424 tls12_prf->seed, tls12_prf->seed_length );
5425 if( status != PSA_SUCCESS )
5426 goto cleanup;
5427 }
5428 else
5429 {
5430 /* A(i) = HMAC_hash(secret, A(i-1)) */
5431 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5432 tls12_prf->Ai, hash_length );
5433 if( status != PSA_SUCCESS )
5434 goto cleanup;
5435 }
5436
5437 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5438 tls12_prf->Ai, hash_length );
5439 if( status != PSA_SUCCESS )
5440 goto cleanup;
5441 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5442 if( status != PSA_SUCCESS )
5443 goto cleanup;
5444
5445 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5446 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5447 tls12_prf->Ai, hash_length );
5448 if( status != PSA_SUCCESS )
5449 goto cleanup;
5450 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5451 tls12_prf->label, tls12_prf->label_length );
5452 if( status != PSA_SUCCESS )
5453 goto cleanup;
5454 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5455 tls12_prf->seed, tls12_prf->seed_length );
5456 if( status != PSA_SUCCESS )
5457 goto cleanup;
5458 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5459 tls12_prf->output_block, hash_length );
5460 if( status != PSA_SUCCESS )
5461 goto cleanup;
5462 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5463 if( status != PSA_SUCCESS )
5464 goto cleanup;
5465
Janos Follath7742fee2019-06-17 12:58:10 +01005466
5467cleanup:
5468
Janos Follathea29bfb2019-06-19 12:21:20 +01005469 cleanup_status = psa_hash_abort( &backup );
5470 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5471 status = cleanup_status;
5472
5473 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005474}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005475
Janos Follath844eb0e2019-06-19 12:10:49 +01005476static psa_status_t psa_key_derivation_tls12_prf_read(
5477 psa_tls12_prf_key_derivation_t *tls12_prf,
5478 psa_algorithm_t alg,
5479 uint8_t *output,
5480 size_t output_length )
5481{
5482 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5483 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5484 psa_status_t status;
5485 uint8_t offset, length;
5486
5487 while( output_length != 0 )
5488 {
5489 /* Check if we have fully processed the current block. */
5490 if( tls12_prf->left_in_block == 0 )
5491 {
5492 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5493 alg );
5494 if( status != PSA_SUCCESS )
5495 return( status );
5496
5497 continue;
5498 }
5499
5500 if( tls12_prf->left_in_block > output_length )
5501 length = (uint8_t) output_length;
5502 else
5503 length = tls12_prf->left_in_block;
5504
5505 offset = hash_length - tls12_prf->left_in_block;
5506 memcpy( output, tls12_prf->output_block + offset, length );
5507 output += length;
5508 output_length -= length;
5509 tls12_prf->left_in_block -= length;
5510 }
5511
5512 return( PSA_SUCCESS );
5513}
John Durkop07cc04a2020-11-16 22:08:34 -08005514#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5515 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005516
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005517psa_status_t psa_key_derivation_output_bytes(
5518 psa_key_derivation_operation_t *operation,
5519 uint8_t *output,
5520 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005521{
5522 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005524
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005526 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005527 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005528 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005529 }
5530
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005531 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005532 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005534 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005535 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005536 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005537 goto exit;
5538 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005539 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005540 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005541 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005542 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005543 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5544 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005545 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005546 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005547 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005548 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005549 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005550
John Durkopd0321952020-10-29 21:37:36 -07005551#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005552 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005553 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005554 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005555 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005556 output, output_length );
5557 }
Janos Follathadbec812019-06-14 11:05:39 +01005558 else
John Durkop07cc04a2020-11-16 22:08:34 -08005559#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5560#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5561 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005562 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005563 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005564 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005565 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005566 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005567 output_length );
5568 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005569 else
John Durkop07cc04a2020-11-16 22:08:34 -08005570#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5571 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005572 {
5573 return( PSA_ERROR_BAD_STATE );
5574 }
5575
5576exit:
5577 if( status != PSA_SUCCESS )
5578 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005579 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005580 * This allows us to differentiate between exhausted operations and
5581 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5582 * operations. */
5583 psa_algorithm_t alg = operation->alg;
5584 psa_key_derivation_abort( operation );
5585 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005586 memset( output, '!', output_length );
5587 }
5588 return( status );
5589}
5590
Gilles Peskine08542d82018-07-19 17:05:42 +02005591#if defined(MBEDTLS_DES_C)
5592static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5593{
5594 if( data_size >= 8 )
5595 mbedtls_des_key_set_parity( data );
5596 if( data_size >= 16 )
5597 mbedtls_des_key_set_parity( data + 8 );
5598 if( data_size >= 24 )
5599 mbedtls_des_key_set_parity( data + 16 );
5600}
5601#endif /* MBEDTLS_DES_C */
5602
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005603static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005604 psa_key_slot_t *slot,
5605 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005606 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005607{
5608 uint8_t *data = NULL;
5609 size_t bytes = PSA_BITS_TO_BYTES( bits );
5610 psa_status_t status;
5611
Gilles Peskine8e338702019-07-30 20:06:31 +02005612 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005613 return( PSA_ERROR_INVALID_ARGUMENT );
5614 if( bits % 8 != 0 )
5615 return( PSA_ERROR_INVALID_ARGUMENT );
5616 data = mbedtls_calloc( 1, bytes );
5617 if( data == NULL )
5618 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5619
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005620 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005621 if( status != PSA_SUCCESS )
5622 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005623#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005624 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005625 psa_des_set_key_parity( data, bytes );
5626#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005627 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005628
5629exit:
5630 mbedtls_free( data );
5631 return( status );
5632}
5633
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005634psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005635 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005636 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005637{
5638 psa_status_t status;
5639 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005640 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005641
Ronald Cron81709fc2020-11-14 12:10:32 +01005642 *key = MBEDTLS_SVC_KEY_ID_INIT;
5643
Gilles Peskine0f84d622019-09-12 19:03:13 +02005644 /* Reject any attempt to create a zero-length key so that we don't
5645 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5646 if( psa_get_key_bits( attributes ) == 0 )
5647 return( PSA_ERROR_INVALID_ARGUMENT );
5648
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005649 if( ! operation->can_output_key )
5650 return( PSA_ERROR_NOT_PERMITTED );
5651
Ronald Cron81709fc2020-11-14 12:10:32 +01005652 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5653 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005654#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5655 if( driver != NULL )
5656 {
5657 /* Deriving a key in a secure element is not implemented yet. */
5658 status = PSA_ERROR_NOT_SUPPORTED;
5659 }
5660#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005661 if( status == PSA_SUCCESS )
5662 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005663 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005664 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005665 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005666 }
5667 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005668 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005669 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005670 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005671
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005672 return( status );
5673}
5674
Gilles Peskineeab56e42018-07-12 17:12:33 +02005675
5676
5677/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005678/* Key derivation */
5679/****************************************************************/
5680
John Durkop07cc04a2020-11-16 22:08:34 -08005681#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005682static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005683 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005684 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005685{
John Durkop07cc04a2020-11-16 22:08:34 -08005686 int is_kdf_alg_supported;
5687
Janos Follath5fe19732019-06-20 15:09:30 +01005688 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5689 * initialisers for this union leave some bytes unspecified.) */
5690 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5691
Gilles Peskine969c5d62019-01-16 15:53:06 +01005692 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005693#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005694 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5695 is_kdf_alg_supported = 1;
5696 else
5697#endif
5698#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5699 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5700 is_kdf_alg_supported = 1;
5701 else
5702#endif
5703#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5704 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5705 is_kdf_alg_supported = 1;
5706 else
5707#endif
5708 is_kdf_alg_supported = 0;
5709
5710 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005711 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005712 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005713 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5714 if( hash_size == 0 )
5715 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005716 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5717 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005718 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005719 {
5720 return( PSA_ERROR_NOT_SUPPORTED );
5721 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005722 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005723 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005724 }
John Durkop07cc04a2020-11-16 22:08:34 -08005725
5726 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005727}
John Durkop07cc04a2020-11-16 22:08:34 -08005728#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005729
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005731 psa_algorithm_t alg )
5732{
5733 psa_status_t status;
5734
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005735 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005736 return( PSA_ERROR_BAD_STATE );
5737
Gilles Peskine6843c292019-01-18 16:44:49 +01005738 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5739 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005740#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005741 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005742 {
5743 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005744 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005745 }
5746 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5747 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005748 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005749 }
John Durkop07cc04a2020-11-16 22:08:34 -08005750#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005751 else
5752 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005753
5754 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005756 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005757}
5758
John Durkopd0321952020-10-29 21:37:36 -07005759#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005760static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005761 psa_algorithm_t hash_alg,
5762 psa_key_derivation_step_t step,
5763 const uint8_t *data,
5764 size_t data_length )
5765{
5766 psa_status_t status;
5767 switch( step )
5768 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005769 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005770 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005771 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005772 status = psa_hmac_setup_internal( &hkdf->hmac,
5773 data, data_length,
5774 hash_alg );
5775 if( status != PSA_SUCCESS )
5776 return( status );
5777 hkdf->state = HKDF_STATE_STARTED;
5778 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005779 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005780 /* If no salt was provided, use an empty salt. */
5781 if( hkdf->state == HKDF_STATE_INIT )
5782 {
5783 status = psa_hmac_setup_internal( &hkdf->hmac,
5784 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005785 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005786 if( status != PSA_SUCCESS )
5787 return( status );
5788 hkdf->state = HKDF_STATE_STARTED;
5789 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005790 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005791 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005792 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5793 data, data_length );
5794 if( status != PSA_SUCCESS )
5795 return( status );
5796 status = psa_hmac_finish_internal( &hkdf->hmac,
5797 hkdf->prk,
5798 sizeof( hkdf->prk ) );
5799 if( status != PSA_SUCCESS )
5800 return( status );
5801 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5802 hkdf->block_number = 0;
5803 hkdf->state = HKDF_STATE_KEYED;
5804 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005805 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005806 if( hkdf->state == HKDF_STATE_OUTPUT )
5807 return( PSA_ERROR_BAD_STATE );
5808 if( hkdf->info_set )
5809 return( PSA_ERROR_BAD_STATE );
5810 hkdf->info_length = data_length;
5811 if( data_length != 0 )
5812 {
5813 hkdf->info = mbedtls_calloc( 1, data_length );
5814 if( hkdf->info == NULL )
5815 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5816 memcpy( hkdf->info, data, data_length );
5817 }
5818 hkdf->info_set = 1;
5819 return( PSA_SUCCESS );
5820 default:
5821 return( PSA_ERROR_INVALID_ARGUMENT );
5822 }
5823}
John Durkop07cc04a2020-11-16 22:08:34 -08005824#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005825
John Durkop07cc04a2020-11-16 22:08:34 -08005826#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5827 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005828static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5829 const uint8_t *data,
5830 size_t data_length )
5831{
5832 if( prf->state != TLS12_PRF_STATE_INIT )
5833 return( PSA_ERROR_BAD_STATE );
5834
Janos Follathd6dce9f2019-07-04 09:11:38 +01005835 if( data_length != 0 )
5836 {
5837 prf->seed = mbedtls_calloc( 1, data_length );
5838 if( prf->seed == NULL )
5839 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005840
Janos Follathd6dce9f2019-07-04 09:11:38 +01005841 memcpy( prf->seed, data, data_length );
5842 prf->seed_length = data_length;
5843 }
Janos Follathf08e2652019-06-13 09:05:41 +01005844
5845 prf->state = TLS12_PRF_STATE_SEED_SET;
5846
5847 return( PSA_SUCCESS );
5848}
5849
Janos Follath81550542019-06-13 14:26:34 +01005850static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5851 psa_algorithm_t hash_alg,
5852 const uint8_t *data,
5853 size_t data_length )
5854{
5855 psa_status_t status;
5856 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5857 return( PSA_ERROR_BAD_STATE );
5858
5859 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5860 if( status != PSA_SUCCESS )
5861 return( status );
5862
5863 prf->state = TLS12_PRF_STATE_KEY_SET;
5864
5865 return( PSA_SUCCESS );
5866}
5867
Janos Follath63028dd2019-06-13 09:15:47 +01005868static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5869 const uint8_t *data,
5870 size_t data_length )
5871{
5872 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5873 return( PSA_ERROR_BAD_STATE );
5874
Janos Follathd6dce9f2019-07-04 09:11:38 +01005875 if( data_length != 0 )
5876 {
5877 prf->label = mbedtls_calloc( 1, data_length );
5878 if( prf->label == NULL )
5879 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005880
Janos Follathd6dce9f2019-07-04 09:11:38 +01005881 memcpy( prf->label, data, data_length );
5882 prf->label_length = data_length;
5883 }
Janos Follath63028dd2019-06-13 09:15:47 +01005884
5885 prf->state = TLS12_PRF_STATE_LABEL_SET;
5886
5887 return( PSA_SUCCESS );
5888}
5889
Janos Follathb03233e2019-06-11 15:30:30 +01005890static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5891 psa_algorithm_t hash_alg,
5892 psa_key_derivation_step_t step,
5893 const uint8_t *data,
5894 size_t data_length )
5895{
Janos Follathb03233e2019-06-11 15:30:30 +01005896 switch( step )
5897 {
Janos Follathf08e2652019-06-13 09:05:41 +01005898 case PSA_KEY_DERIVATION_INPUT_SEED:
5899 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005900 case PSA_KEY_DERIVATION_INPUT_SECRET:
5901 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005902 case PSA_KEY_DERIVATION_INPUT_LABEL:
5903 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005904 default:
5905 return( PSA_ERROR_INVALID_ARGUMENT );
5906 }
5907}
John Durkop07cc04a2020-11-16 22:08:34 -08005908#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5909 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5910
5911#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5912static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5913 psa_tls12_prf_key_derivation_t *prf,
5914 psa_algorithm_t hash_alg,
5915 const uint8_t *data,
5916 size_t data_length )
5917{
5918 psa_status_t status;
5919 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5920 uint8_t *cur = pms;
5921
5922 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5923 return( PSA_ERROR_INVALID_ARGUMENT );
5924
5925 /* Quoting RFC 4279, Section 2:
5926 *
5927 * The premaster secret is formed as follows: if the PSK is N octets
5928 * long, concatenate a uint16 with the value N, N zero octets, a second
5929 * uint16 with the value N, and the PSK itself.
5930 */
5931
5932 *cur++ = ( data_length >> 8 ) & 0xff;
5933 *cur++ = ( data_length >> 0 ) & 0xff;
5934 memset( cur, 0, data_length );
5935 cur += data_length;
5936 *cur++ = pms[0];
5937 *cur++ = pms[1];
5938 memcpy( cur, data, data_length );
5939 cur += data_length;
5940
5941 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5942
5943 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5944 return( status );
5945}
Janos Follath6660f0e2019-06-17 08:44:03 +01005946
5947static psa_status_t psa_tls12_prf_psk_to_ms_input(
5948 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{
5954 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005955 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005956 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5957 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005958 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005959
5960 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5961}
John Durkop07cc04a2020-11-16 22:08:34 -08005962#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005963
Gilles Peskineb8965192019-09-24 16:21:10 +02005964/** Check whether the given key type is acceptable for the given
5965 * input step of a key derivation.
5966 *
5967 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5968 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5969 * Both secret and non-secret inputs can alternatively have the type
5970 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5971 * that the input was passed as a buffer rather than via a key object.
5972 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005973static int psa_key_derivation_check_input_type(
5974 psa_key_derivation_step_t step,
5975 psa_key_type_t key_type )
5976{
5977 switch( step )
5978 {
5979 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005980 if( key_type == PSA_KEY_TYPE_DERIVE )
5981 return( PSA_SUCCESS );
5982 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005983 return( PSA_SUCCESS );
5984 break;
5985 case PSA_KEY_DERIVATION_INPUT_LABEL:
5986 case PSA_KEY_DERIVATION_INPUT_SALT:
5987 case PSA_KEY_DERIVATION_INPUT_INFO:
5988 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005989 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5990 return( PSA_SUCCESS );
5991 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005992 return( PSA_SUCCESS );
5993 break;
5994 }
5995 return( PSA_ERROR_INVALID_ARGUMENT );
5996}
5997
Janos Follathb80a94e2019-06-12 15:54:46 +01005998static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005999 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006000 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006001 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006002 const uint8_t *data,
6003 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006004{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006005 psa_status_t status;
6006 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6007
6008 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006009 if( status != PSA_SUCCESS )
6010 goto exit;
6011
John Durkopd0321952020-10-29 21:37:36 -07006012#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006013 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006014 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006015 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006016 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006017 step, data, data_length );
6018 }
John Durkop07cc04a2020-11-16 22:08:34 -08006019 else
6020#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6021#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6022 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006023 {
Janos Follathb03233e2019-06-11 15:30:30 +01006024 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6025 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6026 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006027 }
John Durkop07cc04a2020-11-16 22:08:34 -08006028 else
6029#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6030#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6031 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006032 {
6033 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6034 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6035 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006036 }
6037 else
John Durkop07cc04a2020-11-16 22:08:34 -08006038#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006039 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006040 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006041 return( PSA_ERROR_BAD_STATE );
6042 }
6043
Gilles Peskine224b0d62019-09-23 18:13:17 +02006044exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006045 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006046 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006047 return( status );
6048}
6049
Janos Follath51f4a0f2019-06-14 11:35:55 +01006050psa_status_t psa_key_derivation_input_bytes(
6051 psa_key_derivation_operation_t *operation,
6052 psa_key_derivation_step_t step,
6053 const uint8_t *data,
6054 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006055{
Gilles Peskineb8965192019-09-24 16:21:10 +02006056 return( psa_key_derivation_input_internal( operation, step,
6057 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006058 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006059}
6060
Janos Follath51f4a0f2019-06-14 11:35:55 +01006061psa_status_t psa_key_derivation_input_key(
6062 psa_key_derivation_operation_t *operation,
6063 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006064 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006065{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006066 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006067 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006068 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006069
Ronald Cron5c522922020-11-14 16:35:34 +01006070 status = psa_get_and_lock_transparent_key_slot_with_policy(
6071 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006072 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006073 {
6074 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006075 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006076 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006077
6078 /* Passing a key object as a SECRET input unlocks the permission
6079 * to output to a key object. */
6080 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6081 operation->can_output_key = 1;
6082
Ronald Cronf95a2b12020-10-22 15:24:49 +02006083 status = psa_key_derivation_input_internal( operation,
6084 step, slot->attr.type,
6085 slot->data.key.data,
6086 slot->data.key.bytes );
6087
Ronald Cron5c522922020-11-14 16:35:34 +01006088 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006089
Ronald Cron5c522922020-11-14 16:35:34 +01006090 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006091}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006092
6093
6094
6095/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006096/* Key agreement */
6097/****************************************************************/
6098
John Durkop6ba40d12020-11-10 08:50:04 -08006099#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006100static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6101 size_t peer_key_length,
6102 const mbedtls_ecp_keypair *our_key,
6103 uint8_t *shared_secret,
6104 size_t shared_secret_size,
6105 size_t *shared_secret_length )
6106{
Steven Cooremand4867872020-08-05 16:31:39 +02006107 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006108 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006109 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006110 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006111 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006112 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006113
Steven Cooreman4fed4552020-08-03 14:46:03 +02006114 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6115 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006116 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006117 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006118 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006119 goto exit;
6120
Jaeden Amero97271b32019-01-10 19:38:51 +00006121 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006122 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006123 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006124 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006125 status = mbedtls_to_psa_error(
6126 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6127 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006128 goto exit;
6129
Jaeden Amero97271b32019-01-10 19:38:51 +00006130 status = mbedtls_to_psa_error(
6131 mbedtls_ecdh_calc_secret( &ecdh,
6132 shared_secret_length,
6133 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006134 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006135 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006136 if( status != PSA_SUCCESS )
6137 goto exit;
6138 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6139 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006140
6141exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006142 if( status != PSA_SUCCESS )
6143 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006144 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006145 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006146 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006147
Jaeden Amero97271b32019-01-10 19:38:51 +00006148 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006149}
John Durkop6ba40d12020-11-10 08:50:04 -08006150#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006151
Gilles Peskine01d718c2018-09-18 12:01:02 +02006152#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6153
Gilles Peskine0216fe12019-04-11 21:23:21 +02006154static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6155 psa_key_slot_t *private_key,
6156 const uint8_t *peer_key,
6157 size_t peer_key_length,
6158 uint8_t *shared_secret,
6159 size_t shared_secret_size,
6160 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006161{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006162 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006163 {
John Durkop6ba40d12020-11-10 08:50:04 -08006164#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006165 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006166 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006167 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006168 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006169 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006170 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006171 private_key->data.key.data,
6172 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006173 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006174 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006175 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006176 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006177 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006178 shared_secret, shared_secret_size,
6179 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006180 mbedtls_ecp_keypair_free( ecp );
6181 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006182 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006183#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006184 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006185 (void) private_key;
6186 (void) peer_key;
6187 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006188 (void) shared_secret;
6189 (void) shared_secret_size;
6190 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006191 return( PSA_ERROR_NOT_SUPPORTED );
6192 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006193}
6194
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006195/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006196 * to potentially free embedded data structures and wipe confidential data.
6197 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006198static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006199 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006200 psa_key_slot_t *private_key,
6201 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006202 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006203{
6204 psa_status_t status;
6205 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6206 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006207 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006208
6209 /* Step 1: run the secret agreement algorithm to generate the shared
6210 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006211 status = psa_key_agreement_raw_internal( ka_alg,
6212 private_key,
6213 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006214 shared_secret,
6215 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006216 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006217 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006218 goto exit;
6219
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006220 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006221 * the shared secret. A shared secret is permitted wherever a key
6222 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006223 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006224 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006225 shared_secret,
6226 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006227exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006228 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006229 return( status );
6230}
6231
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006232psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006233 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006234 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006235 const uint8_t *peer_key,
6236 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006237{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006238 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006239 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006240 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006241
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006242 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006243 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006244 status = psa_get_and_lock_transparent_key_slot_with_policy(
6245 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006246 if( status != PSA_SUCCESS )
6247 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006248 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006249 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006250 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006251 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006252 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006253 else
6254 {
6255 /* If a private key has been added as SECRET, we allow the derived
6256 * key material to be used as a key in PSA Crypto. */
6257 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6258 operation->can_output_key = 1;
6259 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006260
Ronald Cron5c522922020-11-14 16:35:34 +01006261 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006262
Ronald Cron5c522922020-11-14 16:35:34 +01006263 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006264}
6265
Gilles Peskinebe697d82019-05-16 18:00:41 +02006266psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006267 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006268 const uint8_t *peer_key,
6269 size_t peer_key_length,
6270 uint8_t *output,
6271 size_t output_size,
6272 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006273{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006274 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006275 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006276 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006277
6278 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6279 {
6280 status = PSA_ERROR_INVALID_ARGUMENT;
6281 goto exit;
6282 }
Ronald Cron5c522922020-11-14 16:35:34 +01006283 status = psa_get_and_lock_transparent_key_slot_with_policy(
6284 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006285 if( status != PSA_SUCCESS )
6286 goto exit;
6287
6288 status = psa_key_agreement_raw_internal( alg, slot,
6289 peer_key, peer_key_length,
6290 output, output_size,
6291 output_length );
6292
6293exit:
6294 if( status != PSA_SUCCESS )
6295 {
6296 /* If an error happens and is not handled properly, the output
6297 * may be used as a key to protect sensitive data. Arrange for such
6298 * a key to be random, which is likely to result in decryption or
6299 * verification errors. This is better than filling the buffer with
6300 * some constant data such as zeros, which would result in the data
6301 * being protected with a reproducible, easily knowable key.
6302 */
6303 psa_generate_random( output, output_size );
6304 *output_length = output_size;
6305 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006306
Ronald Cron5c522922020-11-14 16:35:34 +01006307 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006308
Ronald Cron5c522922020-11-14 16:35:34 +01006309 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006310}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006311
6312
Gilles Peskine30524eb2020-11-13 17:02:26 +01006313
Gilles Peskine86a440b2018-11-12 18:39:40 +01006314/****************************************************************/
6315/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006316/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006317
Gilles Peskine30524eb2020-11-13 17:02:26 +01006318/** Initialize the PSA random generator.
6319 */
6320static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6321{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006322#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6323 memset( rng, 0, sizeof( *rng ) );
6324#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6325
Gilles Peskine30524eb2020-11-13 17:02:26 +01006326 /* Set default configuration if
6327 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6328 if( rng->entropy_init == NULL )
6329 rng->entropy_init = mbedtls_entropy_init;
6330 if( rng->entropy_free == NULL )
6331 rng->entropy_free = mbedtls_entropy_free;
6332
6333 rng->entropy_init( &rng->entropy );
6334#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6335 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6336 /* The PSA entropy injection feature depends on using NV seed as an entropy
6337 * source. Add NV seed as an entropy source for PSA entropy injection. */
6338 mbedtls_entropy_add_source( &rng->entropy,
6339 mbedtls_nv_seed_poll, NULL,
6340 MBEDTLS_ENTROPY_BLOCK_SIZE,
6341 MBEDTLS_ENTROPY_SOURCE_STRONG );
6342#endif
6343
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006344 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006345#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006346}
6347
6348/** Deinitialize the PSA random generator.
6349 */
6350static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6351{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006352#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6353 memset( rng, 0, sizeof( *rng ) );
6354#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006355 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006356 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006357#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006358}
6359
6360/** Seed the PSA random generator.
6361 */
6362static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6363{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006364#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6365 /* Do nothing: the external RNG seeds itself. */
6366 (void) rng;
6367 return( PSA_SUCCESS );
6368#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006369 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006370 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6371 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006372 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006373#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006374}
6375
Gilles Peskinee59236f2018-01-27 23:32:46 +01006376psa_status_t psa_generate_random( uint8_t *output,
6377 size_t output_size )
6378{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006379 GUARD_MODULE_INITIALIZED;
6380
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006381#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6382
6383 size_t output_length = 0;
6384 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6385 output, output_size,
6386 &output_length );
6387 if( status != PSA_SUCCESS )
6388 return( status );
6389 /* Breaking up a request into smaller chunks is currently not supported
6390 * for the extrernal RNG interface. */
6391 if( output_length != output_size )
6392 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6393 return( PSA_SUCCESS );
6394
6395#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6396
6397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6398
Gilles Peskine30524eb2020-11-13 17:02:26 +01006399 while( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006400 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006401 ret = mbedtls_psa_get_random(
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006402 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006403 output, MBEDTLS_PSA_RANDOM_MAX_REQUEST );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006404 if( ret != 0 )
6405 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006406 output += MBEDTLS_PSA_RANDOM_MAX_REQUEST;
6407 output_size -= MBEDTLS_PSA_RANDOM_MAX_REQUEST;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006408 }
6409
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006410 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006411 output, output_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006412 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006413#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006414}
6415
Gilles Peskine8814fc42020-12-14 15:33:44 +01006416/* Wrapper function allowing the classic API to use the PSA RNG.
6417 * In the non-external case, mbedtls_psa_get_random is defined
Gilles Peskineb2b64d32020-12-14 16:43:58 +01006418 * as a constant function pointer in psa_crypto_random_impl.h.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006419 */
6420#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6421int mbedtls_psa_get_random( void *p_rng,
6422 unsigned char *output,
6423 size_t output_size )
6424{
6425 (void) p_rng;
6426 psa_status_t status = psa_generate_random( output, output_size );
6427 if( status == PSA_SUCCESS )
6428 return( 0 );
6429 else
6430 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6431}
6432#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6433
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006434#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6435#include "mbedtls/entropy_poll.h"
6436
Gilles Peskine7228da22019-07-15 11:06:15 +02006437psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006438 size_t seed_size )
6439{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006440 if( global_data.initialized )
6441 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006442
6443 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6444 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6445 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6446 return( PSA_ERROR_INVALID_ARGUMENT );
6447
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006448 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006449}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006450#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006451
John Durkop07cc04a2020-11-16 22:08:34 -08006452#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006453static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6454 size_t domain_parameters_size,
6455 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006456{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006457 size_t i;
6458 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006459
Gilles Peskinee56e8782019-04-26 17:34:02 +02006460 if( domain_parameters_size == 0 )
6461 {
6462 *exponent = 65537;
6463 return( PSA_SUCCESS );
6464 }
6465
6466 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6467 * support values that fit in a 32-bit integer, which is larger than
6468 * int on just about every platform anyway. */
6469 if( domain_parameters_size > sizeof( acc ) )
6470 return( PSA_ERROR_NOT_SUPPORTED );
6471 for( i = 0; i < domain_parameters_size; i++ )
6472 acc = ( acc << 8 ) | domain_parameters[i];
6473 if( acc > INT_MAX )
6474 return( PSA_ERROR_NOT_SUPPORTED );
6475 *exponent = acc;
6476 return( PSA_SUCCESS );
6477}
John Durkop07cc04a2020-11-16 22:08:34 -08006478#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006479
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006480static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006481 psa_key_slot_t *slot, size_t bits,
6482 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006483{
Gilles Peskine8e338702019-07-30 20:06:31 +02006484 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006485
Gilles Peskinee56e8782019-04-26 17:34:02 +02006486 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006487 return( PSA_ERROR_INVALID_ARGUMENT );
6488
Gilles Peskinee59236f2018-01-27 23:32:46 +01006489 if( key_type_is_raw_bytes( type ) )
6490 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006491 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006492
6493 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006494 if( status != PSA_SUCCESS )
6495 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006496
6497 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006498 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6499 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006500 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006501
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006502 status = psa_generate_random( slot->data.key.data,
6503 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006504 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006505 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006506
6507 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006508#if defined(MBEDTLS_DES_C)
6509 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006510 psa_des_set_key_parity( slot->data.key.data,
6511 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006512#endif /* MBEDTLS_DES_C */
6513 }
6514 else
6515
John Durkop07cc04a2020-11-16 22:08:34 -08006516#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006517 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006518 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006519 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006521 int exponent;
6522 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006523 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6524 return( PSA_ERROR_NOT_SUPPORTED );
6525 /* Accept only byte-aligned keys, for the same reasons as
6526 * in psa_import_rsa_key(). */
6527 if( bits % 8 != 0 )
6528 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006529 status = psa_read_rsa_exponent( domain_parameters,
6530 domain_parameters_size,
6531 &exponent );
6532 if( status != PSA_SUCCESS )
6533 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006534 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6535 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006536 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006537 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006538 (unsigned int) bits,
6539 exponent );
6540 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006541 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006542
6543 /* Make sure to always have an export representation available */
6544 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6545
Steven Cooreman75b74362020-07-28 14:30:13 +02006546 status = psa_allocate_buffer_to_slot( slot, bytes );
6547 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006548 {
6549 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006550 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006551 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006552
6553 status = psa_export_rsa_key( type,
6554 &rsa,
6555 slot->data.key.data,
6556 bytes,
6557 &slot->data.key.bytes );
6558 mbedtls_rsa_free( &rsa );
6559 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006560 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006561 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006562 }
6563 else
John Durkop07cc04a2020-11-16 22:08:34 -08006564#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006565
John Durkop9814fa22020-11-04 12:28:15 -08006566#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006567 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006568 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006569 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006570 mbedtls_ecp_group_id grp_id =
6571 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006572 const mbedtls_ecp_curve_info *curve_info =
6573 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006574 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006576 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006577 return( PSA_ERROR_NOT_SUPPORTED );
6578 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6579 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006580 mbedtls_ecp_keypair_init( &ecp );
6581 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006582 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006583 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006584 if( ret != 0 )
6585 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006586 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006587 return( mbedtls_to_psa_error( ret ) );
6588 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006589
6590
6591 /* Make sure to always have an export representation available */
6592 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006593 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6594 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006595 {
6596 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006597 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006598 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006599
6600 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006601 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6602
6603 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006604 if( status != PSA_SUCCESS ) {
6605 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006606 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006607 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006608 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006609 }
6610 else
John Durkop9814fa22020-11-04 12:28:15 -08006611#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006612 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006613 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006614 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006615
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006616 return( PSA_SUCCESS );
6617}
Darryl Green0c6575a2018-11-07 16:05:30 +00006618
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006619psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006620 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006621{
6622 psa_status_t status;
6623 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006624 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006625
Ronald Cron81709fc2020-11-14 12:10:32 +01006626 *key = MBEDTLS_SVC_KEY_ID_INIT;
6627
Gilles Peskine0f84d622019-09-12 19:03:13 +02006628 /* Reject any attempt to create a zero-length key so that we don't
6629 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6630 if( psa_get_key_bits( attributes ) == 0 )
6631 return( PSA_ERROR_INVALID_ARGUMENT );
6632
Ronald Cron81709fc2020-11-14 12:10:32 +01006633 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6634 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006635 if( status != PSA_SUCCESS )
6636 goto exit;
6637
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006638 status = psa_driver_wrapper_generate_key( attributes,
6639 slot );
6640 if( status != PSA_ERROR_NOT_SUPPORTED ||
6641 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6642 goto exit;
6643
6644 status = psa_generate_key_internal(
6645 slot, attributes->core.bits,
6646 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006647
6648exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006649 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006650 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006651 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006652 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006653
Darryl Green0c6575a2018-11-07 16:05:30 +00006654 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006655}
6656
6657
Gilles Peskinee59236f2018-01-27 23:32:46 +01006658
6659/****************************************************************/
6660/* Module setup */
6661/****************************************************************/
6662
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006663#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006664psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6665 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6666 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6667{
6668 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6669 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006670 global_data.rng.entropy_init = entropy_init;
6671 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006672 return( PSA_SUCCESS );
6673}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006674#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006675
Gilles Peskinee59236f2018-01-27 23:32:46 +01006676void mbedtls_psa_crypto_free( void )
6677{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006678 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006679 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6680 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006681 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006682 }
6683 /* Wipe all remaining data, including configuration.
6684 * In particular, this sets all state indicator to the value
6685 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006686 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006688 /* Unregister all secure element drivers, so that we restart from
6689 * a pristine state. */
6690 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006691#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006692}
6693
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006694#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6695/** Recover a transaction that was interrupted by a power failure.
6696 *
6697 * This function is called during initialization, before psa_crypto_init()
6698 * returns. If this function returns a failure status, the initialization
6699 * fails.
6700 */
6701static psa_status_t psa_crypto_recover_transaction(
6702 const psa_crypto_transaction_t *transaction )
6703{
6704 switch( transaction->unknown.type )
6705 {
6706 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6707 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006708 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006709 * is implemented.
6710 * https://github.com/ARMmbed/mbed-crypto/issues/218
6711 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006712 default:
6713 /* We found an unsupported transaction in the storage.
6714 * We don't know what state the storage is in. Give up. */
6715 return( PSA_ERROR_STORAGE_FAILURE );
6716 }
6717}
6718#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6719
Gilles Peskinee59236f2018-01-27 23:32:46 +01006720psa_status_t psa_crypto_init( void )
6721{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006722 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006723
Gilles Peskinec6b69072018-11-20 21:42:52 +01006724 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006725 if( global_data.initialized != 0 )
6726 return( PSA_SUCCESS );
6727
Gilles Peskine30524eb2020-11-13 17:02:26 +01006728 /* Initialize and seed the random generator. */
6729 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006730 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006731 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006732 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006733 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006734 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006735
Gilles Peskine66fb1262018-12-10 16:29:04 +01006736 status = psa_initialize_key_slots( );
6737 if( status != PSA_SUCCESS )
6738 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006739
Gilles Peskined9348f22019-10-01 15:22:29 +02006740#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6741 status = psa_init_all_se_drivers( );
6742 if( status != PSA_SUCCESS )
6743 goto exit;
6744#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6745
Gilles Peskine4b734222019-07-24 15:56:31 +02006746#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006747 status = psa_crypto_load_transaction( );
6748 if( status == PSA_SUCCESS )
6749 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006750 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6751 if( status != PSA_SUCCESS )
6752 goto exit;
6753 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006754 }
6755 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6756 {
6757 /* There's no transaction to complete. It's all good. */
6758 status = PSA_SUCCESS;
6759 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006760#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006761
Gilles Peskinec6b69072018-11-20 21:42:52 +01006762 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006763 global_data.initialized = 1;
6764
6765exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006766 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006767 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006768 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006769}
6770
6771#endif /* MBEDTLS_PSA_CRYPTO_C */