blob: f54715dc3f7c28e22361696c36e817a9ff5d1d69 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb2b64d32020-12-14 16:43:58 +010043#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010044
Gilles Peskineb46bef22019-07-30 21:32:04 +020045#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020049#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#define mbedtls_calloc calloc
51#define mbedtls_free free
52#endif
53
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010054#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000057#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020058#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/blowfish.h"
60#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
72#include "mbedtls/md2.h"
73#include "mbedtls/md4.h"
74#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010075#include "mbedtls/md.h"
76#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010079#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000080#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
86#include "mbedtls/xtea.h"
87
Gilles Peskine996deb12018-08-01 15:45:45 +020088#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
89
Gilles Peskine9ef733f2018-02-07 21:05:37 +010090/* constant-time buffer comparison */
91static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
92{
93 size_t i;
94 unsigned char diff = 0;
95
96 for( i = 0; i < n; i++ )
97 diff |= a[i] ^ b[i];
98
99 return( diff );
100}
101
102
103
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100104/****************************************************************/
105/* Global data, support functions and library management */
106/****************************************************************/
107
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108static int key_type_is_raw_bytes( psa_key_type_t type )
109{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200110 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111}
112
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100113/* Values for psa_global_data_t::rng_state */
114#define RNG_NOT_INITIALIZED 0
115#define RNG_INITIALIZED 1
116#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100117
Gilles Peskine2d277862018-06-18 15:41:12 +0200118typedef struct
119{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100120 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100127#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
128mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
129 &global_data.rng.drbg;
130#endif
131
itayzafrir0adf0fc2018-09-06 16:24:41 +0300132#define GUARD_MODULE_INITIALIZED \
133 if( global_data.initialized == 0 ) \
134 return( PSA_ERROR_BAD_STATE );
135
Steven Cooreman01164162020-07-20 15:31:37 +0200136psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100138 /* Mbed TLS error codes can combine a high-level error code and a
139 * low-level error code. The low-level error usually reflects the
140 * root cause better, so dispatch on that preferably. */
141 int low_level_ret = - ( -ret & 0x007f );
142 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 {
144 case 0:
145 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100146
147 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
148 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
149 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
150 return( PSA_ERROR_NOT_SUPPORTED );
151 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
152 return( PSA_ERROR_HARDWARE_FAILURE );
153
154 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
155 return( PSA_ERROR_HARDWARE_FAILURE );
156
Gilles Peskine9a944802018-06-21 09:35:35 +0200157 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
158 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
159 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
160 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
161 case MBEDTLS_ERR_ASN1_INVALID_DATA:
162 return( PSA_ERROR_INVALID_ARGUMENT );
163 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
164 return( PSA_ERROR_INSUFFICIENT_MEMORY );
165 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
166 return( PSA_ERROR_BUFFER_TOO_SMALL );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
Jaeden Amero93e21112019-02-20 13:57:28 +0000178#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000179 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
181 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
182#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_CCM_BAD_INPUT:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190 case MBEDTLS_ERR_CCM_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
193 return( PSA_ERROR_HARDWARE_FAILURE );
194
Gilles Peskine26869f22019-05-06 15:25:00 +0200195 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197
198 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
199 return( PSA_ERROR_BAD_STATE );
200 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
201 return( PSA_ERROR_INVALID_SIGNATURE );
202
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
204 return( PSA_ERROR_NOT_SUPPORTED );
205 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
206 return( PSA_ERROR_INVALID_ARGUMENT );
207 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
208 return( PSA_ERROR_INSUFFICIENT_MEMORY );
209 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
210 return( PSA_ERROR_INVALID_PADDING );
211 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200212 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
214 return( PSA_ERROR_INVALID_SIGNATURE );
215 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200216 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
218 return( PSA_ERROR_HARDWARE_FAILURE );
219
220 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
Gilles Peskine82e57d12020-11-13 21:31:17 +0100223#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
224 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100225 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
226 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
229 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
230 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
231 return( PSA_ERROR_NOT_SUPPORTED );
232 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100235
236 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
237 return( PSA_ERROR_NOT_SUPPORTED );
238 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
Gilles Peskinee59236f2018-01-27 23:32:46 +0100241 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
242 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
243 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
244 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100245
246 case MBEDTLS_ERR_GCM_AUTH_FAILED:
247 return( PSA_ERROR_INVALID_SIGNATURE );
248 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200249 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
Gilles Peskine82e57d12020-11-13 21:31:17 +0100253#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
254 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100255 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
256 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
258 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
259 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
260 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
261 return( PSA_ERROR_NOT_SUPPORTED );
262 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
263 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
264#endif
265
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
267 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
268 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
269 return( PSA_ERROR_HARDWARE_FAILURE );
270
271 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MD_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
278 return( PSA_ERROR_STORAGE_FAILURE );
279 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
280 return( PSA_ERROR_HARDWARE_FAILURE );
281
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
283 return( PSA_ERROR_STORAGE_FAILURE );
284 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
289 return( PSA_ERROR_BUFFER_TOO_SMALL );
290 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
297 return( PSA_ERROR_INSUFFICIENT_MEMORY );
298
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
302 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
313 return( PSA_ERROR_NOT_PERMITTED );
314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
321 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
323 return( PSA_ERROR_HARDWARE_FAILURE );
324
Gilles Peskineff2d2002019-05-06 15:26:23 +0200325 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
328 return( PSA_ERROR_NOT_SUPPORTED );
329
Gilles Peskinea5905292018-02-07 20:59:33 +0100330 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
334 return( PSA_ERROR_INVALID_ARGUMENT );
335 case MBEDTLS_ERR_RSA_INVALID_PADDING:
336 return( PSA_ERROR_INVALID_PADDING );
337 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
338 return( PSA_ERROR_HARDWARE_FAILURE );
339 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
340 return( PSA_ERROR_INVALID_ARGUMENT );
341 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
342 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200343 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100344 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
345 return( PSA_ERROR_INVALID_SIGNATURE );
346 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
347 return( PSA_ERROR_BUFFER_TOO_SMALL );
348 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100349 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100350 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
351 return( PSA_ERROR_NOT_SUPPORTED );
352 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
354
355 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
356 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
357 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
358 return( PSA_ERROR_HARDWARE_FAILURE );
359
360 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
361 return( PSA_ERROR_INVALID_ARGUMENT );
362 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
363 return( PSA_ERROR_HARDWARE_FAILURE );
364
itayzafrir5c753392018-05-08 11:18:38 +0300365 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300367 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300368 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
369 return( PSA_ERROR_BUFFER_TOO_SMALL );
370 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
371 return( PSA_ERROR_NOT_SUPPORTED );
372 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
373 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
374 return( PSA_ERROR_INVALID_SIGNATURE );
375 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
376 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100377 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
378 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300379 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
380 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100381
Janos Follatha13b9052019-11-22 12:48:59 +0000382 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
383 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300384
Gilles Peskinee59236f2018-01-27 23:32:46 +0100385 default:
David Saadab4ecc272019-02-14 13:48:10 +0200386 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100387 }
388}
389
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200390
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200391
392
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100393/****************************************************************/
394/* Key management */
395/****************************************************************/
396
Gilles Peskine73167e12019-07-12 23:44:37 +0200397#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
398static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
399{
Gilles Peskine8e338702019-07-30 20:06:31 +0200400 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200401}
402#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
403
John Durkop07cc04a2020-11-16 22:08:34 -0800404/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
405 * current test driver in key_management.c is using this function
406 * when accelerators are used for ECC key pair and public key.
407 * Once that dependency is resolved these guards can be removed.
408 */
John Durkop9814fa22020-11-04 12:28:15 -0800409#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800410 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
411 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
412 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100413mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100414 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200415{
416 switch( curve )
417 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100418 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100419 switch( byte_length )
420 {
421 case PSA_BITS_TO_BYTES( 192 ):
422 return( MBEDTLS_ECP_DP_SECP192R1 );
423 case PSA_BITS_TO_BYTES( 224 ):
424 return( MBEDTLS_ECP_DP_SECP224R1 );
425 case PSA_BITS_TO_BYTES( 256 ):
426 return( MBEDTLS_ECP_DP_SECP256R1 );
427 case PSA_BITS_TO_BYTES( 384 ):
428 return( MBEDTLS_ECP_DP_SECP384R1 );
429 case PSA_BITS_TO_BYTES( 521 ):
430 return( MBEDTLS_ECP_DP_SECP521R1 );
431 default:
432 return( MBEDTLS_ECP_DP_NONE );
433 }
434 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100435
Paul Elliott8ff510a2020-06-02 17:19:28 +0100436 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100437 switch( byte_length )
438 {
439 case PSA_BITS_TO_BYTES( 256 ):
440 return( MBEDTLS_ECP_DP_BP256R1 );
441 case PSA_BITS_TO_BYTES( 384 ):
442 return( MBEDTLS_ECP_DP_BP384R1 );
443 case PSA_BITS_TO_BYTES( 512 ):
444 return( MBEDTLS_ECP_DP_BP512R1 );
445 default:
446 return( MBEDTLS_ECP_DP_NONE );
447 }
448 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100449
Paul Elliott8ff510a2020-06-02 17:19:28 +0100450 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100451 switch( byte_length )
452 {
453 case PSA_BITS_TO_BYTES( 255 ):
454 return( MBEDTLS_ECP_DP_CURVE25519 );
455 case PSA_BITS_TO_BYTES( 448 ):
456 return( MBEDTLS_ECP_DP_CURVE448 );
457 default:
458 return( MBEDTLS_ECP_DP_NONE );
459 }
460 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100461
Paul Elliott8ff510a2020-06-02 17:19:28 +0100462 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100463 switch( byte_length )
464 {
465 case PSA_BITS_TO_BYTES( 192 ):
466 return( MBEDTLS_ECP_DP_SECP192K1 );
467 case PSA_BITS_TO_BYTES( 224 ):
468 return( MBEDTLS_ECP_DP_SECP224K1 );
469 case PSA_BITS_TO_BYTES( 256 ):
470 return( MBEDTLS_ECP_DP_SECP256K1 );
471 default:
472 return( MBEDTLS_ECP_DP_NONE );
473 }
474 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100475
Gilles Peskine12313cd2018-06-20 00:20:32 +0200476 default:
477 return( MBEDTLS_ECP_DP_NONE );
478 }
479}
John Durkop9814fa22020-11-04 12:28:15 -0800480#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800481 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
482 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
483 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200484
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200485static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
486 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487{
488 /* Check that the bit size is acceptable for the key type */
489 switch( type )
490 {
491 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200493 case PSA_KEY_TYPE_DERIVE:
494 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495#if defined(MBEDTLS_AES_C)
496 case PSA_KEY_TYPE_AES:
497 if( bits != 128 && bits != 192 && bits != 256 )
498 return( PSA_ERROR_INVALID_ARGUMENT );
499 break;
500#endif
501#if defined(MBEDTLS_CAMELLIA_C)
502 case PSA_KEY_TYPE_CAMELLIA:
503 if( bits != 128 && bits != 192 && bits != 256 )
504 return( PSA_ERROR_INVALID_ARGUMENT );
505 break;
506#endif
507#if defined(MBEDTLS_DES_C)
508 case PSA_KEY_TYPE_DES:
509 if( bits != 64 && bits != 128 && bits != 192 )
510 return( PSA_ERROR_INVALID_ARGUMENT );
511 break;
512#endif
513#if defined(MBEDTLS_ARC4_C)
514 case PSA_KEY_TYPE_ARC4:
515 if( bits < 8 || bits > 2048 )
516 return( PSA_ERROR_INVALID_ARGUMENT );
517 break;
518#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200519#if defined(MBEDTLS_CHACHA20_C)
520 case PSA_KEY_TYPE_CHACHA20:
521 if( bits != 256 )
522 return( PSA_ERROR_INVALID_ARGUMENT );
523 break;
524#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200525 default:
526 return( PSA_ERROR_NOT_SUPPORTED );
527 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200528 if( bits % 8 != 0 )
529 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200530
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200531 return( PSA_SUCCESS );
532}
533
John Durkop0e005192020-10-31 22:06:54 -0700534#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
535 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
536 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800537 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
538 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
539 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200540
Gilles Peskine86a440b2018-11-12 18:39:40 +0100541/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
542 * that are not a multiple of 8) well. For example, there is only
543 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
544 * way to return the exact bit size of a key.
545 * To keep things simple, reject non-byte-aligned key sizes. */
546static psa_status_t psa_check_rsa_key_byte_aligned(
547 const mbedtls_rsa_context *rsa )
548{
549 mbedtls_mpi n;
550 psa_status_t status;
551 mbedtls_mpi_init( &n );
552 status = mbedtls_to_psa_error(
553 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
554 if( status == PSA_SUCCESS )
555 {
556 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
557 status = PSA_ERROR_NOT_SUPPORTED;
558 }
559 mbedtls_mpi_free( &n );
560 return( status );
561}
562
Steven Cooreman7f391872020-07-30 14:57:44 +0200563/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200564 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200565 * \param[in] type The type of key contained in \p data.
566 * \param[in] data The buffer from which to load the representation.
567 * \param[in] data_length The size in bytes of \p data.
568 * \param[out] p_rsa Returns a pointer to an RSA context on success.
569 * The caller is responsible for freeing both the
570 * contents of the context and the context itself
571 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200572 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200573static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
574 const uint8_t *data,
575 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200576 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200577{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000578 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200579 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000580 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200581 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000582
583 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200584 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000585 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200586 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200587 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000588 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200589 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000590 if( status != PSA_SUCCESS )
591 goto exit;
592
593 /* We have something that the pkparse module recognizes. If it is a
594 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200595 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200596 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000597 status = PSA_ERROR_INVALID_ARGUMENT;
598 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200599 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000600
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000601 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
602 * supports non-byte-aligned key sizes, but not well. For example,
603 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200604 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000605 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
606 {
607 status = PSA_ERROR_NOT_SUPPORTED;
608 goto exit;
609 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200610 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200611 if( status != PSA_SUCCESS )
612 goto exit;
613
Steven Cooremana2371e52020-07-28 14:30:39 +0200614 /* Copy out the pointer to the RSA context, and reset the PK context
615 * such that pk_free doesn't free the RSA context we just grabbed. */
616 *p_rsa = mbedtls_pk_rsa( ctx );
617 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000618
619exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200620 mbedtls_pk_free( &ctx );
621 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200622}
623
John Durkop6ba40d12020-11-10 08:50:04 -0800624#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
625 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
626 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
627 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
628 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
629 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
630
631#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
632 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
633
Steven Cooremana01795d2020-07-24 22:48:15 +0200634/** Export an RSA key to export representation
635 *
636 * \param[in] type The type of key (public/private) to export
637 * \param[in] rsa The internal RSA representation from which to export
638 * \param[out] data The buffer to export to
639 * \param[in] data_size The length of the buffer to export to
640 * \param[out] data_length The amount of bytes written to \p data
641 */
642static psa_status_t psa_export_rsa_key( psa_key_type_t type,
643 mbedtls_rsa_context *rsa,
644 uint8_t *data,
645 size_t data_size,
646 size_t *data_length )
647{
648#if defined(MBEDTLS_PK_WRITE_C)
649 int ret;
650 mbedtls_pk_context pk;
651 uint8_t *pos = data + data_size;
652
653 mbedtls_pk_init( &pk );
654 pk.pk_info = &mbedtls_rsa_info;
655 pk.pk_ctx = rsa;
656
657 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200658 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
659 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200660 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
661 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
662 else
663 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
664
665 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200666 {
667 /* Clean up in case pk_write failed halfway through. */
668 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200669 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200670 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200671
672 /* The mbedtls_pk_xxx functions write to the end of the buffer.
673 * Move the data to the beginning and erase remaining data
674 * at the original location. */
675 if( 2 * (size_t) ret <= data_size )
676 {
677 memcpy( data, data + data_size - ret, ret );
678 memset( data + data_size - ret, 0, ret );
679 }
680 else if( (size_t) ret < data_size )
681 {
682 memmove( data, data + data_size - ret, ret );
683 memset( data + ret, 0, data_size - ret );
684 }
685
686 *data_length = ret;
687 return( PSA_SUCCESS );
688#else
689 (void) type;
690 (void) rsa;
691 (void) data;
692 (void) data_size;
693 (void) data_length;
694 return( PSA_ERROR_NOT_SUPPORTED );
695#endif /* MBEDTLS_PK_WRITE_C */
696}
697
698/** Import an RSA key from import representation to a slot
699 *
700 * \param[in,out] slot The slot where to store the export representation to
701 * \param[in] data The buffer containing the import representation
702 * \param[in] data_length The amount of bytes in \p data
703 */
704static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
705 const uint8_t *data,
706 size_t data_length )
707{
708 psa_status_t status;
709 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200710 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200711
Steven Cooremana01795d2020-07-24 22:48:15 +0200712 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200713 status = psa_load_rsa_representation( slot->attr.type,
714 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200715 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200716 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200717 if( status != PSA_SUCCESS )
718 goto exit;
719
720 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200721 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200722
Steven Cooreman75b74362020-07-28 14:30:13 +0200723 /* Re-export the data to PSA export format, such that we can store export
724 * representation in the key slot. Export representation in case of RSA is
725 * the smallest representation that's allowed as input, so a straight-up
726 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200727 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200728 if( output == NULL )
729 {
730 status = PSA_ERROR_INSUFFICIENT_MEMORY;
731 goto exit;
732 }
733
Steven Cooremana01795d2020-07-24 22:48:15 +0200734 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200735 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200736 output,
737 data_length,
738 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200739exit:
740 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200741 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200742 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200743
744 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000745 if( status != PSA_SUCCESS )
746 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200747 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000748 return( status );
749 }
750
Steven Cooremana01795d2020-07-24 22:48:15 +0200751 /* On success, store the allocated export-formatted key. */
752 slot->data.key.data = output;
753 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000754
755 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200756}
John Durkop6ba40d12020-11-10 08:50:04 -0800757
758#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800759 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200760
John Durkop9814fa22020-11-04 12:28:15 -0800761#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800762 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
763 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
764 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
765 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200766/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200767 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200768 * \param[in] type The type of key contained in \p data.
769 * \param[in] data The buffer from which to load the representation.
770 * \param[in] data_length The size in bytes of \p data.
771 * \param[out] p_ecp Returns a pointer to an ECP context on success.
772 * The caller is responsible for freeing both the
773 * contents of the context and the context itself
774 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200775 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200776static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
777 const uint8_t *data,
778 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200779 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100780{
781 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200782 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200783 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200784 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100785
Steven Cooreman4fed4552020-08-03 14:46:03 +0200786 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
787 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100788 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200789 /* A Weierstrass public key is represented as:
790 * - The byte 0x04;
791 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
792 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200793 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200794 */
795 if( ( data_length & 1 ) == 0 )
796 return( PSA_ERROR_INVALID_ARGUMENT );
797 curve_size = data_length / 2;
798
799 /* Montgomery public keys are represented in compressed format, meaning
800 * their curve_size is equal to the amount of input. */
801
802 /* Private keys are represented in uncompressed private random integer
803 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100804 }
805
Steven Cooreman6d839f02020-07-30 11:36:45 +0200806 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200807 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200808 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200809 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200810 mbedtls_ecp_keypair_init( ecp );
811
Gilles Peskine4cd32772019-12-02 20:49:42 +0100812 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200813 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
814 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100815 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200816 {
817 status = PSA_ERROR_INVALID_ARGUMENT;
818 goto exit;
819 }
820
Steven Cooremanacda8342020-07-24 23:09:52 +0200821 status = mbedtls_to_psa_error(
822 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
823 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200824 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200825
Steven Cooreman6d839f02020-07-30 11:36:45 +0200826 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200827 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200828 {
829 /* Load the public value. */
830 status = mbedtls_to_psa_error(
831 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200832 data,
833 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200834 if( status != PSA_SUCCESS )
835 goto exit;
836
837 /* Check that the point is on the curve. */
838 status = mbedtls_to_psa_error(
839 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
840 if( status != PSA_SUCCESS )
841 goto exit;
842 }
843 else
844 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200845 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200846 status = mbedtls_to_psa_error(
847 mbedtls_ecp_read_key( ecp->grp.id,
848 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200849 data,
850 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200851 if( status != PSA_SUCCESS )
852 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200853 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200854
855 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200856exit:
857 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200858 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200859 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200860 mbedtls_free( ecp );
861 }
862
Steven Cooreman29149862020-08-05 15:43:42 +0200863 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100864}
John Durkop6ba40d12020-11-10 08:50:04 -0800865#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
866 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
867 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
868 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
869 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000870
John Durkop6ba40d12020-11-10 08:50:04 -0800871#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
872 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200873/** Export an ECP key to export representation
874 *
875 * \param[in] type The type of key (public/private) to export
876 * \param[in] ecp The internal ECP representation from which to export
877 * \param[out] data The buffer to export to
878 * \param[in] data_size The length of the buffer to export to
879 * \param[out] data_length The amount of bytes written to \p data
880 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200881static psa_status_t psa_export_ecp_key( psa_key_type_t type,
882 mbedtls_ecp_keypair *ecp,
883 uint8_t *data,
884 size_t data_size,
885 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200886{
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000888
Steven Cooremanacda8342020-07-24 23:09:52 +0200889 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200890 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200891 /* Check whether the public part is loaded */
892 if( mbedtls_ecp_is_zero( &ecp->Q ) )
893 {
894 /* Calculate the public key */
895 status = mbedtls_to_psa_error(
896 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100897 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200898 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200899 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200900 }
901
Steven Cooreman4fed4552020-08-03 14:46:03 +0200902 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200903 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
904 MBEDTLS_ECP_PF_UNCOMPRESSED,
905 data_length,
906 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200907 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200908 if( status != PSA_SUCCESS )
909 memset( data, 0, data_size );
910
Steven Cooreman29149862020-08-05 15:43:42 +0200911 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200912 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200913 else
914 {
Steven Cooreman29149862020-08-05 15:43:42 +0200915 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200916 return( PSA_ERROR_BUFFER_TOO_SMALL );
917
918 status = mbedtls_to_psa_error(
919 mbedtls_ecp_write_key( ecp,
920 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200921 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200922 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200923 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200924 else
925 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200926
927 return( status );
928 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200929}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200930
Steven Cooreman4fed4552020-08-03 14:46:03 +0200931/** Import an ECP key from import representation to a slot
932 *
933 * \param[in,out] slot The slot where to store the export representation to
934 * \param[in] data The buffer containing the import representation
935 * \param[in] data_length The amount of bytes in \p data
936 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200937static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
938 const uint8_t *data,
939 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100940{
Steven Cooremanacda8342020-07-24 23:09:52 +0200941 psa_status_t status;
942 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200943 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100944
Steven Cooremanacda8342020-07-24 23:09:52 +0200945 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200946 status = psa_load_ecp_representation( slot->attr.type,
947 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200948 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200949 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100950 if( status != PSA_SUCCESS )
951 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100952
Steven Cooremanacda8342020-07-24 23:09:52 +0200953 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200954 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200955 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200956 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200957
Steven Cooremanacda8342020-07-24 23:09:52 +0200958 /* Re-export the data to PSA export format. There is currently no support
959 * for other input formats then the export format, so this is a 1-1
960 * copy operation. */
961 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200962 if( output == NULL )
963 {
964 status = PSA_ERROR_INSUFFICIENT_MEMORY;
965 goto exit;
966 }
967
968 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200969 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200970 output,
971 data_length,
972 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100973exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200974 /* Always free the PK object (will also free contained ECP context) */
975 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200976 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200977
978 /* Free the allocated buffer only on error. */
979 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100980 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200981 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200982 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100983 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200984
985 /* On success, store the allocated export-formatted key. */
986 slot->data.key.data = output;
987 slot->data.key.bytes = data_length;
988
989 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100990}
John Durkop9814fa22020-11-04 12:28:15 -0800991#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
992 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100993
Gilles Peskineb46bef22019-07-30 21:32:04 +0200994/** Return the size of the key in the given slot, in bits.
995 *
996 * \param[in] slot A key slot.
997 *
998 * \return The key size in bits, read from the metadata in the slot.
999 */
1000static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
1001{
1002 return( slot->attr.bits );
1003}
1004
Steven Cooreman75b74362020-07-28 14:30:13 +02001005/** Try to allocate a buffer to an empty key slot.
1006 *
1007 * \param[in,out] slot Key slot to attach buffer to.
1008 * \param[in] buffer_length Requested size of the buffer.
1009 *
1010 * \retval #PSA_SUCCESS
1011 * The buffer has been successfully allocated.
1012 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1013 * Not enough memory was available for allocation.
1014 * \retval #PSA_ERROR_ALREADY_EXISTS
1015 * Trying to allocate a buffer to a non-empty key slot.
1016 */
1017static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
1018 size_t buffer_length )
1019{
1020 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001021 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001022
1023 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
1024 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001025 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001026
1027 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001028 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001029}
1030
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001031psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1032 const uint8_t* data,
1033 size_t data_length )
1034{
1035 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1036 data_length );
1037 if( status != PSA_SUCCESS )
1038 return( status );
1039
1040 memcpy( slot->data.key.data, data, data_length );
1041 return( PSA_SUCCESS );
1042}
1043
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001044/** Import key data into a slot.
1045 *
1046 * `slot->type` must have been set previously.
1047 * This function assumes that the slot does not contain any key material yet.
1048 * On failure, the slot content is unchanged.
1049 *
1050 * Persistent storage is not affected.
1051 *
1052 * \param[in,out] slot The key slot to import data into.
1053 * Its `type` field must have previously been set to
1054 * the desired key type.
1055 * It must not contain any key material yet.
1056 * \param[in] data Buffer containing the key material to parse and import.
1057 * \param data_length Size of \p data in bytes.
1058 *
1059 * \retval #PSA_SUCCESS
1060 * \retval #PSA_ERROR_INVALID_ARGUMENT
1061 * \retval #PSA_ERROR_NOT_SUPPORTED
1062 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1063 */
1064static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1065 const uint8_t *data,
1066 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001067{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001068 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001069 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001071 /* zero-length keys are never supported. */
1072 if( data_length == 0 )
1073 return( PSA_ERROR_NOT_SUPPORTED );
1074
Gilles Peskine8e338702019-07-30 20:06:31 +02001075 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001076 {
Steven Cooreman04524762020-10-13 17:43:44 +02001077 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001078
Steven Cooreman75b74362020-07-28 14:30:13 +02001079 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1080 if( data_length > SIZE_MAX / 8 )
1081 return( PSA_ERROR_NOT_SUPPORTED );
1082
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001083 /* Enforce a size limit, and in particular ensure that the bit
1084 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001085 if( bit_size > PSA_MAX_KEY_BITS )
1086 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001087
1088 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001089 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001090 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001091
1092 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001093 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001094 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001095 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001096
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001097 /* Write the actual key size to the slot.
1098 * psa_start_key_creation() wrote the size declared by the
1099 * caller, which may be 0 (meaning unspecified) or wrong. */
1100 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001101
1102 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001103 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001104 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001105 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001106 /* Try validation through accelerators first. */
1107 bit_size = slot->attr.bits;
1108 psa_key_attributes_t attributes = {
1109 .core = slot->attr
1110 };
1111 status = psa_driver_wrapper_validate_key( &attributes,
1112 data,
1113 data_length,
1114 &bit_size );
1115 if( status == PSA_SUCCESS )
1116 {
1117 /* Key has been validated successfully by an accelerator.
1118 * Copy key material into slot. */
1119 status = psa_copy_key_material_into_slot( slot, data, data_length );
1120 if( status != PSA_SUCCESS )
1121 return( status );
1122
1123 slot->attr.bits = (psa_key_bits_t) bit_size;
1124 return( PSA_SUCCESS );
1125 }
1126 else if( status != PSA_ERROR_NOT_SUPPORTED )
1127 return( status );
1128
1129 /* Key format is not supported by any accelerator, try software fallback
1130 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001131#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1132 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001133 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1134 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001135 return( psa_import_ecp_key( slot, data, data_length ) );
1136 }
John Durkop9814fa22020-11-04 12:28:15 -08001137#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1138 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001139#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1140 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001141 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001142 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001143 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001144 }
John Durkop9814fa22020-11-04 12:28:15 -08001145#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1146 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001147
1148 /* Fell through the fallback as well, so have nothing else to try. */
1149 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001150 }
1151 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001152 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001153 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001154 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001155 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001156}
1157
Gilles Peskinef603c712019-01-19 13:40:11 +01001158/** Calculate the intersection of two algorithm usage policies.
1159 *
1160 * Return 0 (which allows no operation) on incompatibility.
1161 */
1162static psa_algorithm_t psa_key_policy_algorithm_intersection(
1163 psa_algorithm_t alg1,
1164 psa_algorithm_t alg2 )
1165{
Gilles Peskine549ea862019-05-22 11:45:59 +02001166 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001167 if( alg1 == alg2 )
1168 return( alg1 );
1169 /* If the policies are from the same hash-and-sign family, check
1170 * if one is a wildcard. If so the other has the specific algorithm. */
1171 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1172 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1173 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1174 {
1175 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1176 return( alg2 );
1177 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1178 return( alg1 );
1179 }
1180 /* If the policies are incompatible, allow nothing. */
1181 return( 0 );
1182}
1183
Gilles Peskined6f371b2019-05-10 19:33:38 +02001184static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1185 psa_algorithm_t requested_alg )
1186{
Gilles Peskine549ea862019-05-22 11:45:59 +02001187 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001188 if( requested_alg == policy_alg )
1189 return( 1 );
1190 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001191 * and requested_alg is the same hash-and-sign family with any hash,
1192 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001193 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1194 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1195 {
1196 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1197 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1198 }
Steven Cooremance48e852020-10-05 16:02:45 +02001199 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001200 * a key derivation with that key agreement should also be allowed. This
1201 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001202 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1203 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1204 {
1205 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1206 policy_alg );
1207 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001208 /* If it isn't permitted, it's forbidden. */
1209 return( 0 );
1210}
1211
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001212/** Test whether a policy permits an algorithm.
1213 *
1214 * The caller must test usage flags separately.
1215 */
1216static int psa_key_policy_permits( const psa_key_policy_t *policy,
1217 psa_algorithm_t alg )
1218{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001219 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1220 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001221}
1222
Gilles Peskinef603c712019-01-19 13:40:11 +01001223/** Restrict a key policy based on a constraint.
1224 *
1225 * \param[in,out] policy The policy to restrict.
1226 * \param[in] constraint The policy constraint to apply.
1227 *
1228 * \retval #PSA_SUCCESS
1229 * \c *policy contains the intersection of the original value of
1230 * \c *policy and \c *constraint.
1231 * \retval #PSA_ERROR_INVALID_ARGUMENT
1232 * \c *policy and \c *constraint are incompatible.
1233 * \c *policy is unchanged.
1234 */
1235static psa_status_t psa_restrict_key_policy(
1236 psa_key_policy_t *policy,
1237 const psa_key_policy_t *constraint )
1238{
1239 psa_algorithm_t intersection_alg =
1240 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001241 psa_algorithm_t intersection_alg2 =
1242 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001243 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1244 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001245 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1246 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001247 policy->usage &= constraint->usage;
1248 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001249 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001250 return( PSA_SUCCESS );
1251}
1252
Ronald Cron5c522922020-11-14 16:35:34 +01001253/** Get the description of a key given its identifier and policy constraints
1254 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001255 *
Ronald Cron5c522922020-11-14 16:35:34 +01001256 * The key must have allow all the usage flags set in \p usage. If \p alg is
1257 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001258 *
Ronald Cron5c522922020-11-14 16:35:34 +01001259 * In case of a persistent key, the function loads the description of the key
1260 * into a key slot if not already done.
1261 *
1262 * On success, the returned key slot is locked. It is the responsibility of
1263 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001264 */
Ronald Cron5c522922020-11-14 16:35:34 +01001265static psa_status_t psa_get_and_lock_key_slot_with_policy(
1266 mbedtls_svc_key_id_t key,
1267 psa_key_slot_t **p_slot,
1268 psa_key_usage_t usage,
1269 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001270{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1272 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001273
Ronald Cron5c522922020-11-14 16:35:34 +01001274 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001275 if( status != PSA_SUCCESS )
1276 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001277 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001278
1279 /* Enforce that usage policy for the key slot contains all the flags
1280 * required by the usage parameter. There is one exception: public
1281 * keys can always be exported, so we treat public key objects as
1282 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001283 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001284 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001285
1286 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001287 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001288 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001289
1290 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001291 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001292 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001293
Darryl Green06fd18d2018-07-16 11:21:11 +01001294 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001295
1296error:
1297 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001298 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001299
1300 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001301}
Darryl Green940d72c2018-07-13 13:18:51 +01001302
Ronald Cron5c522922020-11-14 16:35:34 +01001303/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001304 *
1305 * A transparent key is a key for which the key material is directly
1306 * available, as opposed to a key in a secure element.
1307 *
Ronald Cron5c522922020-11-14 16:35:34 +01001308 * This is a temporary function to use instead of
1309 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1310 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001311 *
Ronald Cron5c522922020-11-14 16:35:34 +01001312 * On success, the returned key slot is locked. It is the responsibility of the
1313 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001314 */
1315#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001316static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1317 mbedtls_svc_key_id_t key,
1318 psa_key_slot_t **p_slot,
1319 psa_key_usage_t usage,
1320 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001321{
Ronald Cron5c522922020-11-14 16:35:34 +01001322 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1323 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001324 if( status != PSA_SUCCESS )
1325 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001326
Gilles Peskineadad8132019-07-25 11:31:23 +02001327 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001328 {
Ronald Cron5c522922020-11-14 16:35:34 +01001329 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001330 *p_slot = NULL;
1331 return( PSA_ERROR_NOT_SUPPORTED );
1332 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001333
Gilles Peskine28f8f302019-07-24 13:30:31 +02001334 return( PSA_SUCCESS );
1335}
1336#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1337/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001338#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1339 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001340#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1341
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001342/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001343static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001344{
Gilles Peskine73167e12019-07-12 23:44:37 +02001345#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Fredrik Strupe462aa572020-12-17 10:44:38 +01001346 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) &&
1347 psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001348 {
1349 /* No key material to clean. */
1350 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001351 else
1352#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001353 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001354 /* Data pointer will always be either a valid pointer or NULL in an
1355 * initialized slot, so we can just free it. */
1356 if( slot->data.key.data != NULL )
1357 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001358 mbedtls_free( slot->data.key.data );
1359 slot->data.key.data = NULL;
1360 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001361 }
Darryl Green40225ba2018-11-15 14:48:15 +00001362
1363 return( PSA_SUCCESS );
1364}
1365
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001366/** Completely wipe a slot in memory, including its policy.
1367 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001368psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001369{
1370 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001371
1372 /*
1373 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001374 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001375 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1376 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001377 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001378 */
Ronald Cron5c522922020-11-14 16:35:34 +01001379 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001380 {
1381#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001382 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001383#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001384 status = PSA_ERROR_CORRUPTION_DETECTED;
1385 }
1386
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001387 /* Multipart operations may still be using the key. This is safe
1388 * because all multipart operation objects are independent from
1389 * the key slot: if they need to access the key after the setup
1390 * phase, they have a copy of the key. Note that this means that
1391 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001392 /* At this point, key material and other type-specific content has
1393 * been wiped. Clear remaining metadata. We can call memset and not
1394 * zeroize because the metadata is not particularly sensitive. */
1395 memset( slot, 0, sizeof( *slot ) );
1396 return( status );
1397}
1398
Ronald Croncf56a0a2020-08-04 09:51:30 +02001399psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001400{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001401 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001402 psa_status_t status; /* status of the last operation */
1403 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001404#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1405 psa_se_drv_table_entry_t *driver;
1406#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001407
Ronald Croncf56a0a2020-08-04 09:51:30 +02001408 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001409 return( PSA_SUCCESS );
1410
Ronald Cronf2911112020-10-29 17:51:10 +01001411 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001412 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001413 * key, this will load the key description from persistent memory if not
1414 * done yet. We cannot avoid this loading as without it we don't know if
1415 * the key is operated by an SE or not and this information is needed by
1416 * the current implementation.
1417 */
Ronald Cron5c522922020-11-14 16:35:34 +01001418 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001419 if( status != PSA_SUCCESS )
1420 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001421
Ronald Cronf2911112020-10-29 17:51:10 +01001422 /*
1423 * If the key slot containing the key description is under access by the
1424 * library (apart from the present access), the key cannot be destroyed
1425 * yet. For the time being, just return in error. Eventually (to be
1426 * implemented), the key should be destroyed when all accesses have
1427 * stopped.
1428 */
Ronald Cron5c522922020-11-14 16:35:34 +01001429 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001430 {
Ronald Cron5c522922020-11-14 16:35:34 +01001431 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001432 return( PSA_ERROR_GENERIC_ERROR );
1433 }
1434
Gilles Peskine354f7672019-07-12 23:46:38 +02001435#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001436 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001437 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001438 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001439 /* For a key in a secure element, we need to do three things:
1440 * remove the key file in internal storage, destroy the
1441 * key inside the secure element, and update the driver's
1442 * persistent data. Start a transaction that will encompass these
1443 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001444 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001445 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001446 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001447 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001448 status = psa_crypto_save_transaction( );
1449 if( status != PSA_SUCCESS )
1450 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001451 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001452 /* We should still try to destroy the key in the secure
1453 * element and the key metadata in storage. This is especially
1454 * important if the error is that the storage is full.
1455 * But how to do it exactly without risking an inconsistent
1456 * state after a reset?
1457 * https://github.com/ARMmbed/mbed-crypto/issues/215
1458 */
1459 overall_status = status;
1460 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001461 }
1462
Gilles Peskine354f7672019-07-12 23:46:38 +02001463 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001464 if( overall_status == PSA_SUCCESS )
1465 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001466 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001467#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1468
Darryl Greend49a4992018-06-18 17:27:26 +01001469#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001470 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001471 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001472 status = psa_destroy_persistent_key( slot->attr.id );
1473 if( overall_status == PSA_SUCCESS )
1474 overall_status = status;
1475
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001476 /* TODO: other slots may have a copy of the same key. We should
1477 * invalidate them.
1478 * https://github.com/ARMmbed/mbed-crypto/issues/214
1479 */
Darryl Greend49a4992018-06-18 17:27:26 +01001480 }
1481#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001482
Gilles Peskinefc762652019-07-22 19:30:34 +02001483#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1484 if( driver != NULL )
1485 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001486 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001487 if( overall_status == PSA_SUCCESS )
1488 overall_status = status;
1489 status = psa_crypto_stop_transaction( );
1490 if( overall_status == PSA_SUCCESS )
1491 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001492 }
1493#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1494
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001495#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1496exit:
1497#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001498 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001499 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1500 if( overall_status == PSA_SUCCESS )
1501 overall_status = status;
1502 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503}
1504
John Durkop07cc04a2020-11-16 22:08:34 -08001505#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001506 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001507static psa_status_t psa_get_rsa_public_exponent(
1508 const mbedtls_rsa_context *rsa,
1509 psa_key_attributes_t *attributes )
1510{
1511 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001513 uint8_t *buffer = NULL;
1514 size_t buflen;
1515 mbedtls_mpi_init( &mpi );
1516
1517 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1518 if( ret != 0 )
1519 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001520 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1521 {
1522 /* It's the default value, which is reported as an empty string,
1523 * so there's nothing to do. */
1524 goto exit;
1525 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001526
1527 buflen = mbedtls_mpi_size( &mpi );
1528 buffer = mbedtls_calloc( 1, buflen );
1529 if( buffer == NULL )
1530 {
1531 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1532 goto exit;
1533 }
1534 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1535 if( ret != 0 )
1536 goto exit;
1537 attributes->domain_parameters = buffer;
1538 attributes->domain_parameters_size = buflen;
1539
1540exit:
1541 mbedtls_mpi_free( &mpi );
1542 if( ret != 0 )
1543 mbedtls_free( buffer );
1544 return( mbedtls_to_psa_error( ret ) );
1545}
John Durkop07cc04a2020-11-16 22:08:34 -08001546#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001547 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001548
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001549/** Retrieve all the publicly-accessible attributes of a key.
1550 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001551psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001552 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001555 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001556 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001557
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001558 psa_reset_key_attributes( attributes );
1559
Ronald Cron5c522922020-11-14 16:35:34 +01001560 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001561 if( status != PSA_SUCCESS )
1562 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001563
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001564 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001565 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1566 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1567
1568#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1569 if( psa_key_slot_is_external( slot ) )
1570 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1571#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001572
Gilles Peskine8e338702019-07-30 20:06:31 +02001573 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001574 {
John Durkop07cc04a2020-11-16 22:08:34 -08001575#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001576 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001577 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001578 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001579#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001580 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001581 * is not yet implemented.
1582 * https://github.com/ARMmbed/mbed-crypto/issues/216
1583 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001584 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001585 break;
1586#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001587 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001588 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001589
Steven Cooreman4fed4552020-08-03 14:46:03 +02001590 status = psa_load_rsa_representation( slot->attr.type,
1591 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001592 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001593 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001594 if( status != PSA_SUCCESS )
1595 break;
1596
Steven Cooremana2371e52020-07-28 14:30:39 +02001597 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001598 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001599 mbedtls_rsa_free( rsa );
1600 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001601 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001602 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001603#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001604 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001605 default:
1606 /* Nothing else to do. */
1607 break;
1608 }
1609
1610 if( status != PSA_SUCCESS )
1611 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001612
Ronald Cron5c522922020-11-14 16:35:34 +01001613 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001614
Ronald Cron5c522922020-11-14 16:35:34 +01001615 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001616}
1617
Gilles Peskinec8000c02019-08-02 20:15:51 +02001618#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1619psa_status_t psa_get_key_slot_number(
1620 const psa_key_attributes_t *attributes,
1621 psa_key_slot_number_t *slot_number )
1622{
1623 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1624 {
1625 *slot_number = attributes->slot_number;
1626 return( PSA_SUCCESS );
1627 }
1628 else
1629 return( PSA_ERROR_INVALID_ARGUMENT );
1630}
1631#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1632
Steven Cooremana01795d2020-07-24 22:48:15 +02001633static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1634 uint8_t *data,
1635 size_t data_size,
1636 size_t *data_length )
1637{
1638 if( slot->data.key.bytes > data_size )
1639 return( PSA_ERROR_BUFFER_TOO_SMALL );
1640 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1641 memset( data + slot->data.key.bytes, 0,
1642 data_size - slot->data.key.bytes );
1643 *data_length = slot->data.key.bytes;
1644 return( PSA_SUCCESS );
1645}
1646
Gilles Peskinef603c712019-01-19 13:40:11 +01001647static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1648 uint8_t *data,
1649 size_t data_size,
1650 size_t *data_length,
1651 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001652{
Gilles Peskine5d309672019-07-12 23:47:28 +02001653#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1654 const psa_drv_se_t *drv;
1655 psa_drv_se_context_t *drv_context;
1656#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1657
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001658 *data_length = 0;
1659
Gilles Peskine8e338702019-07-30 20:06:31 +02001660 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001661 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001662
Gilles Peskinef9168942019-09-12 19:20:29 +02001663 /* Reject a zero-length output buffer now, since this can never be a
1664 * valid key representation. This way we know that data must be a valid
1665 * pointer and we can do things like memset(data, ..., data_size). */
1666 if( data_size == 0 )
1667 return( PSA_ERROR_BUFFER_TOO_SMALL );
1668
Gilles Peskine5d309672019-07-12 23:47:28 +02001669#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001670 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001671 {
1672 psa_drv_se_export_key_t method;
1673 if( drv->key_management == NULL )
1674 return( PSA_ERROR_NOT_SUPPORTED );
1675 method = ( export_public_key ?
1676 drv->key_management->p_export_public :
1677 drv->key_management->p_export );
1678 if( method == NULL )
1679 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001680 return( method( drv_context,
1681 slot->data.se.slot_number,
1682 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001683 }
1684#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1685
Gilles Peskine8e338702019-07-30 20:06:31 +02001686 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001687 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001688 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001689 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001690 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1691 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001692 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001693 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001694 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001695 /* Exporting public -> public */
1696 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1697 }
1698 else if( !export_public_key )
1699 {
1700 /* Exporting private -> private */
1701 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1702 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001703
Steven Cooreman560c28a2020-07-24 23:20:24 +02001704 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001705 * so conversion is needed. Try the accelerators first. */
1706 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1707 data,
1708 data_size,
1709 data_length );
1710
1711 if( status != PSA_ERROR_NOT_SUPPORTED ||
1712 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1713 return( status );
1714
Steven Cooreman560c28a2020-07-24 23:20:24 +02001715 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1716 {
John Durkop0e005192020-10-31 22:06:54 -07001717#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1718 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001719 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001720 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001721 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001722 slot->data.key.data,
1723 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001724 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001725 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001726 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001727
Steven Cooreman560c28a2020-07-24 23:20:24 +02001728 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001729 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001730 data,
1731 data_size,
1732 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001733
Steven Cooremana2371e52020-07-28 14:30:39 +02001734 mbedtls_rsa_free( rsa );
1735 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001736
Steven Cooreman560c28a2020-07-24 23:20:24 +02001737 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001738#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001739 /* We don't know how to convert a private RSA key to public. */
1740 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001741#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1742 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001743 }
1744 else
Moran Pekera998bc62018-04-16 18:16:20 +03001745 {
John Durkop6ba40d12020-11-10 08:50:04 -08001746#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1747 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001748 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001749 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001750 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001751 slot->data.key.data,
1752 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001753 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001754 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001755 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001756
1757 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1758 PSA_KEY_TYPE_ECC_GET_FAMILY(
1759 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001760 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001761 data,
1762 data_size,
1763 data_length );
1764
Steven Cooremana2371e52020-07-28 14:30:39 +02001765 mbedtls_ecp_keypair_free( ecp );
1766 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001767 return( status );
1768#else
1769 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001770 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001771#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1772 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001773 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001774 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001775 else
1776 {
1777 /* This shouldn't happen in the reference implementation, but
1778 it is valid for a special-purpose implementation to omit
1779 support for exporting certain key types. */
1780 return( PSA_ERROR_NOT_SUPPORTED );
1781 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001782}
1783
Ronald Croncf56a0a2020-08-04 09:51:30 +02001784psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001785 uint8_t *data,
1786 size_t data_size,
1787 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001788{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001789 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001790 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001791 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001792
1793 /* Set the key to empty now, so that even when there are errors, we always
1794 * set data_length to a value between 0 and data_size. On error, setting
1795 * the key to empty is a good choice because an empty key representation is
1796 * unlikely to be accepted anywhere. */
1797 *data_length = 0;
1798
1799 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001800 * which don't require any flag, but
1801 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1802 */
1803 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1804 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001805 if( status != PSA_SUCCESS )
1806 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001807
1808 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001809 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001810
Ronald Cron5c522922020-11-14 16:35:34 +01001811 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001812}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001813
Ronald Croncf56a0a2020-08-04 09:51:30 +02001814psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001815 uint8_t *data,
1816 size_t data_size,
1817 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001818{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001819 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001820 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001821 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001822
1823 /* Set the key to empty now, so that even when there are errors, we always
1824 * set data_length to a value between 0 and data_size. On error, setting
1825 * the key to empty is a good choice because an empty key representation is
1826 * unlikely to be accepted anywhere. */
1827 *data_length = 0;
1828
1829 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001830 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001831 if( status != PSA_SUCCESS )
1832 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001833
1834 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001835 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001836
Ronald Cron5c522922020-11-14 16:35:34 +01001837 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001838}
1839
Gilles Peskine91e8c332019-08-02 19:19:39 +02001840#if defined(static_assert)
1841static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1842 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001843static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001844 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001845static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001846 "One or more key attribute flag is listed as both internal-only and external-only" );
1847#endif
1848
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001849/** Validate that a key policy is internally well-formed.
1850 *
1851 * This function only rejects invalid policies. It does not validate the
1852 * consistency of the policy with respect to other attributes of the key
1853 * such as the key type.
1854 */
1855static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001856{
1857 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001858 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 PSA_KEY_USAGE_ENCRYPT |
1860 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001861 PSA_KEY_USAGE_SIGN_HASH |
1862 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001863 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1864 return( PSA_ERROR_INVALID_ARGUMENT );
1865
Gilles Peskine4747d192019-04-17 15:05:45 +02001866 return( PSA_SUCCESS );
1867}
1868
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001869/** Validate the internal consistency of key attributes.
1870 *
1871 * This function only rejects invalid attribute values. If does not
1872 * validate the consistency of the attributes with any key data that may
1873 * be involved in the creation of the key.
1874 *
1875 * Call this function early in the key creation process.
1876 *
1877 * \param[in] attributes Key attributes for the new key.
1878 * \param[out] p_drv On any return, the driver for the key, if any.
1879 * NULL for a transparent key.
1880 *
1881 */
1882static psa_status_t psa_validate_key_attributes(
1883 const psa_key_attributes_t *attributes,
1884 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001885{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001886 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001887 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001888 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001889
Ronald Cron54b90082020-10-29 15:26:43 +01001890 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001891 if( status != PSA_SUCCESS )
1892 return( status );
1893
Ronald Crond2ed4812020-07-17 16:11:30 +02001894 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001895 if( status != PSA_SUCCESS )
1896 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001897
Ronald Cron65f38a32020-10-23 17:11:13 +02001898 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1899 {
1900 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1901 return( PSA_ERROR_INVALID_ARGUMENT );
1902 }
1903 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001904 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001905 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001906 if( status != PSA_SUCCESS )
1907 return( status );
1908 }
1909
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001910 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001911 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001912 return( status );
1913
1914 /* Refuse to create overly large keys.
1915 * Note that this doesn't trigger on import if the attributes don't
1916 * explicitly specify a size (so psa_get_key_bits returns 0), so
1917 * psa_import_key() needs its own checks. */
1918 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1919 return( PSA_ERROR_NOT_SUPPORTED );
1920
Gilles Peskine91e8c332019-08-02 19:19:39 +02001921 /* Reject invalid flags. These should not be reachable through the API. */
1922 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1923 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1924 return( PSA_ERROR_INVALID_ARGUMENT );
1925
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001926 return( PSA_SUCCESS );
1927}
1928
Gilles Peskine4747d192019-04-17 15:05:45 +02001929/** Prepare a key slot to receive key material.
1930 *
1931 * This function allocates a key slot and sets its metadata.
1932 *
1933 * If this function fails, call psa_fail_key_creation().
1934 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001935 * This function is intended to be used as follows:
1936 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001937 * it with the specified attributes, and in case of a volatile key assign it
1938 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001939 * -# Populate the slot with the key material.
1940 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1941 * In case of failure at any step, stop the sequence and call
1942 * psa_fail_key_creation().
1943 *
Ronald Cron5c522922020-11-14 16:35:34 +01001944 * On success, the key slot is locked. It is the responsibility of the caller
1945 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001946 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001947 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001948 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001949 * \param[out] p_slot On success, a pointer to the prepared slot.
1950 * \param[out] p_drv On any return, the driver for the key, if any.
1951 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001952 *
1953 * \retval #PSA_SUCCESS
1954 * The key slot is ready to receive key material.
1955 * \return If this function fails, the key slot is an invalid state.
1956 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001957 */
1958static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001959 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001960 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001961 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001962 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001963{
1964 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001965 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001966 psa_key_slot_t *slot;
1967
Gilles Peskinedf179142019-07-15 22:02:14 +02001968 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001969 *p_drv = NULL;
1970
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001971 status = psa_validate_key_attributes( attributes, p_drv );
1972 if( status != PSA_SUCCESS )
1973 return( status );
1974
Ronald Cronc4d1b512020-07-31 11:26:37 +02001975 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001976 if( status != PSA_SUCCESS )
1977 return( status );
1978 slot = *p_slot;
1979
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001980 /* We're storing the declared bit-size of the key. It's up to each
1981 * creation mechanism to verify that this information is correct.
1982 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001983 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001984 * is optional (import, copy). In case of a volatile key, assign it the
1985 * volatile key identifier associated to the slot returned to contain its
1986 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001987
1988 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001989 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1990 {
1991#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1992 slot->attr.id = volatile_key_id;
1993#else
1994 slot->attr.id.key_id = volatile_key_id;
1995#endif
1996 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001997
Gilles Peskine91e8c332019-08-02 19:19:39 +02001998 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001999 * external-only flags, query `attributes`. Thanks to the check
2000 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002001 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002002 * may have set. */
2003 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002004
Gilles Peskinecbaff462019-07-12 23:46:04 +02002005#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002006 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002007 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002008 * create the key file in internal storage, create the
2009 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002010 * persistent data. This is done by starting a transaction that will
2011 * encompass these three actions.
2012 * For registering a volatile key, we just need to find an appropriate
2013 * slot number inside the SE. Since the key is designated volatile, creating
2014 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002015 /* The first thing to do is to find a slot number for the new key.
2016 * We save the slot number in persistent storage as part of the
2017 * transaction data. It will be needed to recover if the power
2018 * fails during the key creation process, to clean up on the secure
2019 * element side after restarting. Obtaining a slot number from the
2020 * secure element driver updates its persistent state, but we do not yet
2021 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002022 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002023 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002024 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002025 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002026 &slot->data.se.slot_number );
2027 if( status != PSA_SUCCESS )
2028 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002029
2030 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002031 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002032 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2033 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2034 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2035 psa_crypto_transaction.key.id = slot->attr.id;
2036 status = psa_crypto_save_transaction( );
2037 if( status != PSA_SUCCESS )
2038 {
2039 (void) psa_crypto_stop_transaction( );
2040 return( status );
2041 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002042 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002043 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002044
2045 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2046 {
2047 /* Key registration only makes sense with a secure element. */
2048 return( PSA_ERROR_INVALID_ARGUMENT );
2049 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002050#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2051
Ronald Cronc4d1b512020-07-31 11:26:37 +02002052 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002053}
Gilles Peskine4747d192019-04-17 15:05:45 +02002054
2055/** Finalize the creation of a key once its key material has been set.
2056 *
2057 * This entails writing the key to persistent storage.
2058 *
2059 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002060 * See the documentation of psa_start_key_creation() for the intended use
2061 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002062 *
Ronald Cron5c522922020-11-14 16:35:34 +01002063 * If the finalization succeeds, the function unlocks the key slot (it was
2064 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2065 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002066 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002067 * \param[in,out] slot Pointer to the slot with key material.
2068 * \param[in] driver The secure element driver for the key,
2069 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002070 * \param[out] key On success, identifier of the key. Note that the
2071 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002072 *
2073 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002074 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002075 * \return If this function fails, the key slot is an invalid state.
2076 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002077 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002078static psa_status_t psa_finish_key_creation(
2079 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002080 psa_se_drv_table_entry_t *driver,
2081 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002082{
2083 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002084 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002085 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002086
2087#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002088 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002089 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002090#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2091 if( driver != NULL )
2092 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002093 psa_se_key_data_storage_t data;
2094#if defined(static_assert)
2095 static_assert( sizeof( slot->data.se.slot_number ) ==
2096 sizeof( data.slot_number ),
2097 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002098#endif
2099 memcpy( &data.slot_number, &slot->data.se.slot_number,
2100 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002101 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002102 (uint8_t*) &data,
2103 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002104 }
2105 else
2106#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2107 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002108 /* Key material is saved in export representation in the slot, so
2109 * just pass the slot buffer for storage. */
2110 status = psa_save_persistent_key( &slot->attr,
2111 slot->data.key.data,
2112 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002113 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002114 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002115#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2116
Gilles Peskinecbaff462019-07-12 23:46:04 +02002117#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002118 /* Finish the transaction for a key creation. This does not
2119 * happen when registering an existing key. Detect this case
2120 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002121 * creation of a persistent key in a secure element requires a transaction,
2122 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002123 if( driver != NULL &&
2124 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002125 {
2126 status = psa_save_se_persistent_data( driver );
2127 if( status != PSA_SUCCESS )
2128 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002129 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002130 return( status );
2131 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002132 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002133 }
2134#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2135
Ronald Cron50972942020-11-14 11:28:25 +01002136 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002137 {
2138 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002139 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002140 if( status != PSA_SUCCESS )
2141 *key = MBEDTLS_SVC_KEY_ID_INIT;
2142 }
Ronald Cron50972942020-11-14 11:28:25 +01002143
Gilles Peskine4747d192019-04-17 15:05:45 +02002144 return( status );
2145}
2146
2147/** Abort the creation of a key.
2148 *
2149 * You may call this function after calling psa_start_key_creation(),
2150 * or after psa_finish_key_creation() fails. In other circumstances, this
2151 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002152 * See the documentation of psa_start_key_creation() for the intended use
2153 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002154 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002155 * \param[in,out] slot Pointer to the slot with key material.
2156 * \param[in] driver The secure element driver for the key,
2157 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002158 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002159static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002160 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002161{
Gilles Peskine011e4282019-06-26 18:34:38 +02002162 (void) driver;
2163
Gilles Peskine4747d192019-04-17 15:05:45 +02002164 if( slot == NULL )
2165 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002166
2167#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002168 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002169 * element, and the failure happened later (when saving metadata
2170 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002171 * element.
2172 * https://github.com/ARMmbed/mbed-crypto/issues/217
2173 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002174
Gilles Peskined7729582019-08-05 15:55:54 +02002175 /* Abort the ongoing transaction if any (there may not be one if
2176 * the creation process failed before starting one, or if the
2177 * key creation is a registration of a key in a secure element).
2178 * Earlier functions must already have done what it takes to undo any
2179 * partial creation. All that's left is to update the transaction data
2180 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002181 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002182#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2183
Gilles Peskine4747d192019-04-17 15:05:45 +02002184 psa_wipe_key_slot( slot );
2185}
2186
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002187/** Validate optional attributes during key creation.
2188 *
2189 * Some key attributes are optional during key creation. If they are
2190 * specified in the attributes structure, check that they are consistent
2191 * with the data in the slot.
2192 *
2193 * This function should be called near the end of key creation, after
2194 * the slot in memory is fully populated but before saving persistent data.
2195 */
2196static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002197 const psa_key_slot_t *slot,
2198 const psa_key_attributes_t *attributes )
2199{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002200 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002201 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002202 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002203 return( PSA_ERROR_INVALID_ARGUMENT );
2204 }
2205
2206 if( attributes->domain_parameters_size != 0 )
2207 {
John Durkop0e005192020-10-31 22:06:54 -07002208#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2209 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002210 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002211 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002212 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002213 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002215
Steven Cooreman7f391872020-07-30 14:57:44 +02002216 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002217 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002218 slot->data.key.data,
2219 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002220 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002221 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002222 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002223
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002224 mbedtls_mpi_init( &actual );
2225 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002226 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002227 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002228 mbedtls_rsa_free( rsa );
2229 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002230 if( ret != 0 )
2231 goto rsa_exit;
2232 ret = mbedtls_mpi_read_binary( &required,
2233 attributes->domain_parameters,
2234 attributes->domain_parameters_size );
2235 if( ret != 0 )
2236 goto rsa_exit;
2237 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2238 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2239 rsa_exit:
2240 mbedtls_mpi_free( &actual );
2241 mbedtls_mpi_free( &required );
2242 if( ret != 0)
2243 return( mbedtls_to_psa_error( ret ) );
2244 }
2245 else
John Durkop9814fa22020-11-04 12:28:15 -08002246#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2247 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002248 {
2249 return( PSA_ERROR_INVALID_ARGUMENT );
2250 }
2251 }
2252
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002253 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002254 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002255 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002256 return( PSA_ERROR_INVALID_ARGUMENT );
2257 }
2258
2259 return( PSA_SUCCESS );
2260}
2261
Gilles Peskine4747d192019-04-17 15:05:45 +02002262psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002263 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002264 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002265 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002266{
2267 psa_status_t status;
2268 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002269 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002270
Ronald Cron81709fc2020-11-14 12:10:32 +01002271 *key = MBEDTLS_SVC_KEY_ID_INIT;
2272
Gilles Peskine0f84d622019-09-12 19:03:13 +02002273 /* Reject zero-length symmetric keys (including raw data key objects).
2274 * This also rejects any key which might be encoded as an empty string,
2275 * which is never valid. */
2276 if( data_length == 0 )
2277 return( PSA_ERROR_INVALID_ARGUMENT );
2278
Gilles Peskinedf179142019-07-15 22:02:14 +02002279 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002280 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002281 if( status != PSA_SUCCESS )
2282 goto exit;
2283
Gilles Peskine5d309672019-07-12 23:47:28 +02002284#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2285 if( driver != NULL )
2286 {
2287 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002288 /* The driver should set the number of key bits, however in
2289 * case it doesn't, we initialize bits to an invalid value. */
2290 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002291 if( drv->key_management == NULL ||
2292 drv->key_management->p_import == NULL )
2293 {
2294 status = PSA_ERROR_NOT_SUPPORTED;
2295 goto exit;
2296 }
2297 status = drv->key_management->p_import(
2298 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002299 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002300 &bits );
2301 if( status != PSA_SUCCESS )
2302 goto exit;
2303 if( bits > PSA_MAX_KEY_BITS )
2304 {
2305 status = PSA_ERROR_NOT_SUPPORTED;
2306 goto exit;
2307 }
2308 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002309 }
2310 else
2311#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002312 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2313 {
2314 /* Importing a key with external lifetime through the driver wrapper
2315 * interface is not yet supported. Return as if this was an invalid
2316 * lifetime. */
2317 status = PSA_ERROR_INVALID_ARGUMENT;
2318 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002319 }
2320 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002321 {
2322 status = psa_import_key_into_slot( slot, data, data_length );
2323 if( status != PSA_SUCCESS )
2324 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002325 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002326 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002327 if( status != PSA_SUCCESS )
2328 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002329
Ronald Cron81709fc2020-11-14 12:10:32 +01002330 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002331exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002332 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002333 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002334
Gilles Peskine4747d192019-04-17 15:05:45 +02002335 return( status );
2336}
2337
Gilles Peskined7729582019-08-05 15:55:54 +02002338#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2339psa_status_t mbedtls_psa_register_se_key(
2340 const psa_key_attributes_t *attributes )
2341{
2342 psa_status_t status;
2343 psa_key_slot_t *slot = NULL;
2344 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002346
2347 /* Leaving attributes unspecified is not currently supported.
2348 * It could make sense to query the key type and size from the
2349 * secure element, but not all secure elements support this
2350 * and the driver HAL doesn't currently support it. */
2351 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2352 return( PSA_ERROR_NOT_SUPPORTED );
2353 if( psa_get_key_bits( attributes ) == 0 )
2354 return( PSA_ERROR_NOT_SUPPORTED );
2355
2356 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002357 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002358 if( status != PSA_SUCCESS )
2359 goto exit;
2360
Ronald Cron81709fc2020-11-14 12:10:32 +01002361 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002362
2363exit:
2364 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002365 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002366
Gilles Peskined7729582019-08-05 15:55:54 +02002367 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002368 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002369 return( status );
2370}
2371#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2372
Gilles Peskinef603c712019-01-19 13:40:11 +01002373static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002374 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002375{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002376 psa_status_t status = psa_copy_key_material_into_slot( target,
2377 source->data.key.data,
2378 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002379 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002380 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002381
Steven Cooreman398aee52020-10-13 14:35:45 +02002382 target->attr.type = source->attr.type;
2383 target->attr.bits = source->attr.bits;
2384
2385 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002386}
2387
Ronald Croncf56a0a2020-08-04 09:51:30 +02002388psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002389 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002390 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002391{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002392 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002393 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002394 psa_key_slot_t *source_slot = NULL;
2395 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002396 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002397 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002398
Ronald Cron81709fc2020-11-14 12:10:32 +01002399 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2400
Ronald Cron5c522922020-11-14 16:35:34 +01002401 status = psa_get_and_lock_transparent_key_slot_with_policy(
2402 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002403 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002404 goto exit;
2405
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002406 status = psa_validate_optional_attributes( source_slot,
2407 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002408 if( status != PSA_SUCCESS )
2409 goto exit;
2410
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002411 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002412 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002413 if( status != PSA_SUCCESS )
2414 goto exit;
2415
Ronald Cron81709fc2020-11-14 12:10:32 +01002416 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2417 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002418 if( status != PSA_SUCCESS )
2419 goto exit;
2420
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002421#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2422 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002423 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002424 /* Copying to a secure element is not implemented yet. */
2425 status = PSA_ERROR_NOT_SUPPORTED;
2426 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002427 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002428#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002429
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002430 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002431 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002432 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002433
Ronald Cron81709fc2020-11-14 12:10:32 +01002434 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002435exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002436 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002437 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002438
Ronald Cron5c522922020-11-14 16:35:34 +01002439 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002440
Ronald Cron5c522922020-11-14 16:35:34 +01002441 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002442}
2443
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002444
2445
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002446/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002447/* Message digests */
2448/****************************************************************/
2449
John Durkop07cc04a2020-11-16 22:08:34 -08002450#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002451 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2452 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002453 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002454static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002455{
2456 switch( alg )
2457 {
John Durkopee4e6602020-11-27 08:48:46 -08002458#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002459 case PSA_ALG_MD2:
2460 return( &mbedtls_md2_info );
2461#endif
John Durkopee4e6602020-11-27 08:48:46 -08002462#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002463 case PSA_ALG_MD4:
2464 return( &mbedtls_md4_info );
2465#endif
John Durkopee4e6602020-11-27 08:48:46 -08002466#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002467 case PSA_ALG_MD5:
2468 return( &mbedtls_md5_info );
2469#endif
John Durkopee4e6602020-11-27 08:48:46 -08002470#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002471 case PSA_ALG_RIPEMD160:
2472 return( &mbedtls_ripemd160_info );
2473#endif
John Durkopee4e6602020-11-27 08:48:46 -08002474#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002475 case PSA_ALG_SHA_1:
2476 return( &mbedtls_sha1_info );
2477#endif
John Durkopee4e6602020-11-27 08:48:46 -08002478#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002479 case PSA_ALG_SHA_224:
2480 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002481#endif
2482#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002483 case PSA_ALG_SHA_256:
2484 return( &mbedtls_sha256_info );
2485#endif
John Durkopee4e6602020-11-27 08:48:46 -08002486#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002487 case PSA_ALG_SHA_384:
2488 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002489#endif
John Durkopee4e6602020-11-27 08:48:46 -08002490#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002491 case PSA_ALG_SHA_512:
2492 return( &mbedtls_sha512_info );
2493#endif
2494 default:
2495 return( NULL );
2496 }
2497}
John Durkop07cc04a2020-11-16 22:08:34 -08002498#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002499 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2500 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2501 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002502
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2504{
2505 switch( operation->alg )
2506 {
Gilles Peskine81736312018-06-26 15:04:31 +02002507 case 0:
2508 /* The object has (apparently) been initialized but it is not
2509 * in use. It's ok to call abort on such an object, and there's
2510 * nothing to do. */
2511 break;
John Durkopee4e6602020-11-27 08:48:46 -08002512#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002513 case PSA_ALG_MD2:
2514 mbedtls_md2_free( &operation->ctx.md2 );
2515 break;
2516#endif
John Durkopee4e6602020-11-27 08:48:46 -08002517#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002518 case PSA_ALG_MD4:
2519 mbedtls_md4_free( &operation->ctx.md4 );
2520 break;
2521#endif
John Durkopee4e6602020-11-27 08:48:46 -08002522#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002523 case PSA_ALG_MD5:
2524 mbedtls_md5_free( &operation->ctx.md5 );
2525 break;
2526#endif
John Durkopee4e6602020-11-27 08:48:46 -08002527#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002528 case PSA_ALG_RIPEMD160:
2529 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2530 break;
2531#endif
John Durkopee4e6602020-11-27 08:48:46 -08002532#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002533 case PSA_ALG_SHA_1:
2534 mbedtls_sha1_free( &operation->ctx.sha1 );
2535 break;
2536#endif
John Durkop6ca23272020-12-03 06:01:32 -08002537#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002538 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002539 mbedtls_sha256_free( &operation->ctx.sha256 );
2540 break;
2541#endif
2542#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002543 case PSA_ALG_SHA_256:
2544 mbedtls_sha256_free( &operation->ctx.sha256 );
2545 break;
2546#endif
John Durkop6ca23272020-12-03 06:01:32 -08002547#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002548 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002549 mbedtls_sha512_free( &operation->ctx.sha512 );
2550 break;
2551#endif
2552#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002553 case PSA_ALG_SHA_512:
2554 mbedtls_sha512_free( &operation->ctx.sha512 );
2555 break;
2556#endif
2557 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002558 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002559 }
2560 operation->alg = 0;
2561 return( PSA_SUCCESS );
2562}
2563
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002564psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002565 psa_algorithm_t alg )
2566{
Janos Follath24eed8d2019-11-22 13:21:35 +00002567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002568
2569 /* A context must be freshly initialized before it can be set up. */
2570 if( operation->alg != 0 )
2571 {
2572 return( PSA_ERROR_BAD_STATE );
2573 }
2574
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002575 switch( alg )
2576 {
John Durkopee4e6602020-11-27 08:48:46 -08002577#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002578 case PSA_ALG_MD2:
2579 mbedtls_md2_init( &operation->ctx.md2 );
2580 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2581 break;
2582#endif
John Durkopee4e6602020-11-27 08:48:46 -08002583#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002584 case PSA_ALG_MD4:
2585 mbedtls_md4_init( &operation->ctx.md4 );
2586 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2587 break;
2588#endif
John Durkopee4e6602020-11-27 08:48:46 -08002589#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002590 case PSA_ALG_MD5:
2591 mbedtls_md5_init( &operation->ctx.md5 );
2592 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2593 break;
2594#endif
John Durkopee4e6602020-11-27 08:48:46 -08002595#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002596 case PSA_ALG_RIPEMD160:
2597 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2598 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2599 break;
2600#endif
John Durkopee4e6602020-11-27 08:48:46 -08002601#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002602 case PSA_ALG_SHA_1:
2603 mbedtls_sha1_init( &operation->ctx.sha1 );
2604 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2605 break;
2606#endif
John Durkopee4e6602020-11-27 08:48:46 -08002607#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002608 case PSA_ALG_SHA_224:
2609 mbedtls_sha256_init( &operation->ctx.sha256 );
2610 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2611 break;
John Durkopee4e6602020-11-27 08:48:46 -08002612#endif
2613#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002614 case PSA_ALG_SHA_256:
2615 mbedtls_sha256_init( &operation->ctx.sha256 );
2616 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2617 break;
2618#endif
John Durkopee4e6602020-11-27 08:48:46 -08002619#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002620 case PSA_ALG_SHA_384:
2621 mbedtls_sha512_init( &operation->ctx.sha512 );
2622 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2623 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002624#endif
John Durkopee4e6602020-11-27 08:48:46 -08002625#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002626 case PSA_ALG_SHA_512:
2627 mbedtls_sha512_init( &operation->ctx.sha512 );
2628 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2629 break;
2630#endif
2631 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002632 return( PSA_ALG_IS_HASH( alg ) ?
2633 PSA_ERROR_NOT_SUPPORTED :
2634 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002635 }
2636 if( ret == 0 )
2637 operation->alg = alg;
2638 else
2639 psa_hash_abort( operation );
2640 return( mbedtls_to_psa_error( ret ) );
2641}
2642
2643psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2644 const uint8_t *input,
2645 size_t input_length )
2646{
Janos Follath24eed8d2019-11-22 13:21:35 +00002647 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002648
2649 /* Don't require hash implementations to behave correctly on a
2650 * zero-length input, which may have an invalid pointer. */
2651 if( input_length == 0 )
2652 return( PSA_SUCCESS );
2653
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002654 switch( operation->alg )
2655 {
John Durkopee4e6602020-11-27 08:48:46 -08002656#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002657 case PSA_ALG_MD2:
2658 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2659 input, input_length );
2660 break;
2661#endif
John Durkopee4e6602020-11-27 08:48:46 -08002662#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002663 case PSA_ALG_MD4:
2664 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2665 input, input_length );
2666 break;
2667#endif
John Durkopee4e6602020-11-27 08:48:46 -08002668#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002669 case PSA_ALG_MD5:
2670 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2671 input, input_length );
2672 break;
2673#endif
John Durkopee4e6602020-11-27 08:48:46 -08002674#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002675 case PSA_ALG_RIPEMD160:
2676 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2677 input, input_length );
2678 break;
2679#endif
John Durkopee4e6602020-11-27 08:48:46 -08002680#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002681 case PSA_ALG_SHA_1:
2682 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2683 input, input_length );
2684 break;
2685#endif
John Durkop6ca23272020-12-03 06:01:32 -08002686#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002687 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002688 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2689 input, input_length );
2690 break;
2691#endif
2692#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002693 case PSA_ALG_SHA_256:
2694 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2695 input, input_length );
2696 break;
2697#endif
John Durkop6ca23272020-12-03 06:01:32 -08002698#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002699 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002700 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2701 input, input_length );
2702 break;
2703#endif
2704#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002705 case PSA_ALG_SHA_512:
2706 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2707 input, input_length );
2708 break;
2709#endif
2710 default:
John Durkopee4e6602020-11-27 08:48:46 -08002711 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002712 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002713 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002714
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002715 if( ret != 0 )
2716 psa_hash_abort( operation );
2717 return( mbedtls_to_psa_error( ret ) );
2718}
2719
2720psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2721 uint8_t *hash,
2722 size_t hash_size,
2723 size_t *hash_length )
2724{
itayzafrir40835d42018-08-02 13:14:17 +03002725 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002727 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002728
2729 /* Fill the output buffer with something that isn't a valid hash
2730 * (barring an attack on the hash and deliberately-crafted input),
2731 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002732 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002733 /* If hash_size is 0 then hash may be NULL and then the
2734 * call to memset would have undefined behavior. */
2735 if( hash_size != 0 )
2736 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002737
2738 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002739 {
2740 status = PSA_ERROR_BUFFER_TOO_SMALL;
2741 goto exit;
2742 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002743
2744 switch( operation->alg )
2745 {
John Durkopee4e6602020-11-27 08:48:46 -08002746#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002747 case PSA_ALG_MD2:
2748 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2749 break;
2750#endif
John Durkopee4e6602020-11-27 08:48:46 -08002751#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002752 case PSA_ALG_MD4:
2753 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2754 break;
2755#endif
John Durkopee4e6602020-11-27 08:48:46 -08002756#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002757 case PSA_ALG_MD5:
2758 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2759 break;
2760#endif
John Durkopee4e6602020-11-27 08:48:46 -08002761#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002762 case PSA_ALG_RIPEMD160:
2763 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2764 break;
2765#endif
John Durkopee4e6602020-11-27 08:48:46 -08002766#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002767 case PSA_ALG_SHA_1:
2768 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2769 break;
2770#endif
John Durkop6ca23272020-12-03 06:01:32 -08002771#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002772 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002773 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2774 break;
2775#endif
2776#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002777 case PSA_ALG_SHA_256:
2778 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2779 break;
2780#endif
John Durkop6ca23272020-12-03 06:01:32 -08002781#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002782 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002783 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2784 break;
2785#endif
2786#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002787 case PSA_ALG_SHA_512:
2788 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2789 break;
2790#endif
2791 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002792 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002793 }
itayzafrir40835d42018-08-02 13:14:17 +03002794 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002795
itayzafrir40835d42018-08-02 13:14:17 +03002796exit:
2797 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002798 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002799 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002800 return( psa_hash_abort( operation ) );
2801 }
2802 else
2803 {
2804 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002805 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002806 }
2807}
2808
Gilles Peskine2d277862018-06-18 15:41:12 +02002809psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2810 const uint8_t *hash,
2811 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002812{
2813 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2814 size_t actual_hash_length;
2815 psa_status_t status = psa_hash_finish( operation,
2816 actual_hash, sizeof( actual_hash ),
2817 &actual_hash_length );
2818 if( status != PSA_SUCCESS )
2819 return( status );
2820 if( actual_hash_length != hash_length )
2821 return( PSA_ERROR_INVALID_SIGNATURE );
2822 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2823 return( PSA_ERROR_INVALID_SIGNATURE );
2824 return( PSA_SUCCESS );
2825}
2826
Gilles Peskine0a749c82019-11-28 19:33:58 +01002827psa_status_t psa_hash_compute( psa_algorithm_t alg,
2828 const uint8_t *input, size_t input_length,
2829 uint8_t *hash, size_t hash_size,
2830 size_t *hash_length )
2831{
2832 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2833 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2834
2835 *hash_length = hash_size;
2836 status = psa_hash_setup( &operation, alg );
2837 if( status != PSA_SUCCESS )
2838 goto exit;
2839 status = psa_hash_update( &operation, input, input_length );
2840 if( status != PSA_SUCCESS )
2841 goto exit;
2842 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2843 if( status != PSA_SUCCESS )
2844 goto exit;
2845
2846exit:
2847 if( status == PSA_SUCCESS )
2848 status = psa_hash_abort( &operation );
2849 else
2850 psa_hash_abort( &operation );
2851 return( status );
2852}
2853
2854psa_status_t psa_hash_compare( psa_algorithm_t alg,
2855 const uint8_t *input, size_t input_length,
2856 const uint8_t *hash, size_t hash_length )
2857{
2858 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2859 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2860
2861 status = psa_hash_setup( &operation, alg );
2862 if( status != PSA_SUCCESS )
2863 goto exit;
2864 status = psa_hash_update( &operation, input, input_length );
2865 if( status != PSA_SUCCESS )
2866 goto exit;
2867 status = psa_hash_verify( &operation, hash, hash_length );
2868 if( status != PSA_SUCCESS )
2869 goto exit;
2870
2871exit:
2872 if( status == PSA_SUCCESS )
2873 status = psa_hash_abort( &operation );
2874 else
2875 psa_hash_abort( &operation );
2876 return( status );
2877}
2878
Gilles Peskineeb35d782019-01-22 17:56:16 +01002879psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2880 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002881{
2882 if( target_operation->alg != 0 )
2883 return( PSA_ERROR_BAD_STATE );
2884
2885 switch( source_operation->alg )
2886 {
2887 case 0:
2888 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002889#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002890 case PSA_ALG_MD2:
2891 mbedtls_md2_clone( &target_operation->ctx.md2,
2892 &source_operation->ctx.md2 );
2893 break;
2894#endif
John Durkopee4e6602020-11-27 08:48:46 -08002895#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002896 case PSA_ALG_MD4:
2897 mbedtls_md4_clone( &target_operation->ctx.md4,
2898 &source_operation->ctx.md4 );
2899 break;
2900#endif
John Durkopee4e6602020-11-27 08:48:46 -08002901#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002902 case PSA_ALG_MD5:
2903 mbedtls_md5_clone( &target_operation->ctx.md5,
2904 &source_operation->ctx.md5 );
2905 break;
2906#endif
John Durkopee4e6602020-11-27 08:48:46 -08002907#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002908 case PSA_ALG_RIPEMD160:
2909 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2910 &source_operation->ctx.ripemd160 );
2911 break;
2912#endif
John Durkopee4e6602020-11-27 08:48:46 -08002913#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002914 case PSA_ALG_SHA_1:
2915 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2916 &source_operation->ctx.sha1 );
2917 break;
2918#endif
John Durkop6ca23272020-12-03 06:01:32 -08002919#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002920 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002921 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2922 &source_operation->ctx.sha256 );
2923 break;
2924#endif
2925#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002926 case PSA_ALG_SHA_256:
2927 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2928 &source_operation->ctx.sha256 );
2929 break;
2930#endif
John Durkop6ca23272020-12-03 06:01:32 -08002931#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002932 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002933 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2934 &source_operation->ctx.sha512 );
2935 break;
2936#endif
2937#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002938 case PSA_ALG_SHA_512:
2939 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2940 &source_operation->ctx.sha512 );
2941 break;
2942#endif
2943 default:
2944 return( PSA_ERROR_NOT_SUPPORTED );
2945 }
2946
2947 target_operation->alg = source_operation->alg;
2948 return( PSA_SUCCESS );
2949}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002950
2951
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002952/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953/* MAC */
2954/****************************************************************/
2955
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002956static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957 psa_algorithm_t alg,
2958 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002959 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002960 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002961{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002963 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002964
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002965 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002966 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002967
Gilles Peskine8c9def32018-02-08 10:02:12 +01002968 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2969 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002970 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002971 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002972 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002973 mode = MBEDTLS_MODE_STREAM;
2974 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002975 case PSA_ALG_CTR:
2976 mode = MBEDTLS_MODE_CTR;
2977 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002978 case PSA_ALG_CFB:
2979 mode = MBEDTLS_MODE_CFB;
2980 break;
2981 case PSA_ALG_OFB:
2982 mode = MBEDTLS_MODE_OFB;
2983 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002984 case PSA_ALG_ECB_NO_PADDING:
2985 mode = MBEDTLS_MODE_ECB;
2986 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002987 case PSA_ALG_CBC_NO_PADDING:
2988 mode = MBEDTLS_MODE_CBC;
2989 break;
2990 case PSA_ALG_CBC_PKCS7:
2991 mode = MBEDTLS_MODE_CBC;
2992 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002993 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002994 mode = MBEDTLS_MODE_CCM;
2995 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002996 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997 mode = MBEDTLS_MODE_GCM;
2998 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002999 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3000 mode = MBEDTLS_MODE_CHACHAPOLY;
3001 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002 default:
3003 return( NULL );
3004 }
3005 }
3006 else if( alg == PSA_ALG_CMAC )
3007 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008 else
3009 return( NULL );
3010
3011 switch( key_type )
3012 {
3013 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003014 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015 break;
3016 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003017 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3018 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003019 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003020 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003022 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003023 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3024 * but two-key Triple-DES is functionally three-key Triple-DES
3025 * with K1=K3, so that's how we present it to mbedtls. */
3026 if( key_bits == 128 )
3027 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028 break;
3029 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003030 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003031 break;
3032 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003033 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003035 case PSA_KEY_TYPE_CHACHA20:
3036 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3037 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003038 default:
3039 return( NULL );
3040 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003041 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003042 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043
Jaeden Amero23bbb752018-06-26 14:16:54 +01003044 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3045 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003046}
3047
John Durkop6ba40d12020-11-10 08:50:04 -08003048#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003049static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003050{
Gilles Peskine2d277862018-06-18 15:41:12 +02003051 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003052 {
3053 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003054 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003055 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003056 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003057 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003058 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003059 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003060 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003061 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003062 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003063 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003064 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003065 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003066 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003067 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003068 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003069 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003070 return( 128 );
3071 default:
3072 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003073 }
3074}
John Durkop07cc04a2020-11-16 22:08:34 -08003075#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003076
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003077/* Initialize the MAC operation structure. Once this function has been
3078 * called, psa_mac_abort can run and will do the right thing. */
3079static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3080 psa_algorithm_t alg )
3081{
3082 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3083
3084 operation->alg = alg;
3085 operation->key_set = 0;
3086 operation->iv_set = 0;
3087 operation->iv_required = 0;
3088 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003089 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003090
3091#if defined(MBEDTLS_CMAC_C)
3092 if( alg == PSA_ALG_CMAC )
3093 {
3094 operation->iv_required = 0;
3095 mbedtls_cipher_init( &operation->ctx.cmac );
3096 status = PSA_SUCCESS;
3097 }
3098 else
3099#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003100#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003101 if( PSA_ALG_IS_HMAC( operation->alg ) )
3102 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003103 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3104 operation->ctx.hmac.hash_ctx.alg = 0;
3105 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003106 }
3107 else
John Durkopd0321952020-10-29 21:37:36 -07003108#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003109 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003110 if( ! PSA_ALG_IS_MAC( alg ) )
3111 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003112 }
3113
3114 if( status != PSA_SUCCESS )
3115 memset( operation, 0, sizeof( *operation ) );
3116 return( status );
3117}
3118
John Durkop6ba40d12020-11-10 08:50:04 -08003119#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003120static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3121{
Gilles Peskine3f108122018-12-07 18:14:53 +01003122 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003123 return( psa_hash_abort( &hmac->hash_ctx ) );
3124}
John Durkop6ba40d12020-11-10 08:50:04 -08003125#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003126
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3128{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003129 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003130 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003131 /* The object has (apparently) been initialized but it is not
3132 * in use. It's ok to call abort on such an object, and there's
3133 * nothing to do. */
3134 return( PSA_SUCCESS );
3135 }
3136 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003137#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003138 if( operation->alg == PSA_ALG_CMAC )
3139 {
3140 mbedtls_cipher_free( &operation->ctx.cmac );
3141 }
3142 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003143#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003144#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003145 if( PSA_ALG_IS_HMAC( operation->alg ) )
3146 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003147 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003148 }
3149 else
John Durkopd0321952020-10-29 21:37:36 -07003150#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003151 {
3152 /* Sanity check (shouldn't happen: operation->alg should
3153 * always have been initialized to a valid value). */
3154 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003155 }
Moran Peker41deec42018-04-04 15:43:05 +03003156
Gilles Peskine8c9def32018-02-08 10:02:12 +01003157 operation->alg = 0;
3158 operation->key_set = 0;
3159 operation->iv_set = 0;
3160 operation->iv_required = 0;
3161 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003162 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003163
Gilles Peskine8c9def32018-02-08 10:02:12 +01003164 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003165
3166bad_state:
3167 /* If abort is called on an uninitialized object, we can't trust
3168 * anything. Wipe the object in case it contains confidential data.
3169 * This may result in a memory leak if a pointer gets overwritten,
3170 * but it's too late to do anything about this. */
3171 memset( operation, 0, sizeof( *operation ) );
3172 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003173}
3174
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003175#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003176static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003177 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003178 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003179 const mbedtls_cipher_info_t *cipher_info )
3180{
Janos Follath24eed8d2019-11-22 13:21:35 +00003181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003182
3183 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003184
3185 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3186 if( ret != 0 )
3187 return( ret );
3188
3189 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003190 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003191 key_bits );
3192 return( ret );
3193}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003194#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003195
John Durkop6ba40d12020-11-10 08:50:04 -08003196#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003197static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3198 const uint8_t *key,
3199 size_t key_length,
3200 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003201{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003202 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003203 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003204 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003205 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003206 psa_status_t status;
3207
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003208 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3209 * overflow below. This should never trigger if the hash algorithm
3210 * is implemented correctly. */
3211 /* The size checks against the ipad and opad buffers cannot be written
3212 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3213 * because that triggers -Wlogical-op on GCC 7.3. */
3214 if( block_size > sizeof( ipad ) )
3215 return( PSA_ERROR_NOT_SUPPORTED );
3216 if( block_size > sizeof( hmac->opad ) )
3217 return( PSA_ERROR_NOT_SUPPORTED );
3218 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003219 return( PSA_ERROR_NOT_SUPPORTED );
3220
Gilles Peskined223b522018-06-11 18:12:58 +02003221 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003222 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003223 status = psa_hash_compute( hash_alg, key, key_length,
3224 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003225 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003226 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003227 }
Gilles Peskine96889972018-07-12 17:07:03 +02003228 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3229 * but it is permitted. It is common when HMAC is used in HKDF, for
3230 * example. Don't call `memcpy` in the 0-length because `key` could be
3231 * an invalid pointer which would make the behavior undefined. */
3232 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003233 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003234
Gilles Peskined223b522018-06-11 18:12:58 +02003235 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3236 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003237 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003238 ipad[i] ^= 0x36;
3239 memset( ipad + key_length, 0x36, block_size - key_length );
3240
3241 /* Copy the key material from ipad to opad, flipping the requisite bits,
3242 * and filling the rest of opad with the requisite constant. */
3243 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003244 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3245 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003246
Gilles Peskine01126fa2018-07-12 17:04:55 +02003247 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003248 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003249 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003250
Gilles Peskine01126fa2018-07-12 17:04:55 +02003251 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003252
3253cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003254 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003255
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003256 return( status );
3257}
John Durkop6ba40d12020-11-10 08:50:04 -08003258#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003259
Gilles Peskine89167cb2018-07-08 20:12:23 +02003260static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003261 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003262 psa_algorithm_t alg,
3263 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003264{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003265 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003266 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003267 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003268 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003269 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003270 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003271 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003272 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003273
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003274 /* A context must be freshly initialized before it can be set up. */
3275 if( operation->alg != 0 )
3276 {
3277 return( PSA_ERROR_BAD_STATE );
3278 }
3279
Gilles Peskined911eb72018-08-14 15:18:45 +02003280 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003281 if( status != PSA_SUCCESS )
3282 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003283 if( is_sign )
3284 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003285
Ronald Cron5c522922020-11-14 16:35:34 +01003286 status = psa_get_and_lock_transparent_key_slot_with_policy(
3287 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003288 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003289 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003290 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003291
Gilles Peskine8c9def32018-02-08 10:02:12 +01003292#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003293 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003294 {
3295 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003296 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003297 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003299 if( cipher_info == NULL )
3300 {
3301 status = PSA_ERROR_NOT_SUPPORTED;
3302 goto exit;
3303 }
3304 operation->mac_size = cipher_info->block_size;
3305 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3306 status = mbedtls_to_psa_error( ret );
3307 }
3308 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003309#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003310#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003311 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003312 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003313 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003314 if( hash_alg == 0 )
3315 {
3316 status = PSA_ERROR_NOT_SUPPORTED;
3317 goto exit;
3318 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003319
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003320 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003321 /* Sanity check. This shouldn't fail on a valid configuration. */
3322 if( operation->mac_size == 0 ||
3323 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3324 {
3325 status = PSA_ERROR_NOT_SUPPORTED;
3326 goto exit;
3327 }
3328
Gilles Peskine8e338702019-07-30 20:06:31 +02003329 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003330 {
3331 status = PSA_ERROR_INVALID_ARGUMENT;
3332 goto exit;
3333 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003334
Gilles Peskine01126fa2018-07-12 17:04:55 +02003335 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003336 slot->data.key.data,
3337 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003338 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003339 }
3340 else
John Durkopd0321952020-10-29 21:37:36 -07003341#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003342 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003343 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003344 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003345 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003346
Gilles Peskined911eb72018-08-14 15:18:45 +02003347 if( truncated == 0 )
3348 {
3349 /* The "normal" case: untruncated algorithm. Nothing to do. */
3350 }
3351 else if( truncated < 4 )
3352 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003353 /* A very short MAC is too short for security since it can be
3354 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3355 * so we make this our minimum, even though 32 bits is still
3356 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003357 status = PSA_ERROR_NOT_SUPPORTED;
3358 }
3359 else if( truncated > operation->mac_size )
3360 {
3361 /* It's impossible to "truncate" to a larger length. */
3362 status = PSA_ERROR_INVALID_ARGUMENT;
3363 }
3364 else
3365 operation->mac_size = truncated;
3366
Gilles Peskinefbfac682018-07-08 20:51:54 +02003367exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003368 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003369 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003370 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003371 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003372 else
3373 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003374 operation->key_set = 1;
3375 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003376
Ronald Cron5c522922020-11-14 16:35:34 +01003377 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003378
Ronald Cron5c522922020-11-14 16:35:34 +01003379 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003380}
3381
Gilles Peskine89167cb2018-07-08 20:12:23 +02003382psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003383 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003384 psa_algorithm_t alg )
3385{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003386 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003387}
3388
3389psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003390 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003391 psa_algorithm_t alg )
3392{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003393 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003394}
3395
Gilles Peskine8c9def32018-02-08 10:02:12 +01003396psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3397 const uint8_t *input,
3398 size_t input_length )
3399{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003400 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003401 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003402 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003403 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003404 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003405 operation->has_input = 1;
3406
Gilles Peskine8c9def32018-02-08 10:02:12 +01003407#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003408 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003409 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003410 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3411 input, input_length );
3412 status = mbedtls_to_psa_error( ret );
3413 }
3414 else
3415#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003416#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003417 if( PSA_ALG_IS_HMAC( operation->alg ) )
3418 {
3419 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3420 input_length );
3421 }
3422 else
John Durkopd0321952020-10-29 21:37:36 -07003423#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003424 {
3425 /* This shouldn't happen if `operation` was initialized by
3426 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003427 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003428 }
3429
Gilles Peskinefbfac682018-07-08 20:51:54 +02003430 if( status != PSA_SUCCESS )
3431 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003432 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003433}
3434
John Durkop6ba40d12020-11-10 08:50:04 -08003435#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003436static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3437 uint8_t *mac,
3438 size_t mac_size )
3439{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003440 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003441 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3442 size_t hash_size = 0;
3443 size_t block_size = psa_get_hash_block_size( hash_alg );
3444 psa_status_t status;
3445
Gilles Peskine01126fa2018-07-12 17:04:55 +02003446 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3447 if( status != PSA_SUCCESS )
3448 return( status );
3449 /* From here on, tmp needs to be wiped. */
3450
3451 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3452 if( status != PSA_SUCCESS )
3453 goto exit;
3454
3455 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3456 if( status != PSA_SUCCESS )
3457 goto exit;
3458
3459 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3460 if( status != PSA_SUCCESS )
3461 goto exit;
3462
Gilles Peskined911eb72018-08-14 15:18:45 +02003463 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3464 if( status != PSA_SUCCESS )
3465 goto exit;
3466
3467 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003468
3469exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003470 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003471 return( status );
3472}
John Durkop6ba40d12020-11-10 08:50:04 -08003473#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003474
mohammad16036df908f2018-04-02 08:34:15 -07003475static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003476 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003477 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003478{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003479 if( ! operation->key_set )
3480 return( PSA_ERROR_BAD_STATE );
3481 if( operation->iv_required && ! operation->iv_set )
3482 return( PSA_ERROR_BAD_STATE );
3483
Gilles Peskine8c9def32018-02-08 10:02:12 +01003484 if( mac_size < operation->mac_size )
3485 return( PSA_ERROR_BUFFER_TOO_SMALL );
3486
Gilles Peskine8c9def32018-02-08 10:02:12 +01003487#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003488 if( operation->alg == PSA_ALG_CMAC )
3489 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003490 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003491 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3492 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003493 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003494 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003495 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003496 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003497 else
3498#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003499#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003500 if( PSA_ALG_IS_HMAC( operation->alg ) )
3501 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003502 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003503 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003504 }
3505 else
John Durkopd0321952020-10-29 21:37:36 -07003506#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003507 {
3508 /* This shouldn't happen if `operation` was initialized by
3509 * a setup function. */
3510 return( PSA_ERROR_BAD_STATE );
3511 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003512}
3513
Gilles Peskineacd4be32018-07-08 19:56:25 +02003514psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3515 uint8_t *mac,
3516 size_t mac_size,
3517 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003518{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003519 psa_status_t status;
3520
Jaeden Amero252ef282019-02-15 14:05:35 +00003521 if( operation->alg == 0 )
3522 {
3523 return( PSA_ERROR_BAD_STATE );
3524 }
3525
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003526 /* Fill the output buffer with something that isn't a valid mac
3527 * (barring an attack on the mac and deliberately-crafted input),
3528 * in case the caller doesn't check the return status properly. */
3529 *mac_length = mac_size;
3530 /* If mac_size is 0 then mac may be NULL and then the
3531 * call to memset would have undefined behavior. */
3532 if( mac_size != 0 )
3533 memset( mac, '!', mac_size );
3534
Gilles Peskine89167cb2018-07-08 20:12:23 +02003535 if( ! operation->is_sign )
3536 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003537 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003538 }
mohammad16036df908f2018-04-02 08:34:15 -07003539
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003540 status = psa_mac_finish_internal( operation, mac, mac_size );
3541
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003542 if( status == PSA_SUCCESS )
3543 {
3544 status = psa_mac_abort( operation );
3545 if( status == PSA_SUCCESS )
3546 *mac_length = operation->mac_size;
3547 else
3548 memset( mac, '!', mac_size );
3549 }
3550 else
3551 psa_mac_abort( operation );
3552 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003553}
3554
Gilles Peskineacd4be32018-07-08 19:56:25 +02003555psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3556 const uint8_t *mac,
3557 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003558{
Gilles Peskine828ed142018-06-18 23:25:51 +02003559 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003560 psa_status_t status;
3561
Jaeden Amero252ef282019-02-15 14:05:35 +00003562 if( operation->alg == 0 )
3563 {
3564 return( PSA_ERROR_BAD_STATE );
3565 }
3566
Gilles Peskine89167cb2018-07-08 20:12:23 +02003567 if( operation->is_sign )
3568 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003569 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003570 }
3571 if( operation->mac_size != mac_length )
3572 {
3573 status = PSA_ERROR_INVALID_SIGNATURE;
3574 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003575 }
mohammad16036df908f2018-04-02 08:34:15 -07003576
3577 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003578 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003579 if( status != PSA_SUCCESS )
3580 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003581
3582 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3583 status = PSA_ERROR_INVALID_SIGNATURE;
3584
3585cleanup:
3586 if( status == PSA_SUCCESS )
3587 status = psa_mac_abort( operation );
3588 else
3589 psa_mac_abort( operation );
3590
Gilles Peskine3f108122018-12-07 18:14:53 +01003591 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003592
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003593 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003594}
3595
3596
Gilles Peskine20035e32018-02-03 22:44:14 +01003597
Gilles Peskine20035e32018-02-03 22:44:14 +01003598/****************************************************************/
3599/* Asymmetric cryptography */
3600/****************************************************************/
3601
John Durkop6ba40d12020-11-10 08:50:04 -08003602#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3603 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003604/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003605 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003606static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3607 size_t hash_length,
3608 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003609{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003610 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003611 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003612 *md_alg = mbedtls_md_get_type( md_info );
3613
3614 /* The Mbed TLS RSA module uses an unsigned int for hash length
3615 * parameters. Validate that it fits so that we don't risk an
3616 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003617#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003618 if( hash_length > UINT_MAX )
3619 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003620#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003621
John Durkop0e005192020-10-31 22:06:54 -07003622#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003623 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3624 * must be correct. */
3625 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3626 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003627 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003628 if( md_info == NULL )
3629 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003630 if( mbedtls_md_get_size( md_info ) != hash_length )
3631 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003632 }
John Durkop0e005192020-10-31 22:06:54 -07003633#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003634
John Durkop0e005192020-10-31 22:06:54 -07003635#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003636 /* PSS requires a hash internally. */
3637 if( PSA_ALG_IS_RSA_PSS( alg ) )
3638 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003639 if( md_info == NULL )
3640 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003641 }
John Durkop0e005192020-10-31 22:06:54 -07003642#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003643
Gilles Peskine61b91d42018-06-08 16:09:36 +02003644 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003645}
3646
Gilles Peskine2b450e32018-06-27 15:42:46 +02003647static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3648 psa_algorithm_t alg,
3649 const uint8_t *hash,
3650 size_t hash_length,
3651 uint8_t *signature,
3652 size_t signature_size,
3653 size_t *signature_length )
3654{
3655 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003657 mbedtls_md_type_t md_alg;
3658
3659 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3660 if( status != PSA_SUCCESS )
3661 return( status );
3662
Gilles Peskine630a18a2018-06-29 17:49:35 +02003663 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003664 return( PSA_ERROR_BUFFER_TOO_SMALL );
3665
John Durkop0e005192020-10-31 22:06:54 -07003666#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003667 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3668 {
3669 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3670 MBEDTLS_MD_NONE );
3671 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003672 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003673 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003674 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003675 md_alg,
3676 (unsigned int) hash_length,
3677 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003678 signature );
3679 }
3680 else
John Durkop0e005192020-10-31 22:06:54 -07003681#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3682#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003683 if( PSA_ALG_IS_RSA_PSS( alg ) )
3684 {
3685 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3686 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003687 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003688 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003689 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003690 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003691 (unsigned int) hash_length,
3692 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003693 signature );
3694 }
3695 else
John Durkop0e005192020-10-31 22:06:54 -07003696#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003697 {
3698 return( PSA_ERROR_INVALID_ARGUMENT );
3699 }
3700
3701 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003702 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003703 return( mbedtls_to_psa_error( ret ) );
3704}
3705
3706static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3707 psa_algorithm_t alg,
3708 const uint8_t *hash,
3709 size_t hash_length,
3710 const uint8_t *signature,
3711 size_t signature_length )
3712{
3713 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003714 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003715 mbedtls_md_type_t md_alg;
3716
3717 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3718 if( status != PSA_SUCCESS )
3719 return( status );
3720
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003721 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3722 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003723
John Durkop0e005192020-10-31 22:06:54 -07003724#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003725 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3726 {
3727 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3728 MBEDTLS_MD_NONE );
3729 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003730 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003731 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003732 MBEDTLS_RSA_PUBLIC,
3733 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003734 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003735 hash,
3736 signature );
3737 }
3738 else
John Durkop0e005192020-10-31 22:06:54 -07003739#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3740#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003741 if( PSA_ALG_IS_RSA_PSS( alg ) )
3742 {
3743 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3744 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003745 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003746 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003747 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003748 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003749 (unsigned int) hash_length,
3750 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003751 signature );
3752 }
3753 else
John Durkop0e005192020-10-31 22:06:54 -07003754#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003755 {
3756 return( PSA_ERROR_INVALID_ARGUMENT );
3757 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003758
3759 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3760 * the rest of the signature is invalid". This has little use in
3761 * practice and PSA doesn't report this distinction. */
3762 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3763 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003764 return( mbedtls_to_psa_error( ret ) );
3765}
John Durkop6ba40d12020-11-10 08:50:04 -08003766#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3767 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003768
John Durkop6ba40d12020-11-10 08:50:04 -08003769#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3770 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003771/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3772 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3773 * (even though these functions don't modify it). */
3774static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3775 psa_algorithm_t alg,
3776 const uint8_t *hash,
3777 size_t hash_length,
3778 uint8_t *signature,
3779 size_t signature_size,
3780 size_t *signature_length )
3781{
Janos Follath24eed8d2019-11-22 13:21:35 +00003782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003783 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003784 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003785 mbedtls_mpi_init( &r );
3786 mbedtls_mpi_init( &s );
3787
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003788 if( signature_size < 2 * curve_bytes )
3789 {
3790 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3791 goto cleanup;
3792 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003793
John Durkop0ea39e02020-10-13 19:58:20 -07003794#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003795 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3796 {
3797 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3798 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3799 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003800 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3801 &ecp->d, hash,
3802 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003803 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003804 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003805 }
3806 else
John Durkop0ea39e02020-10-13 19:58:20 -07003807#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003808 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003809 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003810 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3811 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003812 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003813 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003814 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003815
3816 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3817 signature,
3818 curve_bytes ) );
3819 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3820 signature + curve_bytes,
3821 curve_bytes ) );
3822
3823cleanup:
3824 mbedtls_mpi_free( &r );
3825 mbedtls_mpi_free( &s );
3826 if( ret == 0 )
3827 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003828 return( mbedtls_to_psa_error( ret ) );
3829}
3830
3831static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3832 const uint8_t *hash,
3833 size_t hash_length,
3834 const uint8_t *signature,
3835 size_t signature_length )
3836{
Janos Follath24eed8d2019-11-22 13:21:35 +00003837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003838 mbedtls_mpi r, s;
3839 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3840 mbedtls_mpi_init( &r );
3841 mbedtls_mpi_init( &s );
3842
3843 if( signature_length != 2 * curve_bytes )
3844 return( PSA_ERROR_INVALID_SIGNATURE );
3845
3846 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3847 signature,
3848 curve_bytes ) );
3849 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3850 signature + curve_bytes,
3851 curve_bytes ) );
3852
Steven Cooremanacda8342020-07-24 23:09:52 +02003853 /* Check whether the public part is loaded. If not, load it. */
3854 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3855 {
3856 MBEDTLS_MPI_CHK(
3857 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003858 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003859 }
3860
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003861 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3862 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003863
3864cleanup:
3865 mbedtls_mpi_free( &r );
3866 mbedtls_mpi_free( &s );
3867 return( mbedtls_to_psa_error( ret ) );
3868}
John Durkop6ba40d12020-11-10 08:50:04 -08003869#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3870 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003871
Ronald Croncf56a0a2020-08-04 09:51:30 +02003872psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003873 psa_algorithm_t alg,
3874 const uint8_t *hash,
3875 size_t hash_length,
3876 uint8_t *signature,
3877 size_t signature_size,
3878 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003879{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003881 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003882 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003883
3884 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003885 /* Immediately reject a zero-length signature buffer. This guarantees
3886 * that signature must be a valid pointer. (On the other hand, the hash
3887 * buffer can in principle be empty since it doesn't actually have
3888 * to be a hash.) */
3889 if( signature_size == 0 )
3890 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003891
Ronald Cron5c522922020-11-14 16:35:34 +01003892 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3893 PSA_KEY_USAGE_SIGN_HASH,
3894 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003895 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003896 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003897 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003898 {
3899 status = PSA_ERROR_INVALID_ARGUMENT;
3900 goto exit;
3901 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003902
Steven Cooremancd84cb42020-07-16 20:28:36 +02003903 /* Try any of the available accelerators first */
3904 status = psa_driver_wrapper_sign_hash( slot,
3905 alg,
3906 hash,
3907 hash_length,
3908 signature,
3909 signature_size,
3910 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003911 if( status != PSA_ERROR_NOT_SUPPORTED ||
3912 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003913 goto exit;
3914
Steven Cooreman7a250572020-07-17 16:43:05 +02003915 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003916#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3917 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003918 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003919 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003920 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003921
Steven Cooreman4fed4552020-08-03 14:46:03 +02003922 status = psa_load_rsa_representation( slot->attr.type,
3923 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003924 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003925 &rsa );
3926 if( status != PSA_SUCCESS )
3927 goto exit;
3928
Steven Cooremana2371e52020-07-28 14:30:39 +02003929 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003930 alg,
3931 hash, hash_length,
3932 signature, signature_size,
3933 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003934
Steven Cooremana2371e52020-07-28 14:30:39 +02003935 mbedtls_rsa_free( rsa );
3936 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003937 }
3938 else
John Durkop6ba40d12020-11-10 08:50:04 -08003939#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3940 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003941 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003942 {
John Durkop9814fa22020-11-04 12:28:15 -08003943#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3944 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003945 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003946#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003947 PSA_ALG_IS_ECDSA( alg )
3948#else
3949 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3950#endif
3951 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003952 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003953 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003954 status = psa_load_ecp_representation( slot->attr.type,
3955 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003956 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003957 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003958 if( status != PSA_SUCCESS )
3959 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003960 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003961 alg,
3962 hash, hash_length,
3963 signature, signature_size,
3964 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003965 mbedtls_ecp_keypair_free( ecp );
3966 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003967 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003968 else
John Durkop9814fa22020-11-04 12:28:15 -08003969#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3970 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003971 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003972 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003973 }
itayzafrir5c753392018-05-08 11:18:38 +03003974 }
3975 else
itayzafrir5c753392018-05-08 11:18:38 +03003976 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003977 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003978 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003979
3980exit:
3981 /* Fill the unused part of the output buffer (the whole buffer on error,
3982 * the trailing part on success) with something that isn't a valid mac
3983 * (barring an attack on the mac and deliberately-crafted input),
3984 * in case the caller doesn't check the return status properly. */
3985 if( status == PSA_SUCCESS )
3986 memset( signature + *signature_length, '!',
3987 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003988 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003989 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003990 /* If signature_size is 0 then we have nothing to do. We must not call
3991 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003992
Ronald Cron5c522922020-11-14 16:35:34 +01003993 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003994
Ronald Cron5c522922020-11-14 16:35:34 +01003995 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003996}
3997
Ronald Croncf56a0a2020-08-04 09:51:30 +02003998psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003999 psa_algorithm_t alg,
4000 const uint8_t *hash,
4001 size_t hash_length,
4002 const uint8_t *signature,
4003 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004004{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004005 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004006 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004007 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004008
Ronald Cron5c522922020-11-14 16:35:34 +01004009 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4010 PSA_KEY_USAGE_VERIFY_HASH,
4011 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004012 if( status != PSA_SUCCESS )
4013 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004014
Steven Cooreman55ae2172020-07-17 19:46:15 +02004015 /* Try any of the available accelerators first */
4016 status = psa_driver_wrapper_verify_hash( slot,
4017 alg,
4018 hash,
4019 hash_length,
4020 signature,
4021 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004022 if( status != PSA_ERROR_NOT_SUPPORTED ||
4023 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004024 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004025
John Durkop6ba40d12020-11-10 08:50:04 -08004026#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4027 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004028 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004030 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004031
Steven Cooreman4fed4552020-08-03 14:46:03 +02004032 status = psa_load_rsa_representation( slot->attr.type,
4033 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004034 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004035 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004036 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004037 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004038
Steven Cooremana2371e52020-07-28 14:30:39 +02004039 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004040 alg,
4041 hash, hash_length,
4042 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004043 mbedtls_rsa_free( rsa );
4044 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004045 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004046 }
4047 else
John Durkop6ba40d12020-11-10 08:50:04 -08004048#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4049 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004050 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004051 {
John Durkop9814fa22020-11-04 12:28:15 -08004052#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4053 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004054 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004055 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004056 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004057 status = psa_load_ecp_representation( slot->attr.type,
4058 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004059 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004060 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004061 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004062 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004063 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004064 hash, hash_length,
4065 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004066 mbedtls_ecp_keypair_free( ecp );
4067 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004068 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004069 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004070 else
John Durkop9814fa22020-11-04 12:28:15 -08004071#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4072 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004073 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004074 status = PSA_ERROR_INVALID_ARGUMENT;
4075 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004076 }
itayzafrir5c753392018-05-08 11:18:38 +03004077 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004078 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004079 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004080 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004081 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004082
4083exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004084 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004085
Ronald Cron5c522922020-11-14 16:35:34 +01004086 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004087}
4088
John Durkop0e005192020-10-31 22:06:54 -07004089#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004090static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4091 mbedtls_rsa_context *rsa )
4092{
4093 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4094 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4095 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4096 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4097}
John Durkop0e005192020-10-31 22:06:54 -07004098#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004099
Ronald Croncf56a0a2020-08-04 09:51:30 +02004100psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004101 psa_algorithm_t alg,
4102 const uint8_t *input,
4103 size_t input_length,
4104 const uint8_t *salt,
4105 size_t salt_length,
4106 uint8_t *output,
4107 size_t output_size,
4108 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004109{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004110 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004111 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004112 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004113
Darryl Green5cc689a2018-07-24 15:34:10 +01004114 (void) input;
4115 (void) input_length;
4116 (void) salt;
4117 (void) output;
4118 (void) output_size;
4119
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004120 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004121
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004122 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4123 return( PSA_ERROR_INVALID_ARGUMENT );
4124
Ronald Cron5c522922020-11-14 16:35:34 +01004125 status = psa_get_and_lock_transparent_key_slot_with_policy(
4126 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004127 if( status != PSA_SUCCESS )
4128 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004129 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4130 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004131 {
4132 status = PSA_ERROR_INVALID_ARGUMENT;
4133 goto exit;
4134 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004135
John Durkop6ba40d12020-11-10 08:50:04 -08004136#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4137 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004138 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004139 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004140 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004141 status = psa_load_rsa_representation( slot->attr.type,
4142 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004143 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004144 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004145 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004146 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004147
4148 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004149 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004150 status = PSA_ERROR_BUFFER_TOO_SMALL;
4151 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004152 }
John Durkop0e005192020-10-31 22:06:54 -07004153#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004154 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004155 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004156 status = mbedtls_to_psa_error(
4157 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004158 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004159 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004160 MBEDTLS_RSA_PUBLIC,
4161 input_length,
4162 input,
4163 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004164 }
4165 else
John Durkop0e005192020-10-31 22:06:54 -07004166#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4167#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004168 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004169 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004170 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004171 status = mbedtls_to_psa_error(
4172 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004173 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004174 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004175 MBEDTLS_RSA_PUBLIC,
4176 salt, salt_length,
4177 input_length,
4178 input,
4179 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004180 }
4181 else
John Durkop0e005192020-10-31 22:06:54 -07004182#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004183 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004184 status = PSA_ERROR_INVALID_ARGUMENT;
4185 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004186 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004187rsa_exit:
4188 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004189 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004190
Steven Cooremana2371e52020-07-28 14:30:39 +02004191 mbedtls_rsa_free( rsa );
4192 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004193 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004194 else
John Durkop9814fa22020-11-04 12:28:15 -08004195#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004196 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004197 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004198 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004200
4201exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004202 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004203
Ronald Cron5c522922020-11-14 16:35:34 +01004204 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004205}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004206
Ronald Croncf56a0a2020-08-04 09:51:30 +02004207psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004208 psa_algorithm_t alg,
4209 const uint8_t *input,
4210 size_t input_length,
4211 const uint8_t *salt,
4212 size_t salt_length,
4213 uint8_t *output,
4214 size_t output_size,
4215 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004216{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004217 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004218 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004219 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004220
Darryl Green5cc689a2018-07-24 15:34:10 +01004221 (void) input;
4222 (void) input_length;
4223 (void) salt;
4224 (void) output;
4225 (void) output_size;
4226
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004227 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004228
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004229 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4230 return( PSA_ERROR_INVALID_ARGUMENT );
4231
Ronald Cron5c522922020-11-14 16:35:34 +01004232 status = psa_get_and_lock_transparent_key_slot_with_policy(
4233 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004234 if( status != PSA_SUCCESS )
4235 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004236 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004237 {
4238 status = PSA_ERROR_INVALID_ARGUMENT;
4239 goto exit;
4240 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004241
John Durkop6ba40d12020-11-10 08:50:04 -08004242#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4243 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004244 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004245 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004246 mbedtls_rsa_context *rsa = NULL;
4247 status = psa_load_rsa_representation( slot->attr.type,
4248 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004249 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004250 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004251 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004252 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004253
Steven Cooremana2371e52020-07-28 14:30:39 +02004254 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004255 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004256 status = PSA_ERROR_INVALID_ARGUMENT;
4257 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004258 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004259
John Durkop0e005192020-10-31 22:06:54 -07004260#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004261 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004262 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004263 status = mbedtls_to_psa_error(
4264 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004265 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004266 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004267 MBEDTLS_RSA_PRIVATE,
4268 output_length,
4269 input,
4270 output,
4271 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004272 }
4273 else
John Durkop0e005192020-10-31 22:06:54 -07004274#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4275#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004276 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004277 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004278 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004279 status = mbedtls_to_psa_error(
4280 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004281 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004282 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004283 MBEDTLS_RSA_PRIVATE,
4284 salt, salt_length,
4285 output_length,
4286 input,
4287 output,
4288 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004289 }
4290 else
John Durkop0e005192020-10-31 22:06:54 -07004291#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004292 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004293 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004294 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004295
Steven Cooreman4fed4552020-08-03 14:46:03 +02004296rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004297 mbedtls_rsa_free( rsa );
4298 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004299 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004300 else
John Durkop6ba40d12020-11-10 08:50:04 -08004301#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4302 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004303 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004304 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004305 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004306
4307exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004308 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004309
Ronald Cron5c522922020-11-14 16:35:34 +01004310 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004311}
Gilles Peskine20035e32018-02-03 22:44:14 +01004312
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004313
4314
mohammad1603503973b2018-03-12 15:59:30 +02004315/****************************************************************/
4316/* Symmetric cryptography */
4317/****************************************************************/
4318
Gilles Peskinee553c652018-06-04 16:22:46 +02004319static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004320 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004321 psa_algorithm_t alg,
4322 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004323{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004325 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004326 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004327 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004328 size_t key_bits;
4329 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004330 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4331 PSA_KEY_USAGE_ENCRYPT :
4332 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004333
Steven Cooremand3feccd2020-09-01 15:56:14 +02004334 /* A context must be freshly initialized before it can be set up. */
4335 if( operation->alg != 0 )
4336 return( PSA_ERROR_BAD_STATE );
4337
Steven Cooremana07b9972020-09-10 14:54:14 +02004338 /* The requested algorithm must be one that can be processed by cipher. */
4339 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004340 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004341
Steven Cooremanef8575e2020-09-11 11:44:50 +02004342 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004343 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004344 if( status != PSA_SUCCESS )
4345 goto exit;
4346
4347 /* Initialize the operation struct members, except for alg. The alg member
4348 * is used to indicate to psa_cipher_abort that there are resources to free,
4349 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004350 operation->key_set = 0;
4351 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004352 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004353 operation->iv_size = 0;
4354 operation->block_size = 0;
4355 if( alg == PSA_ALG_ECB_NO_PADDING )
4356 operation->iv_required = 0;
4357 else
4358 operation->iv_required = 1;
4359
Steven Cooremana07b9972020-09-10 14:54:14 +02004360 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004361 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004362 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004363 slot,
4364 alg );
4365 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004366 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004367 slot,
4368 alg );
4369
Steven Cooremanef8575e2020-09-11 11:44:50 +02004370 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004371 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004372 /* Once the driver context is initialised, it needs to be freed using
4373 * psa_cipher_abort. Indicate this through setting alg. */
4374 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004375 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004376
Steven Cooremand3feccd2020-09-01 15:56:14 +02004377 if( status != PSA_ERROR_NOT_SUPPORTED ||
4378 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4379 goto exit;
4380
Steven Cooremana07b9972020-09-10 14:54:14 +02004381 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004382 * available for the given algorithm & key. */
4383 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004384
Steven Cooremana07b9972020-09-10 14:54:14 +02004385 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004386 * psa_cipher_abort. Indicate there is something to be freed through setting
4387 * alg, and indicate the operation is being done using mbedtls crypto through
4388 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004389 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004390 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004391
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004392 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004393 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004394 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004395 {
4396 status = PSA_ERROR_NOT_SUPPORTED;
4397 goto exit;
4398 }
mohammad1603503973b2018-03-12 15:59:30 +02004399
mohammad1603503973b2018-03-12 15:59:30 +02004400 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004401 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004402 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004403
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004404#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004405 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004406 {
4407 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004408 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004409 memcpy( keys, slot->data.key.data, 16 );
4410 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004411 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4412 keys,
4413 192, cipher_operation );
4414 }
4415 else
4416#endif
4417 {
4418 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004419 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004420 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004421 }
Moran Peker41deec42018-04-04 15:43:05 +03004422 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004423 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004424
mohammad16038481e742018-03-18 13:57:31 +02004425#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004426 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004427 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004428 case PSA_ALG_CBC_NO_PADDING:
4429 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4430 MBEDTLS_PADDING_NONE );
4431 break;
4432 case PSA_ALG_CBC_PKCS7:
4433 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4434 MBEDTLS_PADDING_PKCS7 );
4435 break;
4436 default:
4437 /* The algorithm doesn't involve padding. */
4438 ret = 0;
4439 break;
4440 }
4441 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004442 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004443#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4444
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004445 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004446 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004447 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4448 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004449 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004450 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004451 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004452#if defined(MBEDTLS_CHACHA20_C)
4453 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004454 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004455 operation->iv_size = 12;
4456#endif
mohammad1603503973b2018-03-12 15:59:30 +02004457
Steven Cooreman7df02922020-09-09 15:28:49 +02004458 status = PSA_SUCCESS;
4459
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004460exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004461 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004462 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004463 if( status == PSA_SUCCESS )
4464 {
4465 /* Update operation flags for both driver and software implementations */
4466 operation->key_set = 1;
4467 }
4468 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004469 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004470
Ronald Cron5c522922020-11-14 16:35:34 +01004471 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004472
Ronald Cron5c522922020-11-14 16:35:34 +01004473 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004474}
4475
Gilles Peskinefe119512018-07-08 21:39:34 +02004476psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004477 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004478 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004479{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004480 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004481}
4482
Gilles Peskinefe119512018-07-08 21:39:34 +02004483psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004484 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004485 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004486{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004487 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004488}
4489
Gilles Peskinefe119512018-07-08 21:39:34 +02004490psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004491 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004492 size_t iv_size,
4493 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004494{
itayzafrir534bd7c2018-08-02 13:56:32 +03004495 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004497 if( operation->iv_set || ! operation->iv_required )
4498 {
4499 return( PSA_ERROR_BAD_STATE );
4500 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004501
Steven Cooremanef8575e2020-09-11 11:44:50 +02004502 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004503 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004504 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004505 iv,
4506 iv_size,
4507 iv_length );
4508 goto exit;
4509 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004510
Moran Peker41deec42018-04-04 15:43:05 +03004511 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004512 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004513 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004514 goto exit;
4515 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004516 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004517 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004518 if( ret != 0 )
4519 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004520 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004521 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004522 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004523
mohammad16038481e742018-03-18 13:57:31 +02004524 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004525 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004526
Moran Peker395db872018-05-31 14:07:14 +03004527exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004528 if( status == PSA_SUCCESS )
4529 operation->iv_set = 1;
4530 else
Moran Peker395db872018-05-31 14:07:14 +03004531 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004532 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004533}
4534
Gilles Peskinefe119512018-07-08 21:39:34 +02004535psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004536 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004537 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004538{
itayzafrir534bd7c2018-08-02 13:56:32 +03004539 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004541 if( operation->iv_set || ! operation->iv_required )
4542 {
4543 return( PSA_ERROR_BAD_STATE );
4544 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004545
Steven Cooremanef8575e2020-09-11 11:44:50 +02004546 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004547 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004548 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004549 iv,
4550 iv_length );
4551 goto exit;
4552 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004553
Moran Pekera28258c2018-05-29 16:25:04 +03004554 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004555 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004556 status = PSA_ERROR_INVALID_ARGUMENT;
4557 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004558 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004559 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4560 status = mbedtls_to_psa_error( ret );
4561exit:
4562 if( status == PSA_SUCCESS )
4563 operation->iv_set = 1;
4564 else
mohammad1603503973b2018-03-12 15:59:30 +02004565 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004566 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004567}
4568
Steven Cooreman177deba2020-09-07 17:14:14 +02004569/* Process input for which the algorithm is set to ECB mode. This requires
4570 * manual processing, since the PSA API is defined as being able to process
4571 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4572 * underlying mbedtls_cipher_update only takes full blocks. */
4573static psa_status_t psa_cipher_update_ecb_internal(
4574 mbedtls_cipher_context_t *ctx,
4575 const uint8_t *input,
4576 size_t input_length,
4577 uint8_t *output,
4578 size_t output_size,
4579 size_t *output_length )
4580{
4581 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4582 size_t block_size = ctx->cipher_info->block_size;
4583 size_t internal_output_length = 0;
4584 *output_length = 0;
4585
4586 if( input_length == 0 )
4587 {
4588 status = PSA_SUCCESS;
4589 goto exit;
4590 }
4591
4592 if( ctx->unprocessed_len > 0 )
4593 {
4594 /* Fill up to block size, and run the block if there's a full one. */
4595 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4596
4597 if( input_length < bytes_to_copy )
4598 bytes_to_copy = input_length;
4599
4600 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4601 input, bytes_to_copy );
4602 input_length -= bytes_to_copy;
4603 input += bytes_to_copy;
4604 ctx->unprocessed_len += bytes_to_copy;
4605
4606 if( ctx->unprocessed_len == block_size )
4607 {
4608 status = mbedtls_to_psa_error(
4609 mbedtls_cipher_update( ctx,
4610 ctx->unprocessed_data,
4611 block_size,
4612 output, &internal_output_length ) );
4613
4614 if( status != PSA_SUCCESS )
4615 goto exit;
4616
4617 output += internal_output_length;
4618 output_size -= internal_output_length;
4619 *output_length += internal_output_length;
4620 ctx->unprocessed_len = 0;
4621 }
4622 }
4623
4624 while( input_length >= block_size )
4625 {
4626 /* Run all full blocks we have, one by one */
4627 status = mbedtls_to_psa_error(
4628 mbedtls_cipher_update( ctx, input,
4629 block_size,
4630 output, &internal_output_length ) );
4631
4632 if( status != PSA_SUCCESS )
4633 goto exit;
4634
4635 input_length -= block_size;
4636 input += block_size;
4637
4638 output += internal_output_length;
4639 output_size -= internal_output_length;
4640 *output_length += internal_output_length;
4641 }
4642
4643 if( input_length > 0 )
4644 {
4645 /* Save unprocessed bytes for later processing */
4646 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4647 input, input_length );
4648 ctx->unprocessed_len += input_length;
4649 }
4650
4651 status = PSA_SUCCESS;
4652
4653exit:
4654 return( status );
4655}
4656
Gilles Peskinee553c652018-06-04 16:22:46 +02004657psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4658 const uint8_t *input,
4659 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004660 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004661 size_t output_size,
4662 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004663{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004665 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004666 if( operation->alg == 0 )
4667 {
4668 return( PSA_ERROR_BAD_STATE );
4669 }
4670 if( operation->iv_required && ! operation->iv_set )
4671 {
4672 return( PSA_ERROR_BAD_STATE );
4673 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004674
Steven Cooremanef8575e2020-09-11 11:44:50 +02004675 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004676 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004677 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004678 input,
4679 input_length,
4680 output,
4681 output_size,
4682 output_length );
4683 goto exit;
4684 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004685
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004686 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004687 {
4688 /* Take the unprocessed partial block left over from previous
4689 * update calls, if any, plus the input to this call. Remove
4690 * the last partial block, if any. You get the data that will be
4691 * output in this call. */
4692 expected_output_size =
4693 ( operation->ctx.cipher.unprocessed_len + input_length )
4694 / operation->block_size * operation->block_size;
4695 }
4696 else
4697 {
4698 expected_output_size = input_length;
4699 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004700
Gilles Peskine89d789c2018-06-04 17:17:16 +02004701 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004702 {
4703 status = PSA_ERROR_BUFFER_TOO_SMALL;
4704 goto exit;
4705 }
mohammad160382759612018-03-12 18:16:40 +02004706
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004707 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4708 {
4709 /* mbedtls_cipher_update has an API inconsistency: it will only
4710 * process a single block at a time in ECB mode. Abstract away that
4711 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004712 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4713 input,
4714 input_length,
4715 output,
4716 output_size,
4717 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004718 }
4719 else
4720 {
4721 status = mbedtls_to_psa_error(
4722 mbedtls_cipher_update( &operation->ctx.cipher, input,
4723 input_length, output, output_length ) );
4724 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004725exit:
4726 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004727 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004728 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004729}
4730
Gilles Peskinee553c652018-06-04 16:22:46 +02004731psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4732 uint8_t *output,
4733 size_t output_size,
4734 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004735{
David Saadab4ecc272019-02-14 13:48:10 +02004736 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004737 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004738 if( operation->alg == 0 )
4739 {
4740 return( PSA_ERROR_BAD_STATE );
4741 }
4742 if( operation->iv_required && ! operation->iv_set )
4743 {
4744 return( PSA_ERROR_BAD_STATE );
4745 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004746
Steven Cooremanef8575e2020-09-11 11:44:50 +02004747 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004748 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004749 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004750 output,
4751 output_size,
4752 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004753 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004754 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004755
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004756 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004757 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004758 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004759 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004760 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004761 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004762 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004763 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004764 }
4765
Steven Cooremanef8575e2020-09-11 11:44:50 +02004766 status = mbedtls_to_psa_error(
4767 mbedtls_cipher_finish( &operation->ctx.cipher,
4768 temp_output_buffer,
4769 output_length ) );
4770 if( status != PSA_SUCCESS )
4771 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004772
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004773 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004774 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004775 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004776 memcpy( output, temp_output_buffer, *output_length );
4777 else
Janos Follath315b51c2018-07-09 16:04:51 +01004778 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004779
Steven Cooremanef8575e2020-09-11 11:44:50 +02004780exit:
4781 if( operation->mbedtls_in_use == 1 )
4782 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004783
Steven Cooremanef8575e2020-09-11 11:44:50 +02004784 if( status == PSA_SUCCESS )
4785 return( psa_cipher_abort( operation ) );
4786 else
4787 {
4788 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004789 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004790
Steven Cooremanef8575e2020-09-11 11:44:50 +02004791 return( status );
4792 }
mohammad1603503973b2018-03-12 15:59:30 +02004793}
4794
Gilles Peskinee553c652018-06-04 16:22:46 +02004795psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4796{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004797 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004798 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004799 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004800 * in use. It's ok to call abort on such an object, and there's
4801 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004802 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004803 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004804
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004805 /* Sanity check (shouldn't happen: operation->alg should
4806 * always have been initialized to a valid value). */
4807 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4808 return( PSA_ERROR_BAD_STATE );
4809
Steven Cooremanef8575e2020-09-11 11:44:50 +02004810 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004811 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004812 else
4813 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004814
Moran Peker41deec42018-04-04 15:43:05 +03004815 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004816 operation->key_set = 0;
4817 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004818 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004819 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004820 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004821 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004822
Moran Peker395db872018-05-31 14:07:14 +03004823 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004824}
4825
Gilles Peskinea0655c32018-04-30 17:06:50 +02004826
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004827
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004828
mohammad16035955c982018-04-26 00:53:03 +03004829/****************************************************************/
4830/* AEAD */
4831/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004832
Gilles Peskineedf9a652018-08-17 18:11:56 +02004833typedef struct
4834{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004835 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004836 const mbedtls_cipher_info_t *cipher_info;
4837 union
4838 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004839 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004840#if defined(MBEDTLS_CCM_C)
4841 mbedtls_ccm_context ccm;
4842#endif /* MBEDTLS_CCM_C */
4843#if defined(MBEDTLS_GCM_C)
4844 mbedtls_gcm_context gcm;
4845#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004846#if defined(MBEDTLS_CHACHAPOLY_C)
4847 mbedtls_chachapoly_context chachapoly;
4848#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004849 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004850 psa_algorithm_t core_alg;
4851 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004852 uint8_t tag_length;
4853} aead_operation_t;
4854
Ronald Cronf95a2b12020-10-22 15:24:49 +02004855#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4856
Gilles Peskine30a9e412019-01-14 18:36:12 +01004857static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004858{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004859 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004860 {
4861#if defined(MBEDTLS_CCM_C)
4862 case PSA_ALG_CCM:
4863 mbedtls_ccm_free( &operation->ctx.ccm );
4864 break;
4865#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004866#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004867 case PSA_ALG_GCM:
4868 mbedtls_gcm_free( &operation->ctx.gcm );
4869 break;
4870#endif /* MBEDTLS_GCM_C */
4871 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004872
Ronald Cron5c522922020-11-14 16:35:34 +01004873 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004874}
4875
4876static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004877 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004878 psa_key_usage_t usage,
4879 psa_algorithm_t alg )
4880{
4881 psa_status_t status;
4882 size_t key_bits;
4883 mbedtls_cipher_id_t cipher_id;
4884
Ronald Cron5c522922020-11-14 16:35:34 +01004885 status = psa_get_and_lock_transparent_key_slot_with_policy(
4886 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004887 if( status != PSA_SUCCESS )
4888 return( status );
4889
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004890 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004891
4892 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004893 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004894 &cipher_id );
4895 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004896 {
4897 status = PSA_ERROR_NOT_SUPPORTED;
4898 goto cleanup;
4899 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004900
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004901 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004902 {
4903#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004904 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4905 operation->core_alg = PSA_ALG_CCM;
4906 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004907 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4908 * The call to mbedtls_ccm_encrypt_and_tag or
4909 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004910 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004911 {
4912 status = PSA_ERROR_INVALID_ARGUMENT;
4913 goto cleanup;
4914 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004915 mbedtls_ccm_init( &operation->ctx.ccm );
4916 status = mbedtls_to_psa_error(
4917 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004918 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004919 (unsigned int) key_bits ) );
4920 if( status != 0 )
4921 goto cleanup;
4922 break;
4923#endif /* MBEDTLS_CCM_C */
4924
4925#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004926 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4927 operation->core_alg = PSA_ALG_GCM;
4928 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004929 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4930 * The call to mbedtls_gcm_crypt_and_tag or
4931 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004932 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004933 {
4934 status = PSA_ERROR_INVALID_ARGUMENT;
4935 goto cleanup;
4936 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004937 mbedtls_gcm_init( &operation->ctx.gcm );
4938 status = mbedtls_to_psa_error(
4939 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004940 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004941 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004942 if( status != 0 )
4943 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004944 break;
4945#endif /* MBEDTLS_GCM_C */
4946
Gilles Peskine26869f22019-05-06 15:25:00 +02004947#if defined(MBEDTLS_CHACHAPOLY_C)
4948 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4949 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4950 operation->full_tag_length = 16;
4951 /* We only support the default tag length. */
4952 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004953 {
4954 status = PSA_ERROR_NOT_SUPPORTED;
4955 goto cleanup;
4956 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004957 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4958 status = mbedtls_to_psa_error(
4959 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004960 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004961 if( status != 0 )
4962 goto cleanup;
4963 break;
4964#endif /* MBEDTLS_CHACHAPOLY_C */
4965
Gilles Peskineedf9a652018-08-17 18:11:56 +02004966 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004967 status = PSA_ERROR_NOT_SUPPORTED;
4968 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004969 }
4970
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004971 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4972 {
4973 status = PSA_ERROR_INVALID_ARGUMENT;
4974 goto cleanup;
4975 }
4976 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004977
Gilles Peskineedf9a652018-08-17 18:11:56 +02004978 return( PSA_SUCCESS );
4979
4980cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004981 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004982 return( status );
4983}
4984
Ronald Croncf56a0a2020-08-04 09:51:30 +02004985psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004986 psa_algorithm_t alg,
4987 const uint8_t *nonce,
4988 size_t nonce_length,
4989 const uint8_t *additional_data,
4990 size_t additional_data_length,
4991 const uint8_t *plaintext,
4992 size_t plaintext_length,
4993 uint8_t *ciphertext,
4994 size_t ciphertext_size,
4995 size_t *ciphertext_length )
4996{
mohammad16035955c982018-04-26 00:53:03 +03004997 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004998 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004999 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005000
mohammad1603f08a5502018-06-03 15:05:47 +03005001 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005002
Ronald Croncf56a0a2020-08-04 09:51:30 +02005003 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005004 if( status != PSA_SUCCESS )
5005 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005006
Gilles Peskineedf9a652018-08-17 18:11:56 +02005007 /* For all currently supported modes, the tag is at the end of the
5008 * ciphertext. */
5009 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5010 {
5011 status = PSA_ERROR_BUFFER_TOO_SMALL;
5012 goto exit;
5013 }
5014 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005015
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005016#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005017 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005018 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005019 status = mbedtls_to_psa_error(
5020 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5021 MBEDTLS_GCM_ENCRYPT,
5022 plaintext_length,
5023 nonce, nonce_length,
5024 additional_data, additional_data_length,
5025 plaintext, ciphertext,
5026 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005027 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005028 else
5029#endif /* MBEDTLS_GCM_C */
5030#if defined(MBEDTLS_CCM_C)
5031 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005032 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005033 status = mbedtls_to_psa_error(
5034 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5035 plaintext_length,
5036 nonce, nonce_length,
5037 additional_data,
5038 additional_data_length,
5039 plaintext, ciphertext,
5040 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005041 }
mohammad16035c8845f2018-05-09 05:40:09 -07005042 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005043#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005044#if defined(MBEDTLS_CHACHAPOLY_C)
5045 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5046 {
5047 if( nonce_length != 12 || operation.tag_length != 16 )
5048 {
5049 status = PSA_ERROR_NOT_SUPPORTED;
5050 goto exit;
5051 }
5052 status = mbedtls_to_psa_error(
5053 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5054 plaintext_length,
5055 nonce,
5056 additional_data,
5057 additional_data_length,
5058 plaintext,
5059 ciphertext,
5060 tag ) );
5061 }
5062 else
5063#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005064 {
mohammad1603554faad2018-06-03 15:07:38 +03005065 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005066 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005067
Gilles Peskineedf9a652018-08-17 18:11:56 +02005068 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5069 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005070
Gilles Peskineedf9a652018-08-17 18:11:56 +02005071exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005072 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005073 if( status == PSA_SUCCESS )
5074 *ciphertext_length = plaintext_length + operation.tag_length;
5075 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005076}
5077
Gilles Peskineee652a32018-06-01 19:23:52 +02005078/* Locate the tag in a ciphertext buffer containing the encrypted data
5079 * followed by the tag. Return the length of the part preceding the tag in
5080 * *plaintext_length. This is the size of the plaintext in modes where
5081 * the encrypted data has the same size as the plaintext, such as
5082 * CCM and GCM. */
5083static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5084 const uint8_t *ciphertext,
5085 size_t ciphertext_length,
5086 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005087 const uint8_t **p_tag )
5088{
5089 size_t payload_length;
5090 if( tag_length > ciphertext_length )
5091 return( PSA_ERROR_INVALID_ARGUMENT );
5092 payload_length = ciphertext_length - tag_length;
5093 if( payload_length > plaintext_size )
5094 return( PSA_ERROR_BUFFER_TOO_SMALL );
5095 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005096 return( PSA_SUCCESS );
5097}
5098
Ronald Croncf56a0a2020-08-04 09:51:30 +02005099psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005100 psa_algorithm_t alg,
5101 const uint8_t *nonce,
5102 size_t nonce_length,
5103 const uint8_t *additional_data,
5104 size_t additional_data_length,
5105 const uint8_t *ciphertext,
5106 size_t ciphertext_length,
5107 uint8_t *plaintext,
5108 size_t plaintext_size,
5109 size_t *plaintext_length )
5110{
mohammad16035955c982018-04-26 00:53:03 +03005111 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005112 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005113 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005114
Gilles Peskineee652a32018-06-01 19:23:52 +02005115 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005116
Ronald Croncf56a0a2020-08-04 09:51:30 +02005117 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005118 if( status != PSA_SUCCESS )
5119 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005120
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005121 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5122 ciphertext, ciphertext_length,
5123 plaintext_size, &tag );
5124 if( status != PSA_SUCCESS )
5125 goto exit;
5126
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005127#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005128 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005129 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005130 status = mbedtls_to_psa_error(
5131 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5132 ciphertext_length - operation.tag_length,
5133 nonce, nonce_length,
5134 additional_data,
5135 additional_data_length,
5136 tag, operation.tag_length,
5137 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005138 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005139 else
5140#endif /* MBEDTLS_GCM_C */
5141#if defined(MBEDTLS_CCM_C)
5142 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005143 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005144 status = mbedtls_to_psa_error(
5145 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5146 ciphertext_length - operation.tag_length,
5147 nonce, nonce_length,
5148 additional_data,
5149 additional_data_length,
5150 ciphertext, plaintext,
5151 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005152 }
mohammad160339574652018-06-01 04:39:53 -07005153 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005154#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005155#if defined(MBEDTLS_CHACHAPOLY_C)
5156 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5157 {
5158 if( nonce_length != 12 || operation.tag_length != 16 )
5159 {
5160 status = PSA_ERROR_NOT_SUPPORTED;
5161 goto exit;
5162 }
5163 status = mbedtls_to_psa_error(
5164 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5165 ciphertext_length - operation.tag_length,
5166 nonce,
5167 additional_data,
5168 additional_data_length,
5169 tag,
5170 ciphertext,
5171 plaintext ) );
5172 }
5173 else
5174#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005175 {
mohammad1603554faad2018-06-03 15:07:38 +03005176 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005177 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005178
Gilles Peskineedf9a652018-08-17 18:11:56 +02005179 if( status != PSA_SUCCESS && plaintext_size != 0 )
5180 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005181
Gilles Peskineedf9a652018-08-17 18:11:56 +02005182exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005183 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005184 if( status == PSA_SUCCESS )
5185 *plaintext_length = ciphertext_length - operation.tag_length;
5186 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005187}
5188
Gilles Peskinea0655c32018-04-30 17:06:50 +02005189
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005190
Gilles Peskine20035e32018-02-03 22:44:14 +01005191/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005192/* Generators */
5193/****************************************************************/
5194
John Durkop07cc04a2020-11-16 22:08:34 -08005195#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5196 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5197 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5198#define AT_LEAST_ONE_BUILTIN_KDF
5199#endif
5200
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005201#define HKDF_STATE_INIT 0 /* no input yet */
5202#define HKDF_STATE_STARTED 1 /* got salt */
5203#define HKDF_STATE_KEYED 2 /* got key */
5204#define HKDF_STATE_OUTPUT 3 /* output started */
5205
Gilles Peskinecbe66502019-05-16 16:59:18 +02005206static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005208{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5210 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005211 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005213}
5214
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005216{
5217 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005219 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005220 {
5221 /* The object has (apparently) been initialized but it is not
5222 * in use. It's ok to call abort on such an object, and there's
5223 * nothing to do. */
5224 }
5225 else
John Durkopd0321952020-10-29 21:37:36 -07005226#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005227 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005228 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005229 mbedtls_free( operation->ctx.hkdf.info );
5230 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005231 }
John Durkop07cc04a2020-11-16 22:08:34 -08005232 else
5233#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5234#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5235 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5236 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005237 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005238 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005239 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005240 if( operation->ctx.tls12_prf.seed != NULL )
5241 {
5242 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5243 operation->ctx.tls12_prf.seed_length );
5244 mbedtls_free( operation->ctx.tls12_prf.seed );
5245 }
5246
5247 if( operation->ctx.tls12_prf.label != NULL )
5248 {
5249 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5250 operation->ctx.tls12_prf.label_length );
5251 mbedtls_free( operation->ctx.tls12_prf.label );
5252 }
5253
5254 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5255
5256 /* We leave the fields Ai and output_block to be erased safely by the
5257 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005258 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005259 else
John Durkop07cc04a2020-11-16 22:08:34 -08005260#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5261 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005262 {
5263 status = PSA_ERROR_BAD_STATE;
5264 }
Janos Follath083036a2019-06-11 10:22:26 +01005265 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005266 return( status );
5267}
5268
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005270 size_t *capacity)
5271{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005273 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005274 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005275 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005276 }
5277
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005279 return( PSA_SUCCESS );
5280}
5281
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005282psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005283 size_t capacity )
5284{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005286 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005288 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005290 return( PSA_SUCCESS );
5291}
5292
John Durkopd0321952020-10-29 21:37:36 -07005293#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005294/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005295 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005296static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005297 psa_algorithm_t hash_alg,
5298 uint8_t *output,
5299 size_t output_length )
5300{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005301 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005302 psa_status_t status;
5303
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005304 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5305 return( PSA_ERROR_BAD_STATE );
5306 hkdf->state = HKDF_STATE_OUTPUT;
5307
Gilles Peskinebef7f142018-07-12 17:22:21 +02005308 while( output_length != 0 )
5309 {
5310 /* Copy what remains of the current block */
5311 uint8_t n = hash_length - hkdf->offset_in_block;
5312 if( n > output_length )
5313 n = (uint8_t) output_length;
5314 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5315 output += n;
5316 output_length -= n;
5317 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005318 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005319 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005320 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005321 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005323 * object was corrupted or if this function is called directly
5324 * inside the library. */
5325 if( hkdf->block_number == 0xff )
5326 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005327
5328 /* We need a new block */
5329 ++hkdf->block_number;
5330 hkdf->offset_in_block = 0;
5331 status = psa_hmac_setup_internal( &hkdf->hmac,
5332 hkdf->prk, hash_length,
5333 hash_alg );
5334 if( status != PSA_SUCCESS )
5335 return( status );
5336 if( hkdf->block_number != 1 )
5337 {
5338 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5339 hkdf->output_block,
5340 hash_length );
5341 if( status != PSA_SUCCESS )
5342 return( status );
5343 }
5344 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5345 hkdf->info,
5346 hkdf->info_length );
5347 if( status != PSA_SUCCESS )
5348 return( status );
5349 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5350 &hkdf->block_number, 1 );
5351 if( status != PSA_SUCCESS )
5352 return( status );
5353 status = psa_hmac_finish_internal( &hkdf->hmac,
5354 hkdf->output_block,
5355 sizeof( hkdf->output_block ) );
5356 if( status != PSA_SUCCESS )
5357 return( status );
5358 }
5359
5360 return( PSA_SUCCESS );
5361}
John Durkop07cc04a2020-11-16 22:08:34 -08005362#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005363
John Durkop07cc04a2020-11-16 22:08:34 -08005364#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5365 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005366static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5367 psa_tls12_prf_key_derivation_t *tls12_prf,
5368 psa_algorithm_t alg )
5369{
5370 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005371 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005372 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5373 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005374
Janos Follath7742fee2019-06-17 12:58:10 +01005375 /* We can't be wanting more output after block 0xff, otherwise
5376 * the capacity check in psa_key_derivation_output_bytes() would have
5377 * prevented this call. It could happen only if the operation
5378 * object was corrupted or if this function is called directly
5379 * inside the library. */
5380 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005381 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005382
5383 /* We need a new block */
5384 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005385 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005386
5387 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5388 *
5389 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5390 *
5391 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5392 * HMAC_hash(secret, A(2) + seed) +
5393 * HMAC_hash(secret, A(3) + seed) + ...
5394 *
5395 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005396 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005397 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005398 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005399 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005400 * is currently extracted as `output_block` and where i is
5401 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005402 */
5403
Janos Follathea29bfb2019-06-19 12:21:20 +01005404 /* Save the hash context before using it, to preserve the hash state with
5405 * only the inner padding in it. We need this, because inner padding depends
5406 * on the key (secret in the RFC's terminology). */
5407 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5408 if( status != PSA_SUCCESS )
5409 goto cleanup;
5410
5411 /* Calculate A(i) where i = tls12_prf->block_number. */
5412 if( tls12_prf->block_number == 1 )
5413 {
5414 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5415 * the variable seed and in this instance means it in the context of the
5416 * P_hash function, where seed = label + seed.) */
5417 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5418 tls12_prf->label, tls12_prf->label_length );
5419 if( status != PSA_SUCCESS )
5420 goto cleanup;
5421 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5422 tls12_prf->seed, tls12_prf->seed_length );
5423 if( status != PSA_SUCCESS )
5424 goto cleanup;
5425 }
5426 else
5427 {
5428 /* A(i) = HMAC_hash(secret, A(i-1)) */
5429 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5430 tls12_prf->Ai, hash_length );
5431 if( status != PSA_SUCCESS )
5432 goto cleanup;
5433 }
5434
5435 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5436 tls12_prf->Ai, hash_length );
5437 if( status != PSA_SUCCESS )
5438 goto cleanup;
5439 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5440 if( status != PSA_SUCCESS )
5441 goto cleanup;
5442
5443 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5444 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5445 tls12_prf->Ai, hash_length );
5446 if( status != PSA_SUCCESS )
5447 goto cleanup;
5448 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5449 tls12_prf->label, tls12_prf->label_length );
5450 if( status != PSA_SUCCESS )
5451 goto cleanup;
5452 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5453 tls12_prf->seed, tls12_prf->seed_length );
5454 if( status != PSA_SUCCESS )
5455 goto cleanup;
5456 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5457 tls12_prf->output_block, hash_length );
5458 if( status != PSA_SUCCESS )
5459 goto cleanup;
5460 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5461 if( status != PSA_SUCCESS )
5462 goto cleanup;
5463
Janos Follath7742fee2019-06-17 12:58:10 +01005464
5465cleanup:
5466
Janos Follathea29bfb2019-06-19 12:21:20 +01005467 cleanup_status = psa_hash_abort( &backup );
5468 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5469 status = cleanup_status;
5470
5471 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005472}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005473
Janos Follath844eb0e2019-06-19 12:10:49 +01005474static psa_status_t psa_key_derivation_tls12_prf_read(
5475 psa_tls12_prf_key_derivation_t *tls12_prf,
5476 psa_algorithm_t alg,
5477 uint8_t *output,
5478 size_t output_length )
5479{
5480 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005481 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005482 psa_status_t status;
5483 uint8_t offset, length;
5484
5485 while( output_length != 0 )
5486 {
5487 /* Check if we have fully processed the current block. */
5488 if( tls12_prf->left_in_block == 0 )
5489 {
5490 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5491 alg );
5492 if( status != PSA_SUCCESS )
5493 return( status );
5494
5495 continue;
5496 }
5497
5498 if( tls12_prf->left_in_block > output_length )
5499 length = (uint8_t) output_length;
5500 else
5501 length = tls12_prf->left_in_block;
5502
5503 offset = hash_length - tls12_prf->left_in_block;
5504 memcpy( output, tls12_prf->output_block + offset, length );
5505 output += length;
5506 output_length -= length;
5507 tls12_prf->left_in_block -= length;
5508 }
5509
5510 return( PSA_SUCCESS );
5511}
John Durkop07cc04a2020-11-16 22:08:34 -08005512#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5513 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005514
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005515psa_status_t psa_key_derivation_output_bytes(
5516 psa_key_derivation_operation_t *operation,
5517 uint8_t *output,
5518 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005519{
5520 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005521 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005522
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005524 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005525 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005526 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005527 }
5528
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005529 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005530 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005531 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005532 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005534 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005535 goto exit;
5536 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005537 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005538 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005539 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005540 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005541 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5542 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005543 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005544 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005545 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005546 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005547 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005548
John Durkopd0321952020-10-29 21:37:36 -07005549#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005550 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005551 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005552 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005553 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005554 output, output_length );
5555 }
Janos Follathadbec812019-06-14 11:05:39 +01005556 else
John Durkop07cc04a2020-11-16 22:08:34 -08005557#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5558#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5559 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005560 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005561 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005562 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005563 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005564 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005565 output_length );
5566 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005567 else
John Durkop07cc04a2020-11-16 22:08:34 -08005568#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5569 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005570 {
5571 return( PSA_ERROR_BAD_STATE );
5572 }
5573
5574exit:
5575 if( status != PSA_SUCCESS )
5576 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005577 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005578 * This allows us to differentiate between exhausted operations and
5579 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5580 * operations. */
5581 psa_algorithm_t alg = operation->alg;
5582 psa_key_derivation_abort( operation );
5583 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005584 memset( output, '!', output_length );
5585 }
5586 return( status );
5587}
5588
Gilles Peskine08542d82018-07-19 17:05:42 +02005589#if defined(MBEDTLS_DES_C)
5590static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5591{
5592 if( data_size >= 8 )
5593 mbedtls_des_key_set_parity( data );
5594 if( data_size >= 16 )
5595 mbedtls_des_key_set_parity( data + 8 );
5596 if( data_size >= 24 )
5597 mbedtls_des_key_set_parity( data + 16 );
5598}
5599#endif /* MBEDTLS_DES_C */
5600
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005601static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005602 psa_key_slot_t *slot,
5603 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005604 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005605{
5606 uint8_t *data = NULL;
5607 size_t bytes = PSA_BITS_TO_BYTES( bits );
5608 psa_status_t status;
5609
Gilles Peskine8e338702019-07-30 20:06:31 +02005610 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005611 return( PSA_ERROR_INVALID_ARGUMENT );
5612 if( bits % 8 != 0 )
5613 return( PSA_ERROR_INVALID_ARGUMENT );
5614 data = mbedtls_calloc( 1, bytes );
5615 if( data == NULL )
5616 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5617
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005618 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005619 if( status != PSA_SUCCESS )
5620 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005621#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005622 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005623 psa_des_set_key_parity( data, bytes );
5624#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005625 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005626
5627exit:
5628 mbedtls_free( data );
5629 return( status );
5630}
5631
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005632psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005633 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005634 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005635{
5636 psa_status_t status;
5637 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005638 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005639
Ronald Cron81709fc2020-11-14 12:10:32 +01005640 *key = MBEDTLS_SVC_KEY_ID_INIT;
5641
Gilles Peskine0f84d622019-09-12 19:03:13 +02005642 /* Reject any attempt to create a zero-length key so that we don't
5643 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5644 if( psa_get_key_bits( attributes ) == 0 )
5645 return( PSA_ERROR_INVALID_ARGUMENT );
5646
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005647 if( ! operation->can_output_key )
5648 return( PSA_ERROR_NOT_PERMITTED );
5649
Ronald Cron81709fc2020-11-14 12:10:32 +01005650 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5651 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005652#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5653 if( driver != NULL )
5654 {
5655 /* Deriving a key in a secure element is not implemented yet. */
5656 status = PSA_ERROR_NOT_SUPPORTED;
5657 }
5658#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005659 if( status == PSA_SUCCESS )
5660 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005661 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005662 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005663 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005664 }
5665 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005666 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005667 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005668 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005669
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005670 return( status );
5671}
5672
Gilles Peskineeab56e42018-07-12 17:12:33 +02005673
5674
5675/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005676/* Key derivation */
5677/****************************************************************/
5678
John Durkop07cc04a2020-11-16 22:08:34 -08005679#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005680static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005681 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005682 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005683{
John Durkop07cc04a2020-11-16 22:08:34 -08005684 int is_kdf_alg_supported;
5685
Janos Follath5fe19732019-06-20 15:09:30 +01005686 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5687 * initialisers for this union leave some bytes unspecified.) */
5688 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5689
Gilles Peskine969c5d62019-01-16 15:53:06 +01005690 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005691#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005692 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5693 is_kdf_alg_supported = 1;
5694 else
5695#endif
5696#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5697 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5698 is_kdf_alg_supported = 1;
5699 else
5700#endif
5701#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5702 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5703 is_kdf_alg_supported = 1;
5704 else
5705#endif
5706 is_kdf_alg_supported = 0;
5707
5708 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005709 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005710 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005711 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005712 if( hash_size == 0 )
5713 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005714 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5715 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005716 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005717 {
5718 return( PSA_ERROR_NOT_SUPPORTED );
5719 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005721 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005722 }
John Durkop07cc04a2020-11-16 22:08:34 -08005723
5724 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005725}
John Durkop07cc04a2020-11-16 22:08:34 -08005726#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005727
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005728psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005729 psa_algorithm_t alg )
5730{
5731 psa_status_t status;
5732
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005733 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005734 return( PSA_ERROR_BAD_STATE );
5735
Gilles Peskine6843c292019-01-18 16:44:49 +01005736 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5737 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005738#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005739 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005740 {
5741 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005742 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005743 }
5744 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5745 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005746 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005747 }
John Durkop07cc04a2020-11-16 22:08:34 -08005748#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005749 else
5750 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005751
5752 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005753 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005754 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005755}
5756
John Durkopd0321952020-10-29 21:37:36 -07005757#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005758static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005759 psa_algorithm_t hash_alg,
5760 psa_key_derivation_step_t step,
5761 const uint8_t *data,
5762 size_t data_length )
5763{
5764 psa_status_t status;
5765 switch( step )
5766 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005767 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005768 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005769 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005770 status = psa_hmac_setup_internal( &hkdf->hmac,
5771 data, data_length,
5772 hash_alg );
5773 if( status != PSA_SUCCESS )
5774 return( status );
5775 hkdf->state = HKDF_STATE_STARTED;
5776 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005777 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005778 /* If no salt was provided, use an empty salt. */
5779 if( hkdf->state == HKDF_STATE_INIT )
5780 {
5781 status = psa_hmac_setup_internal( &hkdf->hmac,
5782 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005783 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005784 if( status != PSA_SUCCESS )
5785 return( status );
5786 hkdf->state = HKDF_STATE_STARTED;
5787 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005788 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005789 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005790 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5791 data, data_length );
5792 if( status != PSA_SUCCESS )
5793 return( status );
5794 status = psa_hmac_finish_internal( &hkdf->hmac,
5795 hkdf->prk,
5796 sizeof( hkdf->prk ) );
5797 if( status != PSA_SUCCESS )
5798 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005799 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005800 hkdf->block_number = 0;
5801 hkdf->state = HKDF_STATE_KEYED;
5802 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005803 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005804 if( hkdf->state == HKDF_STATE_OUTPUT )
5805 return( PSA_ERROR_BAD_STATE );
5806 if( hkdf->info_set )
5807 return( PSA_ERROR_BAD_STATE );
5808 hkdf->info_length = data_length;
5809 if( data_length != 0 )
5810 {
5811 hkdf->info = mbedtls_calloc( 1, data_length );
5812 if( hkdf->info == NULL )
5813 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5814 memcpy( hkdf->info, data, data_length );
5815 }
5816 hkdf->info_set = 1;
5817 return( PSA_SUCCESS );
5818 default:
5819 return( PSA_ERROR_INVALID_ARGUMENT );
5820 }
5821}
John Durkop07cc04a2020-11-16 22:08:34 -08005822#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005823
John Durkop07cc04a2020-11-16 22:08:34 -08005824#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5825 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005826static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5827 const uint8_t *data,
5828 size_t data_length )
5829{
5830 if( prf->state != TLS12_PRF_STATE_INIT )
5831 return( PSA_ERROR_BAD_STATE );
5832
Janos Follathd6dce9f2019-07-04 09:11:38 +01005833 if( data_length != 0 )
5834 {
5835 prf->seed = mbedtls_calloc( 1, data_length );
5836 if( prf->seed == NULL )
5837 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005838
Janos Follathd6dce9f2019-07-04 09:11:38 +01005839 memcpy( prf->seed, data, data_length );
5840 prf->seed_length = data_length;
5841 }
Janos Follathf08e2652019-06-13 09:05:41 +01005842
5843 prf->state = TLS12_PRF_STATE_SEED_SET;
5844
5845 return( PSA_SUCCESS );
5846}
5847
Janos Follath81550542019-06-13 14:26:34 +01005848static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5849 psa_algorithm_t hash_alg,
5850 const uint8_t *data,
5851 size_t data_length )
5852{
5853 psa_status_t status;
5854 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5855 return( PSA_ERROR_BAD_STATE );
5856
5857 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5858 if( status != PSA_SUCCESS )
5859 return( status );
5860
5861 prf->state = TLS12_PRF_STATE_KEY_SET;
5862
5863 return( PSA_SUCCESS );
5864}
5865
Janos Follath63028dd2019-06-13 09:15:47 +01005866static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5867 const uint8_t *data,
5868 size_t data_length )
5869{
5870 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5871 return( PSA_ERROR_BAD_STATE );
5872
Janos Follathd6dce9f2019-07-04 09:11:38 +01005873 if( data_length != 0 )
5874 {
5875 prf->label = mbedtls_calloc( 1, data_length );
5876 if( prf->label == NULL )
5877 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005878
Janos Follathd6dce9f2019-07-04 09:11:38 +01005879 memcpy( prf->label, data, data_length );
5880 prf->label_length = data_length;
5881 }
Janos Follath63028dd2019-06-13 09:15:47 +01005882
5883 prf->state = TLS12_PRF_STATE_LABEL_SET;
5884
5885 return( PSA_SUCCESS );
5886}
5887
Janos Follathb03233e2019-06-11 15:30:30 +01005888static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5889 psa_algorithm_t hash_alg,
5890 psa_key_derivation_step_t step,
5891 const uint8_t *data,
5892 size_t data_length )
5893{
Janos Follathb03233e2019-06-11 15:30:30 +01005894 switch( step )
5895 {
Janos Follathf08e2652019-06-13 09:05:41 +01005896 case PSA_KEY_DERIVATION_INPUT_SEED:
5897 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005898 case PSA_KEY_DERIVATION_INPUT_SECRET:
5899 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005900 case PSA_KEY_DERIVATION_INPUT_LABEL:
5901 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005902 default:
5903 return( PSA_ERROR_INVALID_ARGUMENT );
5904 }
5905}
John Durkop07cc04a2020-11-16 22:08:34 -08005906#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5907 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5908
5909#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5910static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5911 psa_tls12_prf_key_derivation_t *prf,
5912 psa_algorithm_t hash_alg,
5913 const uint8_t *data,
5914 size_t data_length )
5915{
5916 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005917 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005918 uint8_t *cur = pms;
5919
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005920 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005921 return( PSA_ERROR_INVALID_ARGUMENT );
5922
5923 /* Quoting RFC 4279, Section 2:
5924 *
5925 * The premaster secret is formed as follows: if the PSK is N octets
5926 * long, concatenate a uint16 with the value N, N zero octets, a second
5927 * uint16 with the value N, and the PSK itself.
5928 */
5929
5930 *cur++ = ( data_length >> 8 ) & 0xff;
5931 *cur++ = ( data_length >> 0 ) & 0xff;
5932 memset( cur, 0, data_length );
5933 cur += data_length;
5934 *cur++ = pms[0];
5935 *cur++ = pms[1];
5936 memcpy( cur, data, data_length );
5937 cur += data_length;
5938
5939 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5940
5941 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5942 return( status );
5943}
Janos Follath6660f0e2019-06-17 08:44:03 +01005944
5945static psa_status_t psa_tls12_prf_psk_to_ms_input(
5946 psa_tls12_prf_key_derivation_t *prf,
5947 psa_algorithm_t hash_alg,
5948 psa_key_derivation_step_t step,
5949 const uint8_t *data,
5950 size_t data_length )
5951{
5952 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005953 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005954 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5955 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005956 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005957
5958 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5959}
John Durkop07cc04a2020-11-16 22:08:34 -08005960#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005961
Gilles Peskineb8965192019-09-24 16:21:10 +02005962/** Check whether the given key type is acceptable for the given
5963 * input step of a key derivation.
5964 *
5965 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5966 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5967 * Both secret and non-secret inputs can alternatively have the type
5968 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5969 * that the input was passed as a buffer rather than via a key object.
5970 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005971static int psa_key_derivation_check_input_type(
5972 psa_key_derivation_step_t step,
5973 psa_key_type_t key_type )
5974{
5975 switch( step )
5976 {
5977 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005978 if( key_type == PSA_KEY_TYPE_DERIVE )
5979 return( PSA_SUCCESS );
5980 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005981 return( PSA_SUCCESS );
5982 break;
5983 case PSA_KEY_DERIVATION_INPUT_LABEL:
5984 case PSA_KEY_DERIVATION_INPUT_SALT:
5985 case PSA_KEY_DERIVATION_INPUT_INFO:
5986 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005987 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5988 return( PSA_SUCCESS );
5989 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005990 return( PSA_SUCCESS );
5991 break;
5992 }
5993 return( PSA_ERROR_INVALID_ARGUMENT );
5994}
5995
Janos Follathb80a94e2019-06-12 15:54:46 +01005996static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005997 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005998 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005999 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006000 const uint8_t *data,
6001 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006002{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006003 psa_status_t status;
6004 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6005
6006 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006007 if( status != PSA_SUCCESS )
6008 goto exit;
6009
John Durkopd0321952020-10-29 21:37:36 -07006010#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006011 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006012 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006013 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006014 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006015 step, data, data_length );
6016 }
John Durkop07cc04a2020-11-16 22:08:34 -08006017 else
6018#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6019#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6020 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006021 {
Janos Follathb03233e2019-06-11 15:30:30 +01006022 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6023 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6024 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006025 }
John Durkop07cc04a2020-11-16 22:08:34 -08006026 else
6027#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6028#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6029 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006030 {
6031 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6032 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6033 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006034 }
6035 else
John Durkop07cc04a2020-11-16 22:08:34 -08006036#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006037 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006038 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006039 return( PSA_ERROR_BAD_STATE );
6040 }
6041
Gilles Peskine224b0d62019-09-23 18:13:17 +02006042exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006043 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006044 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006045 return( status );
6046}
6047
Janos Follath51f4a0f2019-06-14 11:35:55 +01006048psa_status_t psa_key_derivation_input_bytes(
6049 psa_key_derivation_operation_t *operation,
6050 psa_key_derivation_step_t step,
6051 const uint8_t *data,
6052 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006053{
Gilles Peskineb8965192019-09-24 16:21:10 +02006054 return( psa_key_derivation_input_internal( operation, step,
6055 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006056 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006057}
6058
Janos Follath51f4a0f2019-06-14 11:35:55 +01006059psa_status_t psa_key_derivation_input_key(
6060 psa_key_derivation_operation_t *operation,
6061 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006062 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006063{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006065 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006066 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006067
Ronald Cron5c522922020-11-14 16:35:34 +01006068 status = psa_get_and_lock_transparent_key_slot_with_policy(
6069 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006070 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006071 {
6072 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006073 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006074 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006075
6076 /* Passing a key object as a SECRET input unlocks the permission
6077 * to output to a key object. */
6078 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6079 operation->can_output_key = 1;
6080
Ronald Cronf95a2b12020-10-22 15:24:49 +02006081 status = psa_key_derivation_input_internal( operation,
6082 step, slot->attr.type,
6083 slot->data.key.data,
6084 slot->data.key.bytes );
6085
Ronald Cron5c522922020-11-14 16:35:34 +01006086 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006087
Ronald Cron5c522922020-11-14 16:35:34 +01006088 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006089}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006090
6091
6092
6093/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006094/* Key agreement */
6095/****************************************************************/
6096
John Durkop6ba40d12020-11-10 08:50:04 -08006097#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006098static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6099 size_t peer_key_length,
6100 const mbedtls_ecp_keypair *our_key,
6101 uint8_t *shared_secret,
6102 size_t shared_secret_size,
6103 size_t *shared_secret_length )
6104{
Steven Cooremand4867872020-08-05 16:31:39 +02006105 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006106 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006107 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006108 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006109 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006110 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006111
Steven Cooreman4fed4552020-08-03 14:46:03 +02006112 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6113 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006114 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006115 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006116 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006117 goto exit;
6118
Jaeden Amero97271b32019-01-10 19:38:51 +00006119 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006120 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006121 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006122 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006123 status = mbedtls_to_psa_error(
6124 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6125 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006126 goto exit;
6127
Jaeden Amero97271b32019-01-10 19:38:51 +00006128 status = mbedtls_to_psa_error(
6129 mbedtls_ecdh_calc_secret( &ecdh,
6130 shared_secret_length,
6131 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006132 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006133 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006134 if( status != PSA_SUCCESS )
6135 goto exit;
6136 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6137 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006138
6139exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006140 if( status != PSA_SUCCESS )
6141 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006142 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006143 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006144 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006145
Jaeden Amero97271b32019-01-10 19:38:51 +00006146 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006147}
John Durkop6ba40d12020-11-10 08:50:04 -08006148#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006149
Gilles Peskine01d718c2018-09-18 12:01:02 +02006150#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6151
Gilles Peskine0216fe12019-04-11 21:23:21 +02006152static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6153 psa_key_slot_t *private_key,
6154 const uint8_t *peer_key,
6155 size_t peer_key_length,
6156 uint8_t *shared_secret,
6157 size_t shared_secret_size,
6158 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006159{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006160 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006161 {
John Durkop6ba40d12020-11-10 08:50:04 -08006162#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006163 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006164 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006165 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006166 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006167 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006168 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006169 private_key->data.key.data,
6170 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006171 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006172 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006173 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006174 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006175 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006176 shared_secret, shared_secret_size,
6177 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006178 mbedtls_ecp_keypair_free( ecp );
6179 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006180 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006181#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006182 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006183 (void) private_key;
6184 (void) peer_key;
6185 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006186 (void) shared_secret;
6187 (void) shared_secret_size;
6188 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006189 return( PSA_ERROR_NOT_SUPPORTED );
6190 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006191}
6192
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006193/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006194 * to potentially free embedded data structures and wipe confidential data.
6195 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006196static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006197 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006198 psa_key_slot_t *private_key,
6199 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006200 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006201{
6202 psa_status_t status;
6203 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6204 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006205 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006206
6207 /* Step 1: run the secret agreement algorithm to generate the shared
6208 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006209 status = psa_key_agreement_raw_internal( ka_alg,
6210 private_key,
6211 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006212 shared_secret,
6213 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006214 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006215 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006216 goto exit;
6217
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006218 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006219 * the shared secret. A shared secret is permitted wherever a key
6220 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006221 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006222 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006223 shared_secret,
6224 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006225exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006226 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006227 return( status );
6228}
6229
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006230psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006231 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006232 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006233 const uint8_t *peer_key,
6234 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006235{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006236 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006237 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006238 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006239
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006240 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006241 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006242 status = psa_get_and_lock_transparent_key_slot_with_policy(
6243 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006244 if( status != PSA_SUCCESS )
6245 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006246 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006247 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006248 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006249 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006250 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006251 else
6252 {
6253 /* If a private key has been added as SECRET, we allow the derived
6254 * key material to be used as a key in PSA Crypto. */
6255 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6256 operation->can_output_key = 1;
6257 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006258
Ronald Cron5c522922020-11-14 16:35:34 +01006259 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006260
Ronald Cron5c522922020-11-14 16:35:34 +01006261 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006262}
6263
Gilles Peskinebe697d82019-05-16 18:00:41 +02006264psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006265 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006266 const uint8_t *peer_key,
6267 size_t peer_key_length,
6268 uint8_t *output,
6269 size_t output_size,
6270 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006271{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006272 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006273 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006274 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006275
6276 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6277 {
6278 status = PSA_ERROR_INVALID_ARGUMENT;
6279 goto exit;
6280 }
Ronald Cron5c522922020-11-14 16:35:34 +01006281 status = psa_get_and_lock_transparent_key_slot_with_policy(
6282 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006283 if( status != PSA_SUCCESS )
6284 goto exit;
6285
6286 status = psa_key_agreement_raw_internal( alg, slot,
6287 peer_key, peer_key_length,
6288 output, output_size,
6289 output_length );
6290
6291exit:
6292 if( status != PSA_SUCCESS )
6293 {
6294 /* If an error happens and is not handled properly, the output
6295 * may be used as a key to protect sensitive data. Arrange for such
6296 * a key to be random, which is likely to result in decryption or
6297 * verification errors. This is better than filling the buffer with
6298 * some constant data such as zeros, which would result in the data
6299 * being protected with a reproducible, easily knowable key.
6300 */
6301 psa_generate_random( output, output_size );
6302 *output_length = output_size;
6303 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006304
Ronald Cron5c522922020-11-14 16:35:34 +01006305 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006306
Ronald Cron5c522922020-11-14 16:35:34 +01006307 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006308}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006309
6310
Gilles Peskine30524eb2020-11-13 17:02:26 +01006311
Gilles Peskine86a440b2018-11-12 18:39:40 +01006312/****************************************************************/
6313/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006314/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006315
Gilles Peskine30524eb2020-11-13 17:02:26 +01006316/** Initialize the PSA random generator.
6317 */
6318static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6319{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006320#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6321 memset( rng, 0, sizeof( *rng ) );
6322#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6323
Gilles Peskine30524eb2020-11-13 17:02:26 +01006324 /* Set default configuration if
6325 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6326 if( rng->entropy_init == NULL )
6327 rng->entropy_init = mbedtls_entropy_init;
6328 if( rng->entropy_free == NULL )
6329 rng->entropy_free = mbedtls_entropy_free;
6330
6331 rng->entropy_init( &rng->entropy );
6332#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6333 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6334 /* The PSA entropy injection feature depends on using NV seed as an entropy
6335 * source. Add NV seed as an entropy source for PSA entropy injection. */
6336 mbedtls_entropy_add_source( &rng->entropy,
6337 mbedtls_nv_seed_poll, NULL,
6338 MBEDTLS_ENTROPY_BLOCK_SIZE,
6339 MBEDTLS_ENTROPY_SOURCE_STRONG );
6340#endif
6341
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006342 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006343#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006344}
6345
6346/** Deinitialize the PSA random generator.
6347 */
6348static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6349{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006350#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6351 memset( rng, 0, sizeof( *rng ) );
6352#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006353 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006354 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006355#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006356}
6357
6358/** Seed the PSA random generator.
6359 */
6360static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6361{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006362#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6363 /* Do nothing: the external RNG seeds itself. */
6364 (void) rng;
6365 return( PSA_SUCCESS );
6366#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006367 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006368 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6369 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006370 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006371#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006372}
6373
Gilles Peskinee59236f2018-01-27 23:32:46 +01006374psa_status_t psa_generate_random( uint8_t *output,
6375 size_t output_size )
6376{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006377 GUARD_MODULE_INITIALIZED;
6378
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006379#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6380
6381 size_t output_length = 0;
6382 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6383 output, output_size,
6384 &output_length );
6385 if( status != PSA_SUCCESS )
6386 return( status );
6387 /* Breaking up a request into smaller chunks is currently not supported
6388 * for the extrernal RNG interface. */
6389 if( output_length != output_size )
6390 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6391 return( PSA_SUCCESS );
6392
6393#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6394
Gilles Peskine71ddab92021-01-04 21:01:07 +01006395 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006396 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006397 size_t request_size =
6398 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6399 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6400 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006401 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6402 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006403 if( ret != 0 )
6404 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006405 output_size -= request_size;
6406 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006407 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006408 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006409#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006410}
6411
Gilles Peskine8814fc42020-12-14 15:33:44 +01006412/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006413 *
6414 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6415 * `psa_generate_random(...)`. The state parameter is ignored since the
6416 * PSA API doesn't support passing an explicit state.
6417 *
6418 * In the non-external case, psa_generate_random() calls an
6419 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6420 * and semantics as mbedtls_psa_get_random(). As an optimization,
6421 * instead of doing this back-and-forth between the PSA API and the
6422 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6423 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006424 */
6425#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6426int mbedtls_psa_get_random( void *p_rng,
6427 unsigned char *output,
6428 size_t output_size )
6429{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006430 /* This function takes a pointer to the RNG state because that's what
6431 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6432 * its own state internally and doesn't let the caller access that state.
6433 * So we just ignore the state parameter, and in practice we'll pass
6434 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006435 (void) p_rng;
6436 psa_status_t status = psa_generate_random( output, output_size );
6437 if( status == PSA_SUCCESS )
6438 return( 0 );
6439 else
6440 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6441}
6442#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6443
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006444#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6445#include "mbedtls/entropy_poll.h"
6446
Gilles Peskine7228da22019-07-15 11:06:15 +02006447psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006448 size_t seed_size )
6449{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006450 if( global_data.initialized )
6451 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006452
6453 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6454 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6455 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6456 return( PSA_ERROR_INVALID_ARGUMENT );
6457
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006458 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006459}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006460#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006461
John Durkop07cc04a2020-11-16 22:08:34 -08006462#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006463static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6464 size_t domain_parameters_size,
6465 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006467 size_t i;
6468 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006469
Gilles Peskinee56e8782019-04-26 17:34:02 +02006470 if( domain_parameters_size == 0 )
6471 {
6472 *exponent = 65537;
6473 return( PSA_SUCCESS );
6474 }
6475
6476 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6477 * support values that fit in a 32-bit integer, which is larger than
6478 * int on just about every platform anyway. */
6479 if( domain_parameters_size > sizeof( acc ) )
6480 return( PSA_ERROR_NOT_SUPPORTED );
6481 for( i = 0; i < domain_parameters_size; i++ )
6482 acc = ( acc << 8 ) | domain_parameters[i];
6483 if( acc > INT_MAX )
6484 return( PSA_ERROR_NOT_SUPPORTED );
6485 *exponent = acc;
6486 return( PSA_SUCCESS );
6487}
John Durkop07cc04a2020-11-16 22:08:34 -08006488#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006489
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006490static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006491 psa_key_slot_t *slot, size_t bits,
6492 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006493{
Gilles Peskine8e338702019-07-30 20:06:31 +02006494 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006495
Gilles Peskinee56e8782019-04-26 17:34:02 +02006496 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006497 return( PSA_ERROR_INVALID_ARGUMENT );
6498
Gilles Peskinee59236f2018-01-27 23:32:46 +01006499 if( key_type_is_raw_bytes( type ) )
6500 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006501 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006502
6503 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006504 if( status != PSA_SUCCESS )
6505 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006506
6507 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006508 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6509 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006510 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006511
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006512 status = psa_generate_random( slot->data.key.data,
6513 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006514 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006515 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006516
6517 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006518#if defined(MBEDTLS_DES_C)
6519 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006520 psa_des_set_key_parity( slot->data.key.data,
6521 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006522#endif /* MBEDTLS_DES_C */
6523 }
6524 else
6525
John Durkop07cc04a2020-11-16 22:08:34 -08006526#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006527 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006528 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006529 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006531 int exponent;
6532 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006533 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6534 return( PSA_ERROR_NOT_SUPPORTED );
6535 /* Accept only byte-aligned keys, for the same reasons as
6536 * in psa_import_rsa_key(). */
6537 if( bits % 8 != 0 )
6538 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006539 status = psa_read_rsa_exponent( domain_parameters,
6540 domain_parameters_size,
6541 &exponent );
6542 if( status != PSA_SUCCESS )
6543 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006544 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6545 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006546 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006547 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006548 (unsigned int) bits,
6549 exponent );
6550 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006551 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006552
6553 /* Make sure to always have an export representation available */
6554 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6555
Steven Cooreman75b74362020-07-28 14:30:13 +02006556 status = psa_allocate_buffer_to_slot( slot, bytes );
6557 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006558 {
6559 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006560 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006561 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006562
6563 status = psa_export_rsa_key( type,
6564 &rsa,
6565 slot->data.key.data,
6566 bytes,
6567 &slot->data.key.bytes );
6568 mbedtls_rsa_free( &rsa );
6569 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006570 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006571 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006572 }
6573 else
John Durkop07cc04a2020-11-16 22:08:34 -08006574#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006575
John Durkop9814fa22020-11-04 12:28:15 -08006576#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006577 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006578 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006579 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006580 mbedtls_ecp_group_id grp_id =
6581 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006582 const mbedtls_ecp_curve_info *curve_info =
6583 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006584 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006586 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006587 return( PSA_ERROR_NOT_SUPPORTED );
6588 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6589 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006590 mbedtls_ecp_keypair_init( &ecp );
6591 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006592 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006593 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006594 if( ret != 0 )
6595 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006596 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006597 return( mbedtls_to_psa_error( ret ) );
6598 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006599
6600
6601 /* Make sure to always have an export representation available */
6602 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006603 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6604 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006605 {
6606 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006607 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006608 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006609
6610 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006611 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6612
6613 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006614 if( status != PSA_SUCCESS ) {
6615 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006616 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006617 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006618 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006619 }
6620 else
John Durkop9814fa22020-11-04 12:28:15 -08006621#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006622 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006623 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006624 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006625
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006626 return( PSA_SUCCESS );
6627}
Darryl Green0c6575a2018-11-07 16:05:30 +00006628
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006629psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006630 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006631{
6632 psa_status_t status;
6633 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006634 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006635
Ronald Cron81709fc2020-11-14 12:10:32 +01006636 *key = MBEDTLS_SVC_KEY_ID_INIT;
6637
Gilles Peskine0f84d622019-09-12 19:03:13 +02006638 /* Reject any attempt to create a zero-length key so that we don't
6639 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6640 if( psa_get_key_bits( attributes ) == 0 )
6641 return( PSA_ERROR_INVALID_ARGUMENT );
6642
Ronald Cron81709fc2020-11-14 12:10:32 +01006643 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6644 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006645 if( status != PSA_SUCCESS )
6646 goto exit;
6647
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006648 status = psa_driver_wrapper_generate_key( attributes,
6649 slot );
6650 if( status != PSA_ERROR_NOT_SUPPORTED ||
6651 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6652 goto exit;
6653
6654 status = psa_generate_key_internal(
6655 slot, attributes->core.bits,
6656 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006657
6658exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006659 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006660 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006661 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006662 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006663
Darryl Green0c6575a2018-11-07 16:05:30 +00006664 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006665}
6666
6667
Gilles Peskinee59236f2018-01-27 23:32:46 +01006668
6669/****************************************************************/
6670/* Module setup */
6671/****************************************************************/
6672
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006673#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006674psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6675 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6676 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6677{
6678 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6679 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006680 global_data.rng.entropy_init = entropy_init;
6681 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006682 return( PSA_SUCCESS );
6683}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006684#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006685
Gilles Peskinee59236f2018-01-27 23:32:46 +01006686void mbedtls_psa_crypto_free( void )
6687{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006688 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006689 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6690 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006691 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006692 }
6693 /* Wipe all remaining data, including configuration.
6694 * In particular, this sets all state indicator to the value
6695 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006696 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006698 /* Unregister all secure element drivers, so that we restart from
6699 * a pristine state. */
6700 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006701#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006702}
6703
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006704#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6705/** Recover a transaction that was interrupted by a power failure.
6706 *
6707 * This function is called during initialization, before psa_crypto_init()
6708 * returns. If this function returns a failure status, the initialization
6709 * fails.
6710 */
6711static psa_status_t psa_crypto_recover_transaction(
6712 const psa_crypto_transaction_t *transaction )
6713{
6714 switch( transaction->unknown.type )
6715 {
6716 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6717 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006718 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006719 * is implemented.
6720 * https://github.com/ARMmbed/mbed-crypto/issues/218
6721 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006722 default:
6723 /* We found an unsupported transaction in the storage.
6724 * We don't know what state the storage is in. Give up. */
6725 return( PSA_ERROR_STORAGE_FAILURE );
6726 }
6727}
6728#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6729
Gilles Peskinee59236f2018-01-27 23:32:46 +01006730psa_status_t psa_crypto_init( void )
6731{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006732 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006733
Gilles Peskinec6b69072018-11-20 21:42:52 +01006734 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006735 if( global_data.initialized != 0 )
6736 return( PSA_SUCCESS );
6737
Gilles Peskine30524eb2020-11-13 17:02:26 +01006738 /* Initialize and seed the random generator. */
6739 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006740 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006741 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006742 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006743 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006744 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006745
Gilles Peskine66fb1262018-12-10 16:29:04 +01006746 status = psa_initialize_key_slots( );
6747 if( status != PSA_SUCCESS )
6748 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006749
Gilles Peskined9348f22019-10-01 15:22:29 +02006750#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6751 status = psa_init_all_se_drivers( );
6752 if( status != PSA_SUCCESS )
6753 goto exit;
6754#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6755
Gilles Peskine4b734222019-07-24 15:56:31 +02006756#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006757 status = psa_crypto_load_transaction( );
6758 if( status == PSA_SUCCESS )
6759 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006760 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6761 if( status != PSA_SUCCESS )
6762 goto exit;
6763 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006764 }
6765 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6766 {
6767 /* There's no transaction to complete. It's all good. */
6768 status = PSA_SUCCESS;
6769 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006770#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006771
Gilles Peskinec6b69072018-11-20 21:42:52 +01006772 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006773 global_data.initialized = 1;
6774
6775exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006776 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006777 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006778 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006779}
6780
6781#endif /* MBEDTLS_PSA_CRYPTO_C */