blob: d55599e9e352b2ad47ab724ccb216486f13daf39 [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"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035#include "psa_crypto_ecp.h"
36#include "psa_crypto_rsa.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020038#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020039#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010040#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010041/* Include internal declarations that are useful for implementing persistently
42 * stored keys. */
43#include "psa_crypto_storage.h"
44
Gilles Peskineb2b64d32020-12-14 16:43:58 +010045#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010046
Gilles Peskineb46bef22019-07-30 21:32:04 +020047#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#include <stdlib.h>
49#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020051#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#define mbedtls_calloc calloc
53#define mbedtls_free free
54#endif
55
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/blowfish.h"
62#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020063#include "mbedtls/chacha20.h"
64#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/cipher.h"
66#include "mbedtls/ccm.h"
67#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020069#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010071#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/error.h"
73#include "mbedtls/gcm.h"
74#include "mbedtls/md2.h"
75#include "mbedtls/md4.h"
76#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010077#include "mbedtls/md.h"
78#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010079#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010081#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000082#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010084#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085#include "mbedtls/sha1.h"
86#include "mbedtls/sha256.h"
87#include "mbedtls/sha512.h"
88#include "mbedtls/xtea.h"
89
Gilles Peskine996deb12018-08-01 15:45:45 +020090#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
91
Gilles Peskine9ef733f2018-02-07 21:05:37 +010092/* constant-time buffer comparison */
93static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
94{
95 size_t i;
96 unsigned char diff = 0;
97
98 for( i = 0; i < n; i++ )
99 diff |= a[i] ^ b[i];
100
101 return( diff );
102}
103
104
105
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100106/****************************************************************/
107/* Global data, support functions and library management */
108/****************************************************************/
109
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200110static int key_type_is_raw_bytes( psa_key_type_t type )
111{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200112 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200113}
114
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100115/* Values for psa_global_data_t::rng_state */
116#define RNG_NOT_INITIALIZED 0
117#define RNG_INITIALIZED 1
118#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100119
Gilles Peskine2d277862018-06-18 15:41:12 +0200120typedef struct
121{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100122 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100123 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100124 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100125} psa_global_data_t;
126
127static psa_global_data_t global_data;
128
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100129#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
130mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
131 &global_data.rng.drbg;
132#endif
133
itayzafrir0adf0fc2018-09-06 16:24:41 +0300134#define GUARD_MODULE_INITIALIZED \
135 if( global_data.initialized == 0 ) \
136 return( PSA_ERROR_BAD_STATE );
137
Steven Cooreman01164162020-07-20 15:31:37 +0200138psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100139{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100140 /* Mbed TLS error codes can combine a high-level error code and a
141 * low-level error code. The low-level error usually reflects the
142 * root cause better, so dispatch on that preferably. */
143 int low_level_ret = - ( -ret & 0x007f );
144 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145 {
146 case 0:
147 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100148
149 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
150 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
151 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
152 return( PSA_ERROR_NOT_SUPPORTED );
153 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
154 return( PSA_ERROR_HARDWARE_FAILURE );
155
156 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
157 return( PSA_ERROR_HARDWARE_FAILURE );
158
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
160 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
161 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
162 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
163 case MBEDTLS_ERR_ASN1_INVALID_DATA:
164 return( PSA_ERROR_INVALID_ARGUMENT );
165 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
166 return( PSA_ERROR_INSUFFICIENT_MEMORY );
167 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
168 return( PSA_ERROR_BUFFER_TOO_SMALL );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
173 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000181 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000182#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
183 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
184#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
186 return( PSA_ERROR_NOT_SUPPORTED );
187 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
188 return( PSA_ERROR_HARDWARE_FAILURE );
189
190 case MBEDTLS_ERR_CCM_BAD_INPUT:
191 return( PSA_ERROR_INVALID_ARGUMENT );
192 case MBEDTLS_ERR_CCM_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
195 return( PSA_ERROR_HARDWARE_FAILURE );
196
Gilles Peskine26869f22019-05-06 15:25:00 +0200197 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199
200 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
201 return( PSA_ERROR_BAD_STATE );
202 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
203 return( PSA_ERROR_INVALID_SIGNATURE );
204
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
206 return( PSA_ERROR_NOT_SUPPORTED );
207 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
208 return( PSA_ERROR_INVALID_ARGUMENT );
209 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
210 return( PSA_ERROR_INSUFFICIENT_MEMORY );
211 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
212 return( PSA_ERROR_INVALID_PADDING );
213 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200214 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
216 return( PSA_ERROR_INVALID_SIGNATURE );
217 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200218 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100219 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
220 return( PSA_ERROR_HARDWARE_FAILURE );
221
222 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
226 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100227 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
228 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
232 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
235 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
238 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
239 return( PSA_ERROR_NOT_SUPPORTED );
240 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
Gilles Peskinee59236f2018-01-27 23:32:46 +0100243 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
244 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
245 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
246 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100247
248 case MBEDTLS_ERR_GCM_AUTH_FAILED:
249 return( PSA_ERROR_INVALID_SIGNATURE );
250 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200251 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100252 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskine82e57d12020-11-13 21:31:17 +0100255#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
256 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100257 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
258 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100259 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
260 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
261 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
262 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
263 return( PSA_ERROR_NOT_SUPPORTED );
264 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
265 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
266#endif
267
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
269 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
270 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
271 return( PSA_ERROR_HARDWARE_FAILURE );
272
273 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
274 return( PSA_ERROR_NOT_SUPPORTED );
275 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_MD_ALLOC_FAILED:
278 return( PSA_ERROR_INSUFFICIENT_MEMORY );
279 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
280 return( PSA_ERROR_STORAGE_FAILURE );
281 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
282 return( PSA_ERROR_HARDWARE_FAILURE );
283
Gilles Peskinef76aa772018-10-29 19:24:33 +0100284 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
285 return( PSA_ERROR_STORAGE_FAILURE );
286 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
291 return( PSA_ERROR_BUFFER_TOO_SMALL );
292 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
299 return( PSA_ERROR_INSUFFICIENT_MEMORY );
300
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 case MBEDTLS_ERR_PK_ALLOC_FAILED:
302 return( PSA_ERROR_INSUFFICIENT_MEMORY );
303 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
304 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
305 return( PSA_ERROR_INVALID_ARGUMENT );
306 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100308 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
309 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
312 return( PSA_ERROR_NOT_SUPPORTED );
313 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
314 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
315 return( PSA_ERROR_NOT_PERMITTED );
316 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
317 return( PSA_ERROR_INVALID_ARGUMENT );
318 case MBEDTLS_ERR_PK_INVALID_ALG:
319 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
320 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
321 return( PSA_ERROR_NOT_SUPPORTED );
322 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
323 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100324 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
Gilles Peskineff2d2002019-05-06 15:26:23 +0200327 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
330 return( PSA_ERROR_NOT_SUPPORTED );
331
Gilles Peskinea5905292018-02-07 20:59:33 +0100332 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_RSA_INVALID_PADDING:
338 return( PSA_ERROR_INVALID_PADDING );
339 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
340 return( PSA_ERROR_HARDWARE_FAILURE );
341 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
342 return( PSA_ERROR_INVALID_ARGUMENT );
343 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
344 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200345 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100346 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
347 return( PSA_ERROR_INVALID_SIGNATURE );
348 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
349 return( PSA_ERROR_BUFFER_TOO_SMALL );
350 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100351 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100352 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
353 return( PSA_ERROR_NOT_SUPPORTED );
354 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
355 return( PSA_ERROR_HARDWARE_FAILURE );
356
357 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
358 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
359 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
360 return( PSA_ERROR_HARDWARE_FAILURE );
361
362 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
363 return( PSA_ERROR_INVALID_ARGUMENT );
364 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
365 return( PSA_ERROR_HARDWARE_FAILURE );
366
itayzafrir5c753392018-05-08 11:18:38 +0300367 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300368 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300369 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300370 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
371 return( PSA_ERROR_BUFFER_TOO_SMALL );
372 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
373 return( PSA_ERROR_NOT_SUPPORTED );
374 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
375 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
376 return( PSA_ERROR_INVALID_SIGNATURE );
377 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
378 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100379 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
380 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300381 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
382 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100383
Janos Follatha13b9052019-11-22 12:48:59 +0000384 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
385 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300386
Gilles Peskinee59236f2018-01-27 23:32:46 +0100387 default:
David Saadab4ecc272019-02-14 13:48:10 +0200388 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100389 }
390}
391
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200392
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200393
394
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100395/****************************************************************/
396/* Key management */
397/****************************************************************/
398
Gilles Peskine73167e12019-07-12 23:44:37 +0200399#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
400static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
401{
Gilles Peskine8e338702019-07-30 20:06:31 +0200402 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200403}
404#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
405
John Durkop07cc04a2020-11-16 22:08:34 -0800406/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
407 * current test driver in key_management.c is using this function
408 * when accelerators are used for ECC key pair and public key.
409 * Once that dependency is resolved these guards can be removed.
410 */
John Durkop9814fa22020-11-04 12:28:15 -0800411#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800412 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
413 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
414 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100415mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100416 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200417{
418 switch( curve )
419 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100420 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100421 switch( byte_length )
422 {
423 case PSA_BITS_TO_BYTES( 192 ):
424 return( MBEDTLS_ECP_DP_SECP192R1 );
425 case PSA_BITS_TO_BYTES( 224 ):
426 return( MBEDTLS_ECP_DP_SECP224R1 );
427 case PSA_BITS_TO_BYTES( 256 ):
428 return( MBEDTLS_ECP_DP_SECP256R1 );
429 case PSA_BITS_TO_BYTES( 384 ):
430 return( MBEDTLS_ECP_DP_SECP384R1 );
431 case PSA_BITS_TO_BYTES( 521 ):
432 return( MBEDTLS_ECP_DP_SECP521R1 );
433 default:
434 return( MBEDTLS_ECP_DP_NONE );
435 }
436 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100437
Paul Elliott8ff510a2020-06-02 17:19:28 +0100438 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100439 switch( byte_length )
440 {
441 case PSA_BITS_TO_BYTES( 256 ):
442 return( MBEDTLS_ECP_DP_BP256R1 );
443 case PSA_BITS_TO_BYTES( 384 ):
444 return( MBEDTLS_ECP_DP_BP384R1 );
445 case PSA_BITS_TO_BYTES( 512 ):
446 return( MBEDTLS_ECP_DP_BP512R1 );
447 default:
448 return( MBEDTLS_ECP_DP_NONE );
449 }
450 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100451
Paul Elliott8ff510a2020-06-02 17:19:28 +0100452 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100453 switch( byte_length )
454 {
455 case PSA_BITS_TO_BYTES( 255 ):
456 return( MBEDTLS_ECP_DP_CURVE25519 );
457 case PSA_BITS_TO_BYTES( 448 ):
458 return( MBEDTLS_ECP_DP_CURVE448 );
459 default:
460 return( MBEDTLS_ECP_DP_NONE );
461 }
462 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100463
Paul Elliott8ff510a2020-06-02 17:19:28 +0100464 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100465 switch( byte_length )
466 {
467 case PSA_BITS_TO_BYTES( 192 ):
468 return( MBEDTLS_ECP_DP_SECP192K1 );
469 case PSA_BITS_TO_BYTES( 224 ):
470 return( MBEDTLS_ECP_DP_SECP224K1 );
471 case PSA_BITS_TO_BYTES( 256 ):
472 return( MBEDTLS_ECP_DP_SECP256K1 );
473 default:
474 return( MBEDTLS_ECP_DP_NONE );
475 }
476 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100477
Gilles Peskine12313cd2018-06-20 00:20:32 +0200478 default:
479 return( MBEDTLS_ECP_DP_NONE );
480 }
481}
John Durkop9814fa22020-11-04 12:28:15 -0800482#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800483 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
484 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
485 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200486
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200487static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
488 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489{
490 /* Check that the bit size is acceptable for the key type */
491 switch( type )
492 {
493 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200495 case PSA_KEY_TYPE_DERIVE:
496 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497#if defined(MBEDTLS_AES_C)
498 case PSA_KEY_TYPE_AES:
499 if( bits != 128 && bits != 192 && bits != 256 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
501 break;
502#endif
503#if defined(MBEDTLS_CAMELLIA_C)
504 case PSA_KEY_TYPE_CAMELLIA:
505 if( bits != 128 && bits != 192 && bits != 256 )
506 return( PSA_ERROR_INVALID_ARGUMENT );
507 break;
508#endif
509#if defined(MBEDTLS_DES_C)
510 case PSA_KEY_TYPE_DES:
511 if( bits != 64 && bits != 128 && bits != 192 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
515#if defined(MBEDTLS_ARC4_C)
516 case PSA_KEY_TYPE_ARC4:
517 if( bits < 8 || bits > 2048 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200521#if defined(MBEDTLS_CHACHA20_C)
522 case PSA_KEY_TYPE_CHACHA20:
523 if( bits != 256 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527 default:
528 return( PSA_ERROR_NOT_SUPPORTED );
529 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200530 if( bits % 8 != 0 )
531 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200532
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533 return( PSA_SUCCESS );
534}
535
Gilles Peskineb46bef22019-07-30 21:32:04 +0200536/** Return the size of the key in the given slot, in bits.
537 *
538 * \param[in] slot A key slot.
539 *
540 * \return The key size in bits, read from the metadata in the slot.
541 */
542static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
543{
544 return( slot->attr.bits );
545}
546
Steven Cooreman75b74362020-07-28 14:30:13 +0200547/** Try to allocate a buffer to an empty key slot.
548 *
549 * \param[in,out] slot Key slot to attach buffer to.
550 * \param[in] buffer_length Requested size of the buffer.
551 *
552 * \retval #PSA_SUCCESS
553 * The buffer has been successfully allocated.
554 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
555 * Not enough memory was available for allocation.
556 * \retval #PSA_ERROR_ALREADY_EXISTS
557 * Trying to allocate a buffer to a non-empty key slot.
558 */
559static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
560 size_t buffer_length )
561{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100562 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200563 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200564
Ronald Cronea0f8a62020-11-25 17:52:23 +0100565 slot->key.data = mbedtls_calloc( 1, buffer_length );
566 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200567 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200568
Ronald Cronea0f8a62020-11-25 17:52:23 +0100569 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200570 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200571}
572
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200573psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
574 const uint8_t* data,
575 size_t data_length )
576{
577 psa_status_t status = psa_allocate_buffer_to_slot( slot,
578 data_length );
579 if( status != PSA_SUCCESS )
580 return( status );
581
Ronald Cronea0f8a62020-11-25 17:52:23 +0100582 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200583 return( PSA_SUCCESS );
584}
585
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200586/** Import key data into a slot.
587 *
588 * `slot->type` must have been set previously.
589 * This function assumes that the slot does not contain any key material yet.
590 * On failure, the slot content is unchanged.
591 *
592 * Persistent storage is not affected.
593 *
594 * \param[in,out] slot The key slot to import data into.
595 * Its `type` field must have previously been set to
596 * the desired key type.
597 * It must not contain any key material yet.
598 * \param[in] data Buffer containing the key material to parse and import.
599 * \param data_length Size of \p data in bytes.
600 *
601 * \retval #PSA_SUCCESS
602 * \retval #PSA_ERROR_INVALID_ARGUMENT
603 * \retval #PSA_ERROR_NOT_SUPPORTED
604 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
605 */
606static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
607 const uint8_t *data,
608 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200610 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +0200611 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100612
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200613 /* zero-length keys are never supported. */
614 if( data_length == 0 )
615 return( PSA_ERROR_NOT_SUPPORTED );
616
Gilles Peskine8e338702019-07-30 20:06:31 +0200617 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618 {
Steven Cooreman04524762020-10-13 17:43:44 +0200619 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200620
Steven Cooreman75b74362020-07-28 14:30:13 +0200621 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
622 if( data_length > SIZE_MAX / 8 )
623 return( PSA_ERROR_NOT_SUPPORTED );
624
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200625 /* Enforce a size limit, and in particular ensure that the bit
626 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200627 if( bit_size > PSA_MAX_KEY_BITS )
628 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200629
630 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200631 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200632 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200633
634 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200635 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +0200636 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200637 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200638
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200639 /* Write the actual key size to the slot.
640 * psa_start_key_creation() wrote the size declared by the
641 * caller, which may be 0 (meaning unspecified) or wrong. */
642 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +0100643
644 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200646 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200647 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200648 /* Try validation through accelerators first. */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200649 psa_key_attributes_t attributes = {
650 .core = slot->attr
651 };
Ronald Cron83282872020-11-22 14:02:39 +0100652
653 status = psa_allocate_buffer_to_slot( slot, data_length );
654 if( status != PSA_SUCCESS )
655 return( status );
656
657 bit_size = slot->attr.bits;
658 status = psa_driver_wrapper_import_key( &attributes,
659 data, data_length,
660 slot->key.data,
661 slot->key.bytes,
662 &slot->key.bytes,
663 &bit_size );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200664 if( status == PSA_SUCCESS )
665 {
Ronald Cron83282872020-11-22 14:02:39 +0100666 if( slot->attr.bits == 0 )
667 slot->attr.bits = (psa_key_bits_t) bit_size;
668 else if( bit_size != slot->attr.bits )
669 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200670
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200671 return( PSA_SUCCESS );
672 }
Ronald Cron83282872020-11-22 14:02:39 +0100673 else
674 {
675 if( status != PSA_ERROR_NOT_SUPPORTED )
676 return( status );
677 }
678
679 mbedtls_platform_zeroize( slot->key.data, data_length );
680 mbedtls_free( slot->key.data );
681 slot->key.data = NULL;
682 slot->key.bytes = 0;
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200683
684 /* Key format is not supported by any accelerator, try software fallback
685 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -0800686#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
687 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200688 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
689 {
Ronald Cron13f8b092020-11-29 10:48:58 +0100690 status = psa_allocate_buffer_to_slot( slot, data_length );
691 if( status != PSA_SUCCESS )
692 return( status );
693
Ronald Cronb14dbbe2020-11-22 16:36:49 +0100694 status = mbedtls_psa_ecp_import_key( &attributes,
695 data, data_length,
696 slot->key.data, data_length,
697 &slot->key.bytes,
698 &bit_size );
Ronald Cron79cc5482020-11-08 14:54:25 +0100699 slot->attr.bits = (psa_key_bits_t) bit_size;
Ronald Cron13f8b092020-11-29 10:48:58 +0100700 return( status );
Steven Cooreman40120f62020-10-29 11:42:22 +0100701 }
John Durkop9814fa22020-11-04 12:28:15 -0800702#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
703 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700704#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
705 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +0100706 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200707 {
Ronald Cron8f813ee2020-11-07 18:22:09 +0100708 status = psa_allocate_buffer_to_slot( slot, data_length );
709 if( status != PSA_SUCCESS )
710 return( status );
711
Ronald Cronb6420e32020-11-22 16:15:38 +0100712 status = mbedtls_psa_rsa_import_key( &attributes,
713 data, data_length,
714 slot->key.data, data_length,
715 &slot->key.bytes,
716 &bit_size );
Ronald Cron4f2a7f02020-11-08 14:09:44 +0100717 slot->attr.bits = (psa_key_bits_t) bit_size;
Ronald Cron8f813ee2020-11-07 18:22:09 +0100718 return( status );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200719 }
John Durkop9814fa22020-11-04 12:28:15 -0800720#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
721 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +0100722
723 /* Fell through the fallback as well, so have nothing else to try. */
724 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100725 }
726 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100727 {
Steven Cooreman19fd5742020-07-24 23:31:01 +0200728 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100729 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +0100730 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100731}
732
Gilles Peskinef603c712019-01-19 13:40:11 +0100733/** Calculate the intersection of two algorithm usage policies.
734 *
735 * Return 0 (which allows no operation) on incompatibility.
736 */
737static psa_algorithm_t psa_key_policy_algorithm_intersection(
738 psa_algorithm_t alg1,
739 psa_algorithm_t alg2 )
740{
Gilles Peskine549ea862019-05-22 11:45:59 +0200741 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100742 if( alg1 == alg2 )
743 return( alg1 );
744 /* If the policies are from the same hash-and-sign family, check
745 * if one is a wildcard. If so the other has the specific algorithm. */
746 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
747 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
748 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
749 {
750 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
751 return( alg2 );
752 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
753 return( alg1 );
754 }
755 /* If the policies are incompatible, allow nothing. */
756 return( 0 );
757}
758
Gilles Peskined6f371b2019-05-10 19:33:38 +0200759static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
760 psa_algorithm_t requested_alg )
761{
Gilles Peskine549ea862019-05-22 11:45:59 +0200762 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200763 if( requested_alg == policy_alg )
764 return( 1 );
765 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200766 * and requested_alg is the same hash-and-sign family with any hash,
767 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200768 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
769 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
770 {
771 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
772 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
773 }
Steven Cooremance48e852020-10-05 16:02:45 +0200774 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200775 * a key derivation with that key agreement should also be allowed. This
776 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200777 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
778 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
779 {
780 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
781 policy_alg );
782 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200783 /* If it isn't permitted, it's forbidden. */
784 return( 0 );
785}
786
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100787/** Test whether a policy permits an algorithm.
788 *
789 * The caller must test usage flags separately.
790 */
791static int psa_key_policy_permits( const psa_key_policy_t *policy,
792 psa_algorithm_t alg )
793{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200794 return( psa_key_algorithm_permits( policy->alg, alg ) ||
795 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100796}
797
Gilles Peskinef603c712019-01-19 13:40:11 +0100798/** Restrict a key policy based on a constraint.
799 *
800 * \param[in,out] policy The policy to restrict.
801 * \param[in] constraint The policy constraint to apply.
802 *
803 * \retval #PSA_SUCCESS
804 * \c *policy contains the intersection of the original value of
805 * \c *policy and \c *constraint.
806 * \retval #PSA_ERROR_INVALID_ARGUMENT
807 * \c *policy and \c *constraint are incompatible.
808 * \c *policy is unchanged.
809 */
810static psa_status_t psa_restrict_key_policy(
811 psa_key_policy_t *policy,
812 const psa_key_policy_t *constraint )
813{
814 psa_algorithm_t intersection_alg =
815 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200816 psa_algorithm_t intersection_alg2 =
817 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100818 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
819 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200820 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
821 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100822 policy->usage &= constraint->usage;
823 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200824 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100825 return( PSA_SUCCESS );
826}
827
Ronald Cron5c522922020-11-14 16:35:34 +0100828/** Get the description of a key given its identifier and policy constraints
829 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200830 *
Ronald Cron5c522922020-11-14 16:35:34 +0100831 * The key must have allow all the usage flags set in \p usage. If \p alg is
832 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +0100833 *
Ronald Cron5c522922020-11-14 16:35:34 +0100834 * In case of a persistent key, the function loads the description of the key
835 * into a key slot if not already done.
836 *
837 * On success, the returned key slot is locked. It is the responsibility of
838 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200839 */
Ronald Cron5c522922020-11-14 16:35:34 +0100840static psa_status_t psa_get_and_lock_key_slot_with_policy(
841 mbedtls_svc_key_id_t key,
842 psa_key_slot_t **p_slot,
843 psa_key_usage_t usage,
844 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100845{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200846 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
847 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100848
Ronald Cron5c522922020-11-14 16:35:34 +0100849 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100850 if( status != PSA_SUCCESS )
851 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200852 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100853
854 /* Enforce that usage policy for the key slot contains all the flags
855 * required by the usage parameter. There is one exception: public
856 * keys can always be exported, so we treat public key objects as
857 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200858 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100859 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200860
861 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +0200862 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200863 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100864
865 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200866 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200867 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +0100868
Darryl Green06fd18d2018-07-16 11:21:11 +0100869 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200870
871error:
872 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100873 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200874
875 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100876}
Darryl Green940d72c2018-07-13 13:18:51 +0100877
Ronald Cron5c522922020-11-14 16:35:34 +0100878/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200879 *
880 * A transparent key is a key for which the key material is directly
881 * available, as opposed to a key in a secure element.
882 *
Ronald Cron5c522922020-11-14 16:35:34 +0100883 * This is a temporary function to use instead of
884 * psa_get_and_lock_key_slot_with_policy() until secure element support is
885 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200886 *
Ronald Cron5c522922020-11-14 16:35:34 +0100887 * On success, the returned key slot is locked. It is the responsibility of the
888 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200889 */
890#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +0100891static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
892 mbedtls_svc_key_id_t key,
893 psa_key_slot_t **p_slot,
894 psa_key_usage_t usage,
895 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200896{
Ronald Cron5c522922020-11-14 16:35:34 +0100897 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
898 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200899 if( status != PSA_SUCCESS )
900 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200901
Gilles Peskineadad8132019-07-25 11:31:23 +0200902 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200903 {
Ronald Cron5c522922020-11-14 16:35:34 +0100904 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200905 *p_slot = NULL;
906 return( PSA_ERROR_NOT_SUPPORTED );
907 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200908
Gilles Peskine28f8f302019-07-24 13:30:31 +0200909 return( PSA_SUCCESS );
910}
911#else /* MBEDTLS_PSA_CRYPTO_SE_C */
912/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +0100913#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
914 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200915#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
916
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100917/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100918static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000919{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100920 /* Data pointer will always be either a valid pointer or NULL in an
921 * initialized slot, so we can just free it. */
922 if( slot->key.data != NULL )
923 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
924
925 mbedtls_free( slot->key.data );
926 slot->key.data = NULL;
927 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000928
929 return( PSA_SUCCESS );
930}
931
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100932/** Completely wipe a slot in memory, including its policy.
933 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100934psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100935{
936 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100937
938 /*
939 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100940 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +0100941 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
942 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +0100943 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +0100944 */
Ronald Cron5c522922020-11-14 16:35:34 +0100945 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +0100946 {
947#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +0100948 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +0100949#endif
Ronald Cronddd3d052020-10-30 14:07:07 +0100950 status = PSA_ERROR_CORRUPTION_DETECTED;
951 }
952
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200953 /* Multipart operations may still be using the key. This is safe
954 * because all multipart operation objects are independent from
955 * the key slot: if they need to access the key after the setup
956 * phase, they have a copy of the key. Note that this means that
957 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100958 /* At this point, key material and other type-specific content has
959 * been wiped. Clear remaining metadata. We can call memset and not
960 * zeroize because the metadata is not particularly sensitive. */
961 memset( slot, 0, sizeof( *slot ) );
962 return( status );
963}
964
Ronald Croncf56a0a2020-08-04 09:51:30 +0200965psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100967 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +0200968 psa_status_t status; /* status of the last operation */
969 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200970#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
971 psa_se_drv_table_entry_t *driver;
972#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973
Ronald Croncf56a0a2020-08-04 09:51:30 +0200974 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200975 return( PSA_SUCCESS );
976
Ronald Cronf2911112020-10-29 17:51:10 +0100977 /*
Ronald Cron19daca92020-11-10 18:08:03 +0100978 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +0100979 * key, this will load the key description from persistent memory if not
980 * done yet. We cannot avoid this loading as without it we don't know if
981 * the key is operated by an SE or not and this information is needed by
982 * the current implementation.
983 */
Ronald Cron5c522922020-11-14 16:35:34 +0100984 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200985 if( status != PSA_SUCCESS )
986 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +0200987
Ronald Cronf2911112020-10-29 17:51:10 +0100988 /*
989 * If the key slot containing the key description is under access by the
990 * library (apart from the present access), the key cannot be destroyed
991 * yet. For the time being, just return in error. Eventually (to be
992 * implemented), the key should be destroyed when all accesses have
993 * stopped.
994 */
Ronald Cron5c522922020-11-14 16:35:34 +0100995 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +0100996 {
Ronald Cron5c522922020-11-14 16:35:34 +0100997 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +0100998 return( PSA_ERROR_GENERIC_ERROR );
999 }
1000
Gilles Peskine354f7672019-07-12 23:46:38 +02001001#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001002 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001003 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001004 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001005 /* For a key in a secure element, we need to do three things:
1006 * remove the key file in internal storage, destroy the
1007 * key inside the secure element, and update the driver's
1008 * persistent data. Start a transaction that will encompass these
1009 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001010 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001011 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001012 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001013 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001014 status = psa_crypto_save_transaction( );
1015 if( status != PSA_SUCCESS )
1016 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001017 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001018 /* We should still try to destroy the key in the secure
1019 * element and the key metadata in storage. This is especially
1020 * important if the error is that the storage is full.
1021 * But how to do it exactly without risking an inconsistent
1022 * state after a reset?
1023 * https://github.com/ARMmbed/mbed-crypto/issues/215
1024 */
1025 overall_status = status;
1026 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001027 }
1028
Ronald Cronea0f8a62020-11-25 17:52:23 +01001029 status = psa_destroy_se_key( driver,
1030 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001031 if( overall_status == PSA_SUCCESS )
1032 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001033 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001034#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1035
Darryl Greend49a4992018-06-18 17:27:26 +01001036#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001037 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001038 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001039 status = psa_destroy_persistent_key( slot->attr.id );
1040 if( overall_status == PSA_SUCCESS )
1041 overall_status = status;
1042
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001043 /* TODO: other slots may have a copy of the same key. We should
1044 * invalidate them.
1045 * https://github.com/ARMmbed/mbed-crypto/issues/214
1046 */
Darryl Greend49a4992018-06-18 17:27:26 +01001047 }
1048#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001049
Gilles Peskinefc762652019-07-22 19:30:34 +02001050#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1051 if( driver != NULL )
1052 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001053 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001054 if( overall_status == PSA_SUCCESS )
1055 overall_status = status;
1056 status = psa_crypto_stop_transaction( );
1057 if( overall_status == PSA_SUCCESS )
1058 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001059 }
1060#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1061
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001062#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1063exit:
1064#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001065 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001066 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1067 if( overall_status == PSA_SUCCESS )
1068 overall_status = status;
1069 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070}
1071
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001072void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001073{
Gilles Peskineb699f072019-04-26 16:06:02 +02001074 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001075 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001076}
1077
Gilles Peskineb699f072019-04-26 16:06:02 +02001078psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1079 psa_key_type_t type,
1080 const uint8_t *data,
1081 size_t data_length )
1082{
1083 uint8_t *copy = NULL;
1084
1085 if( data_length != 0 )
1086 {
1087 copy = mbedtls_calloc( 1, data_length );
1088 if( copy == NULL )
1089 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1090 memcpy( copy, data, data_length );
1091 }
1092 /* After this point, this function is guaranteed to succeed, so it
1093 * can start modifying `*attributes`. */
1094
1095 if( attributes->domain_parameters != NULL )
1096 {
1097 mbedtls_free( attributes->domain_parameters );
1098 attributes->domain_parameters = NULL;
1099 attributes->domain_parameters_size = 0;
1100 }
1101
1102 attributes->domain_parameters = copy;
1103 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001104 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001105 return( PSA_SUCCESS );
1106}
1107
1108psa_status_t psa_get_key_domain_parameters(
1109 const psa_key_attributes_t *attributes,
1110 uint8_t *data, size_t data_size, size_t *data_length )
1111{
1112 if( attributes->domain_parameters_size > data_size )
1113 return( PSA_ERROR_BUFFER_TOO_SMALL );
1114 *data_length = attributes->domain_parameters_size;
1115 if( attributes->domain_parameters_size != 0 )
1116 memcpy( data, attributes->domain_parameters,
1117 attributes->domain_parameters_size );
1118 return( PSA_SUCCESS );
1119}
1120
John Durkop07cc04a2020-11-16 22:08:34 -08001121#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001122 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001123static psa_status_t psa_get_rsa_public_exponent(
1124 const mbedtls_rsa_context *rsa,
1125 psa_key_attributes_t *attributes )
1126{
1127 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001129 uint8_t *buffer = NULL;
1130 size_t buflen;
1131 mbedtls_mpi_init( &mpi );
1132
1133 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1134 if( ret != 0 )
1135 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001136 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1137 {
1138 /* It's the default value, which is reported as an empty string,
1139 * so there's nothing to do. */
1140 goto exit;
1141 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001142
1143 buflen = mbedtls_mpi_size( &mpi );
1144 buffer = mbedtls_calloc( 1, buflen );
1145 if( buffer == NULL )
1146 {
1147 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1148 goto exit;
1149 }
1150 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1151 if( ret != 0 )
1152 goto exit;
1153 attributes->domain_parameters = buffer;
1154 attributes->domain_parameters_size = buflen;
1155
1156exit:
1157 mbedtls_mpi_free( &mpi );
1158 if( ret != 0 )
1159 mbedtls_free( buffer );
1160 return( mbedtls_to_psa_error( ret ) );
1161}
John Durkop07cc04a2020-11-16 22:08:34 -08001162#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001163 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001164
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001165/** Retrieve all the publicly-accessible attributes of a key.
1166 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001167psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001168 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001169{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001171 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001172 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001173
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001174 psa_reset_key_attributes( attributes );
1175
Ronald Cron5c522922020-11-14 16:35:34 +01001176 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001177 if( status != PSA_SUCCESS )
1178 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001179
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001180 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001181 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1182 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1183
1184#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1185 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001186 psa_set_key_slot_number( attributes,
1187 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001188#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001189
Gilles Peskine8e338702019-07-30 20:06:31 +02001190 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001191 {
John Durkop07cc04a2020-11-16 22:08:34 -08001192#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001193 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001194 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001195 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001196#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001197 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001198 * is not yet implemented.
1199 * https://github.com/ARMmbed/mbed-crypto/issues/216
1200 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001201 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001202 break;
1203#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001204 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001205 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001206
Ronald Cron90857082020-11-25 14:58:33 +01001207 status = mbedtls_psa_rsa_load_representation(
1208 slot->attr.type,
1209 slot->key.data,
1210 slot->key.bytes,
1211 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001212 if( status != PSA_SUCCESS )
1213 break;
1214
Steven Cooremana2371e52020-07-28 14:30:39 +02001215 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001216 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001217 mbedtls_rsa_free( rsa );
1218 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001219 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001221#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001222 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 default:
1224 /* Nothing else to do. */
1225 break;
1226 }
1227
1228 if( status != PSA_SUCCESS )
1229 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001230
Ronald Cron5c522922020-11-14 16:35:34 +01001231 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001232
Ronald Cron5c522922020-11-14 16:35:34 +01001233 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001234}
1235
Gilles Peskinec8000c02019-08-02 20:15:51 +02001236#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1237psa_status_t psa_get_key_slot_number(
1238 const psa_key_attributes_t *attributes,
1239 psa_key_slot_number_t *slot_number )
1240{
1241 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1242 {
1243 *slot_number = attributes->slot_number;
1244 return( PSA_SUCCESS );
1245 }
1246 else
1247 return( PSA_ERROR_INVALID_ARGUMENT );
1248}
1249#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1250
Ronald Crond18b5f82020-11-25 16:46:05 +01001251static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1252 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001253 uint8_t *data,
1254 size_t data_size,
1255 size_t *data_length )
1256{
Ronald Crond18b5f82020-11-25 16:46:05 +01001257 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001258 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001259 memcpy( data, key_buffer, key_buffer_size );
1260 memset( data + key_buffer_size, 0,
1261 data_size - key_buffer_size );
1262 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001263 return( PSA_SUCCESS );
1264}
1265
Ronald Cron7285cda2020-11-26 14:46:37 +01001266psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001267 const psa_key_attributes_t *attributes,
1268 const uint8_t *key_buffer, size_t key_buffer_size,
1269 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001270{
Ronald Crond18b5f82020-11-25 16:46:05 +01001271 psa_key_type_t type = attributes->core.type;
1272
Ronald Crond18b5f82020-11-25 16:46:05 +01001273 if( key_type_is_raw_bytes( type ) ||
1274 PSA_KEY_TYPE_IS_RSA( type ) ||
1275 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001276 {
1277 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001278 key_buffer, key_buffer_size,
1279 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001280 }
1281 else
1282 {
1283 /* This shouldn't happen in the reference implementation, but
1284 it is valid for a special-purpose implementation to omit
1285 support for exporting certain key types. */
1286 return( PSA_ERROR_NOT_SUPPORTED );
1287 }
1288}
1289
1290psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1291 uint8_t *data,
1292 size_t data_size,
1293 size_t *data_length )
1294{
1295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1296 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1297 psa_key_slot_t *slot;
1298
1299 /* Set the key to empty now, so that even when there are errors, we always
1300 * set data_length to a value between 0 and data_size. On error, setting
1301 * the key to empty is a good choice because an empty key representation is
1302 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001303 *data_length = 0;
1304
Ronald Cron9486f9d2020-11-26 13:36:23 +01001305 /* Export requires the EXPORT flag. There is an exception for public keys,
1306 * which don't require any flag, but
1307 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1308 */
1309 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1310 PSA_KEY_USAGE_EXPORT, 0 );
1311 if( status != PSA_SUCCESS )
1312 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001313
Gilles Peskinef9168942019-09-12 19:20:29 +02001314 /* Reject a zero-length output buffer now, since this can never be a
1315 * valid key representation. This way we know that data must be a valid
1316 * pointer and we can do things like memset(data, ..., data_size). */
1317 if( data_size == 0 )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001318 {
1319 status = PSA_ERROR_BUFFER_TOO_SMALL;
1320 goto exit;
1321 }
1322
Ronald Crond18b5f82020-11-25 16:46:05 +01001323 psa_key_attributes_t attributes = {
1324 .core = slot->attr
1325 };
Ronald Cron67227982020-11-26 15:16:05 +01001326 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001327 slot->key.data, slot->key.bytes,
1328 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001329
1330exit:
1331 unlock_status = psa_unlock_key_slot( slot );
1332
1333 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1334}
1335
Ronald Cron7285cda2020-11-26 14:46:37 +01001336psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001337 const psa_key_attributes_t *attributes,
1338 const uint8_t *key_buffer,
1339 size_t key_buffer_size,
1340 uint8_t *data,
1341 size_t data_size,
1342 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001343{
Ronald Crond18b5f82020-11-25 16:46:05 +01001344 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001345
Ronald Crond18b5f82020-11-25 16:46:05 +01001346 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001347 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001348 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001349 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001350 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001351 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001352 key_buffer, key_buffer_size,
1353 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001354 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001355
Ronald Crond18b5f82020-11-25 16:46:05 +01001356 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001357 {
John Durkop0e005192020-10-31 22:06:54 -07001358#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1359 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001360 return( mbedtls_psa_rsa_export_public_key( attributes,
1361 key_buffer,
1362 key_buffer_size,
1363 data,
1364 data_size,
1365 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001366#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001367 /* We don't know how to convert a private RSA key to public. */
1368 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001369#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1370 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001371 }
1372 else
Moran Pekera998bc62018-04-16 18:16:20 +03001373 {
John Durkop6ba40d12020-11-10 08:50:04 -08001374#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1375 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001376 return( mbedtls_psa_ecp_export_public_key( attributes,
1377 key_buffer,
1378 key_buffer_size,
1379 data,
1380 data_size,
1381 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001382#else
1383 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001384 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001385#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1386 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001387 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001388 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001389 else
1390 {
1391 /* This shouldn't happen in the reference implementation, but
1392 it is valid for a special-purpose implementation to omit
1393 support for exporting certain key types. */
1394 return( PSA_ERROR_NOT_SUPPORTED );
1395 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001396}
Ronald Crond18b5f82020-11-25 16:46:05 +01001397
Ronald Croncf56a0a2020-08-04 09:51:30 +02001398psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001399 uint8_t *data,
1400 size_t data_size,
1401 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001402{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001404 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001405 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001406
1407 /* Set the key to empty now, so that even when there are errors, we always
1408 * set data_length to a value between 0 and data_size. On error, setting
1409 * the key to empty is a good choice because an empty key representation is
1410 * unlikely to be accepted anywhere. */
1411 *data_length = 0;
1412
1413 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001414 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001415 if( status != PSA_SUCCESS )
1416 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001417
Ronald Cron9486f9d2020-11-26 13:36:23 +01001418 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1419 {
1420 status = PSA_ERROR_INVALID_ARGUMENT;
1421 goto exit;
1422 }
1423
1424 /* Reject a zero-length output buffer now, since this can never be a
1425 * valid key representation. This way we know that data must be a valid
1426 * pointer and we can do things like memset(data, ..., data_size). */
1427 if( data_size == 0 )
1428 {
1429 status = PSA_ERROR_BUFFER_TOO_SMALL;
1430 goto exit;
1431 }
1432
Ronald Crond18b5f82020-11-25 16:46:05 +01001433 psa_key_attributes_t attributes = {
1434 .core = slot->attr
1435 };
Ronald Cron67227982020-11-26 15:16:05 +01001436 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001437 &attributes, slot->key.data, slot->key.bytes,
1438 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001439
1440exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001441 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001442
Ronald Cron5c522922020-11-14 16:35:34 +01001443 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001444}
1445
Gilles Peskine91e8c332019-08-02 19:19:39 +02001446#if defined(static_assert)
1447static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1448 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001449static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001450 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001451static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001452 "One or more key attribute flag is listed as both internal-only and external-only" );
1453#endif
1454
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001455/** Validate that a key policy is internally well-formed.
1456 *
1457 * This function only rejects invalid policies. It does not validate the
1458 * consistency of the policy with respect to other attributes of the key
1459 * such as the key type.
1460 */
1461static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001462{
1463 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001464 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001465 PSA_KEY_USAGE_ENCRYPT |
1466 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001467 PSA_KEY_USAGE_SIGN_HASH |
1468 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001469 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1470 return( PSA_ERROR_INVALID_ARGUMENT );
1471
Gilles Peskine4747d192019-04-17 15:05:45 +02001472 return( PSA_SUCCESS );
1473}
1474
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001475/** Validate the internal consistency of key attributes.
1476 *
1477 * This function only rejects invalid attribute values. If does not
1478 * validate the consistency of the attributes with any key data that may
1479 * be involved in the creation of the key.
1480 *
1481 * Call this function early in the key creation process.
1482 *
1483 * \param[in] attributes Key attributes for the new key.
1484 * \param[out] p_drv On any return, the driver for the key, if any.
1485 * NULL for a transparent key.
1486 *
1487 */
1488static psa_status_t psa_validate_key_attributes(
1489 const psa_key_attributes_t *attributes,
1490 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001491{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001492 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001493 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001494 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001495
Ronald Cron54b90082020-10-29 15:26:43 +01001496 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001497 if( status != PSA_SUCCESS )
1498 return( status );
1499
Ronald Crond2ed4812020-07-17 16:11:30 +02001500 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001501 if( status != PSA_SUCCESS )
1502 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001503
Ronald Cron65f38a32020-10-23 17:11:13 +02001504 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1505 {
1506 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1507 return( PSA_ERROR_INVALID_ARGUMENT );
1508 }
1509 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001510 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001511 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001512 if( status != PSA_SUCCESS )
1513 return( status );
1514 }
1515
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001516 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001517 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001518 return( status );
1519
1520 /* Refuse to create overly large keys.
1521 * Note that this doesn't trigger on import if the attributes don't
1522 * explicitly specify a size (so psa_get_key_bits returns 0), so
1523 * psa_import_key() needs its own checks. */
1524 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1525 return( PSA_ERROR_NOT_SUPPORTED );
1526
Gilles Peskine91e8c332019-08-02 19:19:39 +02001527 /* Reject invalid flags. These should not be reachable through the API. */
1528 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1529 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1530 return( PSA_ERROR_INVALID_ARGUMENT );
1531
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001532 return( PSA_SUCCESS );
1533}
1534
Gilles Peskine4747d192019-04-17 15:05:45 +02001535/** Prepare a key slot to receive key material.
1536 *
1537 * This function allocates a key slot and sets its metadata.
1538 *
1539 * If this function fails, call psa_fail_key_creation().
1540 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001541 * This function is intended to be used as follows:
1542 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001543 * it with the specified attributes, and in case of a volatile key assign it
1544 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001545 * -# Populate the slot with the key material.
1546 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1547 * In case of failure at any step, stop the sequence and call
1548 * psa_fail_key_creation().
1549 *
Ronald Cron5c522922020-11-14 16:35:34 +01001550 * On success, the key slot is locked. It is the responsibility of the caller
1551 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001552 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001553 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001554 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001555 * \param[out] p_slot On success, a pointer to the prepared slot.
1556 * \param[out] p_drv On any return, the driver for the key, if any.
1557 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001558 *
1559 * \retval #PSA_SUCCESS
1560 * The key slot is ready to receive key material.
1561 * \return If this function fails, the key slot is an invalid state.
1562 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001563 */
1564static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001565 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001566 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001567 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001568 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001569{
1570 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001571 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001572 psa_key_slot_t *slot;
1573
Gilles Peskinedf179142019-07-15 22:02:14 +02001574 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001575 *p_drv = NULL;
1576
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001577 status = psa_validate_key_attributes( attributes, p_drv );
1578 if( status != PSA_SUCCESS )
1579 return( status );
1580
Ronald Cronc4d1b512020-07-31 11:26:37 +02001581 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001582 if( status != PSA_SUCCESS )
1583 return( status );
1584 slot = *p_slot;
1585
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001586 /* We're storing the declared bit-size of the key. It's up to each
1587 * creation mechanism to verify that this information is correct.
1588 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001589 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001590 * is optional (import, copy). In case of a volatile key, assign it the
1591 * volatile key identifier associated to the slot returned to contain its
1592 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001593
1594 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001595 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1596 {
1597#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1598 slot->attr.id = volatile_key_id;
1599#else
1600 slot->attr.id.key_id = volatile_key_id;
1601#endif
1602 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001603
Gilles Peskine91e8c332019-08-02 19:19:39 +02001604 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001605 * external-only flags, query `attributes`. Thanks to the check
1606 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001607 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001608 * may have set. */
1609 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001610
Gilles Peskinecbaff462019-07-12 23:46:04 +02001611#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001612 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001613 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001614 * create the key file in internal storage, create the
1615 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001616 * persistent data. This is done by starting a transaction that will
1617 * encompass these three actions.
1618 * For registering a volatile key, we just need to find an appropriate
1619 * slot number inside the SE. Since the key is designated volatile, creating
1620 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001621 /* The first thing to do is to find a slot number for the new key.
1622 * We save the slot number in persistent storage as part of the
1623 * transaction data. It will be needed to recover if the power
1624 * fails during the key creation process, to clean up on the secure
1625 * element side after restarting. Obtaining a slot number from the
1626 * secure element driver updates its persistent state, but we do not yet
1627 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001628 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001629 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001630 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001631 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001632 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001633 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001634 if( status != PSA_SUCCESS )
1635 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001636
1637 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001638 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001639 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1640 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001641 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001642 psa_crypto_transaction.key.id = slot->attr.id;
1643 status = psa_crypto_save_transaction( );
1644 if( status != PSA_SUCCESS )
1645 {
1646 (void) psa_crypto_stop_transaction( );
1647 return( status );
1648 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001649 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001650
1651 status = psa_copy_key_material_into_slot(
1652 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001653 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001654
1655 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1656 {
1657 /* Key registration only makes sense with a secure element. */
1658 return( PSA_ERROR_INVALID_ARGUMENT );
1659 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001660#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1661
Ronald Cronc4d1b512020-07-31 11:26:37 +02001662 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001663}
Gilles Peskine4747d192019-04-17 15:05:45 +02001664
1665/** Finalize the creation of a key once its key material has been set.
1666 *
1667 * This entails writing the key to persistent storage.
1668 *
1669 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001670 * See the documentation of psa_start_key_creation() for the intended use
1671 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001672 *
Ronald Cron5c522922020-11-14 16:35:34 +01001673 * If the finalization succeeds, the function unlocks the key slot (it was
1674 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1675 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001676 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001677 * \param[in,out] slot Pointer to the slot with key material.
1678 * \param[in] driver The secure element driver for the key,
1679 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001680 * \param[out] key On success, identifier of the key. Note that the
1681 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001682 *
1683 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001684 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001685 * \return If this function fails, the key slot is an invalid state.
1686 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001687 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001688static psa_status_t psa_finish_key_creation(
1689 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001690 psa_se_drv_table_entry_t *driver,
1691 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001692{
1693 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001694 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001695 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001696
1697#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001698 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001699 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001700#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1701 if( driver != NULL )
1702 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001703 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001704 psa_key_slot_number_t slot_number =
1705 psa_key_slot_get_slot_number( slot ) ;
1706
Gilles Peskineb46bef22019-07-30 21:32:04 +02001707#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001708 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001709 sizeof( data.slot_number ),
1710 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001711#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001712 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001713 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001714 (uint8_t*) &data,
1715 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001716 }
1717 else
1718#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1719 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001720 /* Key material is saved in export representation in the slot, so
1721 * just pass the slot buffer for storage. */
1722 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001723 slot->key.data,
1724 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001725 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001727#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1728
Gilles Peskinecbaff462019-07-12 23:46:04 +02001729#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001730 /* Finish the transaction for a key creation. This does not
1731 * happen when registering an existing key. Detect this case
1732 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001733 * creation of a persistent key in a secure element requires a transaction,
1734 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001735 if( driver != NULL &&
1736 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001737 {
1738 status = psa_save_se_persistent_data( driver );
1739 if( status != PSA_SUCCESS )
1740 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001741 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001742 return( status );
1743 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001744 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001745 }
1746#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1747
Ronald Cron50972942020-11-14 11:28:25 +01001748 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001749 {
1750 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001751 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001752 if( status != PSA_SUCCESS )
1753 *key = MBEDTLS_SVC_KEY_ID_INIT;
1754 }
Ronald Cron50972942020-11-14 11:28:25 +01001755
Gilles Peskine4747d192019-04-17 15:05:45 +02001756 return( status );
1757}
1758
1759/** Abort the creation of a key.
1760 *
1761 * You may call this function after calling psa_start_key_creation(),
1762 * or after psa_finish_key_creation() fails. In other circumstances, this
1763 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001764 * See the documentation of psa_start_key_creation() for the intended use
1765 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001766 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001767 * \param[in,out] slot Pointer to the slot with key material.
1768 * \param[in] driver The secure element driver for the key,
1769 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001771static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001772 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001773{
Gilles Peskine011e4282019-06-26 18:34:38 +02001774 (void) driver;
1775
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 if( slot == NULL )
1777 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001778
1779#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001780 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001781 * element, and the failure happened later (when saving metadata
1782 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001783 * element.
1784 * https://github.com/ARMmbed/mbed-crypto/issues/217
1785 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001786
Gilles Peskined7729582019-08-05 15:55:54 +02001787 /* Abort the ongoing transaction if any (there may not be one if
1788 * the creation process failed before starting one, or if the
1789 * key creation is a registration of a key in a secure element).
1790 * Earlier functions must already have done what it takes to undo any
1791 * partial creation. All that's left is to update the transaction data
1792 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001793 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001794#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1795
Gilles Peskine4747d192019-04-17 15:05:45 +02001796 psa_wipe_key_slot( slot );
1797}
1798
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001799/** Validate optional attributes during key creation.
1800 *
1801 * Some key attributes are optional during key creation. If they are
1802 * specified in the attributes structure, check that they are consistent
1803 * with the data in the slot.
1804 *
1805 * This function should be called near the end of key creation, after
1806 * the slot in memory is fully populated but before saving persistent data.
1807 */
1808static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001809 const psa_key_slot_t *slot,
1810 const psa_key_attributes_t *attributes )
1811{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001812 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001813 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001814 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001815 return( PSA_ERROR_INVALID_ARGUMENT );
1816 }
1817
1818 if( attributes->domain_parameters_size != 0 )
1819 {
John Durkop0e005192020-10-31 22:06:54 -07001820#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1821 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001822 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001823 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001824 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001825 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001827
Ronald Cron90857082020-11-25 14:58:33 +01001828 psa_status_t status = mbedtls_psa_rsa_load_representation(
1829 slot->attr.type,
1830 slot->key.data,
1831 slot->key.bytes,
1832 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001833 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001834 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001835
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001836 mbedtls_mpi_init( &actual );
1837 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001838 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001839 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001840 mbedtls_rsa_free( rsa );
1841 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001842 if( ret != 0 )
1843 goto rsa_exit;
1844 ret = mbedtls_mpi_read_binary( &required,
1845 attributes->domain_parameters,
1846 attributes->domain_parameters_size );
1847 if( ret != 0 )
1848 goto rsa_exit;
1849 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1850 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1851 rsa_exit:
1852 mbedtls_mpi_free( &actual );
1853 mbedtls_mpi_free( &required );
1854 if( ret != 0)
1855 return( mbedtls_to_psa_error( ret ) );
1856 }
1857 else
John Durkop9814fa22020-11-04 12:28:15 -08001858#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1859 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001860 {
1861 return( PSA_ERROR_INVALID_ARGUMENT );
1862 }
1863 }
1864
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001865 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001866 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001867 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001868 return( PSA_ERROR_INVALID_ARGUMENT );
1869 }
1870
1871 return( PSA_SUCCESS );
1872}
1873
Gilles Peskine4747d192019-04-17 15:05:45 +02001874psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001875 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001876 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001877 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001878{
1879 psa_status_t status;
1880 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001881 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001882
Ronald Cron81709fc2020-11-14 12:10:32 +01001883 *key = MBEDTLS_SVC_KEY_ID_INIT;
1884
Gilles Peskine0f84d622019-09-12 19:03:13 +02001885 /* Reject zero-length symmetric keys (including raw data key objects).
1886 * This also rejects any key which might be encoded as an empty string,
1887 * which is never valid. */
1888 if( data_length == 0 )
1889 return( PSA_ERROR_INVALID_ARGUMENT );
1890
Gilles Peskinedf179142019-07-15 22:02:14 +02001891 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001892 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001893 if( status != PSA_SUCCESS )
1894 goto exit;
1895
Gilles Peskine5d309672019-07-12 23:47:28 +02001896#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1897 if( driver != NULL )
1898 {
1899 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001900 /* The driver should set the number of key bits, however in
1901 * case it doesn't, we initialize bits to an invalid value. */
1902 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001903 if( drv->key_management == NULL ||
1904 drv->key_management->p_import == NULL )
1905 {
1906 status = PSA_ERROR_NOT_SUPPORTED;
1907 goto exit;
1908 }
1909 status = drv->key_management->p_import(
1910 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01001911 psa_key_slot_get_slot_number( slot ),
1912 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001913 if( status != PSA_SUCCESS )
1914 goto exit;
1915 if( bits > PSA_MAX_KEY_BITS )
1916 {
1917 status = PSA_ERROR_NOT_SUPPORTED;
1918 goto exit;
1919 }
1920 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001921 }
1922 else
1923#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01001924 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
1925 {
1926 /* Importing a key with external lifetime through the driver wrapper
1927 * interface is not yet supported. Return as if this was an invalid
1928 * lifetime. */
1929 status = PSA_ERROR_INVALID_ARGUMENT;
1930 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001931 }
1932 else
Gilles Peskine5d309672019-07-12 23:47:28 +02001933 {
1934 status = psa_import_key_into_slot( slot, data, data_length );
1935 if( status != PSA_SUCCESS )
1936 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001937 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001938 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001939 if( status != PSA_SUCCESS )
1940 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001941
Ronald Cron81709fc2020-11-14 12:10:32 +01001942 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001943exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001944 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001945 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001946
Gilles Peskine4747d192019-04-17 15:05:45 +02001947 return( status );
1948}
1949
Gilles Peskined7729582019-08-05 15:55:54 +02001950#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1951psa_status_t mbedtls_psa_register_se_key(
1952 const psa_key_attributes_t *attributes )
1953{
1954 psa_status_t status;
1955 psa_key_slot_t *slot = NULL;
1956 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001957 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001958
1959 /* Leaving attributes unspecified is not currently supported.
1960 * It could make sense to query the key type and size from the
1961 * secure element, but not all secure elements support this
1962 * and the driver HAL doesn't currently support it. */
1963 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1964 return( PSA_ERROR_NOT_SUPPORTED );
1965 if( psa_get_key_bits( attributes ) == 0 )
1966 return( PSA_ERROR_NOT_SUPPORTED );
1967
1968 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001969 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001970 if( status != PSA_SUCCESS )
1971 goto exit;
1972
Ronald Cron81709fc2020-11-14 12:10:32 +01001973 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001974
1975exit:
1976 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001977 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001978
Gilles Peskined7729582019-08-05 15:55:54 +02001979 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001980 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02001981 return( status );
1982}
1983#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1984
Gilles Peskinef603c712019-01-19 13:40:11 +01001985static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001986 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001987{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001988 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001989 source->key.data,
1990 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01001991 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02001992 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01001993
Steven Cooreman398aee52020-10-13 14:35:45 +02001994 target->attr.type = source->attr.type;
1995 target->attr.bits = source->attr.bits;
1996
1997 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01001998}
1999
Ronald Croncf56a0a2020-08-04 09:51:30 +02002000psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002001 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002002 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002003{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002004 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002005 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002006 psa_key_slot_t *source_slot = NULL;
2007 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002008 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002009 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002010
Ronald Cron81709fc2020-11-14 12:10:32 +01002011 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2012
Ronald Cron5c522922020-11-14 16:35:34 +01002013 status = psa_get_and_lock_transparent_key_slot_with_policy(
2014 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002015 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002016 goto exit;
2017
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002018 status = psa_validate_optional_attributes( source_slot,
2019 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020 if( status != PSA_SUCCESS )
2021 goto exit;
2022
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002023 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002024 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002025 if( status != PSA_SUCCESS )
2026 goto exit;
2027
Ronald Cron81709fc2020-11-14 12:10:32 +01002028 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2029 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002030 if( status != PSA_SUCCESS )
2031 goto exit;
2032
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002033#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2034 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002035 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002036 /* Copying to a secure element is not implemented yet. */
2037 status = PSA_ERROR_NOT_SUPPORTED;
2038 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002039 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002040#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002041
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002042 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002043 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002044 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002045
Ronald Cron81709fc2020-11-14 12:10:32 +01002046 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002047exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002048 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002049 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002050
Ronald Cron5c522922020-11-14 16:35:34 +01002051 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002052
Ronald Cron5c522922020-11-14 16:35:34 +01002053 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002054}
2055
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002056
2057
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002058/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002059/* Message digests */
2060/****************************************************************/
2061
John Durkop07cc04a2020-11-16 22:08:34 -08002062#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002063 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2064 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002065 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002066static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002067{
2068 switch( alg )
2069 {
John Durkopee4e6602020-11-27 08:48:46 -08002070#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002071 case PSA_ALG_MD2:
2072 return( &mbedtls_md2_info );
2073#endif
John Durkopee4e6602020-11-27 08:48:46 -08002074#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002075 case PSA_ALG_MD4:
2076 return( &mbedtls_md4_info );
2077#endif
John Durkopee4e6602020-11-27 08:48:46 -08002078#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002079 case PSA_ALG_MD5:
2080 return( &mbedtls_md5_info );
2081#endif
John Durkopee4e6602020-11-27 08:48:46 -08002082#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002083 case PSA_ALG_RIPEMD160:
2084 return( &mbedtls_ripemd160_info );
2085#endif
John Durkopee4e6602020-11-27 08:48:46 -08002086#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002087 case PSA_ALG_SHA_1:
2088 return( &mbedtls_sha1_info );
2089#endif
John Durkopee4e6602020-11-27 08:48:46 -08002090#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002091 case PSA_ALG_SHA_224:
2092 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002093#endif
2094#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002095 case PSA_ALG_SHA_256:
2096 return( &mbedtls_sha256_info );
2097#endif
John Durkopee4e6602020-11-27 08:48:46 -08002098#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002099 case PSA_ALG_SHA_384:
2100 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002101#endif
John Durkopee4e6602020-11-27 08:48:46 -08002102#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002103 case PSA_ALG_SHA_512:
2104 return( &mbedtls_sha512_info );
2105#endif
2106 default:
2107 return( NULL );
2108 }
2109}
John Durkop07cc04a2020-11-16 22:08:34 -08002110#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002111 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2112 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2113 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002114
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002115psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2116{
2117 switch( operation->alg )
2118 {
Gilles Peskine81736312018-06-26 15:04:31 +02002119 case 0:
2120 /* The object has (apparently) been initialized but it is not
2121 * in use. It's ok to call abort on such an object, and there's
2122 * nothing to do. */
2123 break;
John Durkopee4e6602020-11-27 08:48:46 -08002124#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002125 case PSA_ALG_MD2:
2126 mbedtls_md2_free( &operation->ctx.md2 );
2127 break;
2128#endif
John Durkopee4e6602020-11-27 08:48:46 -08002129#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002130 case PSA_ALG_MD4:
2131 mbedtls_md4_free( &operation->ctx.md4 );
2132 break;
2133#endif
John Durkopee4e6602020-11-27 08:48:46 -08002134#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002135 case PSA_ALG_MD5:
2136 mbedtls_md5_free( &operation->ctx.md5 );
2137 break;
2138#endif
John Durkopee4e6602020-11-27 08:48:46 -08002139#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002140 case PSA_ALG_RIPEMD160:
2141 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2142 break;
2143#endif
John Durkopee4e6602020-11-27 08:48:46 -08002144#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002145 case PSA_ALG_SHA_1:
2146 mbedtls_sha1_free( &operation->ctx.sha1 );
2147 break;
2148#endif
John Durkop6ca23272020-12-03 06:01:32 -08002149#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002150 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002151 mbedtls_sha256_free( &operation->ctx.sha256 );
2152 break;
2153#endif
2154#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002155 case PSA_ALG_SHA_256:
2156 mbedtls_sha256_free( &operation->ctx.sha256 );
2157 break;
2158#endif
John Durkop6ca23272020-12-03 06:01:32 -08002159#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002160 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002161 mbedtls_sha512_free( &operation->ctx.sha512 );
2162 break;
2163#endif
2164#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002165 case PSA_ALG_SHA_512:
2166 mbedtls_sha512_free( &operation->ctx.sha512 );
2167 break;
2168#endif
2169 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002170 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002171 }
2172 operation->alg = 0;
2173 return( PSA_SUCCESS );
2174}
2175
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002176psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002177 psa_algorithm_t alg )
2178{
Janos Follath24eed8d2019-11-22 13:21:35 +00002179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002180
2181 /* A context must be freshly initialized before it can be set up. */
2182 if( operation->alg != 0 )
2183 {
2184 return( PSA_ERROR_BAD_STATE );
2185 }
2186
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002187 switch( alg )
2188 {
John Durkopee4e6602020-11-27 08:48:46 -08002189#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190 case PSA_ALG_MD2:
2191 mbedtls_md2_init( &operation->ctx.md2 );
2192 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2193 break;
2194#endif
John Durkopee4e6602020-11-27 08:48:46 -08002195#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196 case PSA_ALG_MD4:
2197 mbedtls_md4_init( &operation->ctx.md4 );
2198 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2199 break;
2200#endif
John Durkopee4e6602020-11-27 08:48:46 -08002201#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202 case PSA_ALG_MD5:
2203 mbedtls_md5_init( &operation->ctx.md5 );
2204 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2205 break;
2206#endif
John Durkopee4e6602020-11-27 08:48:46 -08002207#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002208 case PSA_ALG_RIPEMD160:
2209 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2210 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2211 break;
2212#endif
John Durkopee4e6602020-11-27 08:48:46 -08002213#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002214 case PSA_ALG_SHA_1:
2215 mbedtls_sha1_init( &operation->ctx.sha1 );
2216 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2217 break;
2218#endif
John Durkopee4e6602020-11-27 08:48:46 -08002219#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002220 case PSA_ALG_SHA_224:
2221 mbedtls_sha256_init( &operation->ctx.sha256 );
2222 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2223 break;
John Durkopee4e6602020-11-27 08:48:46 -08002224#endif
2225#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002226 case PSA_ALG_SHA_256:
2227 mbedtls_sha256_init( &operation->ctx.sha256 );
2228 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2229 break;
2230#endif
John Durkopee4e6602020-11-27 08:48:46 -08002231#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232 case PSA_ALG_SHA_384:
2233 mbedtls_sha512_init( &operation->ctx.sha512 );
2234 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2235 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002236#endif
John Durkopee4e6602020-11-27 08:48:46 -08002237#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238 case PSA_ALG_SHA_512:
2239 mbedtls_sha512_init( &operation->ctx.sha512 );
2240 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2241 break;
2242#endif
2243 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002244 return( PSA_ALG_IS_HASH( alg ) ?
2245 PSA_ERROR_NOT_SUPPORTED :
2246 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247 }
2248 if( ret == 0 )
2249 operation->alg = alg;
2250 else
2251 psa_hash_abort( operation );
2252 return( mbedtls_to_psa_error( ret ) );
2253}
2254
2255psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2256 const uint8_t *input,
2257 size_t input_length )
2258{
Janos Follath24eed8d2019-11-22 13:21:35 +00002259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002260
2261 /* Don't require hash implementations to behave correctly on a
2262 * zero-length input, which may have an invalid pointer. */
2263 if( input_length == 0 )
2264 return( PSA_SUCCESS );
2265
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266 switch( operation->alg )
2267 {
John Durkopee4e6602020-11-27 08:48:46 -08002268#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269 case PSA_ALG_MD2:
2270 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2271 input, input_length );
2272 break;
2273#endif
John Durkopee4e6602020-11-27 08:48:46 -08002274#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275 case PSA_ALG_MD4:
2276 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2277 input, input_length );
2278 break;
2279#endif
John Durkopee4e6602020-11-27 08:48:46 -08002280#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281 case PSA_ALG_MD5:
2282 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2283 input, input_length );
2284 break;
2285#endif
John Durkopee4e6602020-11-27 08:48:46 -08002286#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002287 case PSA_ALG_RIPEMD160:
2288 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2289 input, input_length );
2290 break;
2291#endif
John Durkopee4e6602020-11-27 08:48:46 -08002292#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293 case PSA_ALG_SHA_1:
2294 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2295 input, input_length );
2296 break;
2297#endif
John Durkop6ca23272020-12-03 06:01:32 -08002298#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002300 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2301 input, input_length );
2302 break;
2303#endif
2304#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002305 case PSA_ALG_SHA_256:
2306 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2307 input, input_length );
2308 break;
2309#endif
John Durkop6ca23272020-12-03 06:01:32 -08002310#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002312 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2313 input, input_length );
2314 break;
2315#endif
2316#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317 case PSA_ALG_SHA_512:
2318 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2319 input, input_length );
2320 break;
2321#endif
2322 default:
John Durkopee4e6602020-11-27 08:48:46 -08002323 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002324 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002326
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327 if( ret != 0 )
2328 psa_hash_abort( operation );
2329 return( mbedtls_to_psa_error( ret ) );
2330}
2331
2332psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2333 uint8_t *hash,
2334 size_t hash_size,
2335 size_t *hash_length )
2336{
itayzafrir40835d42018-08-02 13:14:17 +03002337 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002339 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340
2341 /* Fill the output buffer with something that isn't a valid hash
2342 * (barring an attack on the hash and deliberately-crafted input),
2343 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002344 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002345 /* If hash_size is 0 then hash may be NULL and then the
2346 * call to memset would have undefined behavior. */
2347 if( hash_size != 0 )
2348 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349
2350 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002351 {
2352 status = PSA_ERROR_BUFFER_TOO_SMALL;
2353 goto exit;
2354 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355
2356 switch( operation->alg )
2357 {
John Durkopee4e6602020-11-27 08:48:46 -08002358#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002359 case PSA_ALG_MD2:
2360 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2361 break;
2362#endif
John Durkopee4e6602020-11-27 08:48:46 -08002363#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364 case PSA_ALG_MD4:
2365 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2366 break;
2367#endif
John Durkopee4e6602020-11-27 08:48:46 -08002368#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002369 case PSA_ALG_MD5:
2370 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2371 break;
2372#endif
John Durkopee4e6602020-11-27 08:48:46 -08002373#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002374 case PSA_ALG_RIPEMD160:
2375 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2376 break;
2377#endif
John Durkopee4e6602020-11-27 08:48:46 -08002378#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002379 case PSA_ALG_SHA_1:
2380 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2381 break;
2382#endif
John Durkop6ca23272020-12-03 06:01:32 -08002383#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002384 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002385 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2386 break;
2387#endif
2388#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389 case PSA_ALG_SHA_256:
2390 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2391 break;
2392#endif
John Durkop6ca23272020-12-03 06:01:32 -08002393#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002395 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2396 break;
2397#endif
2398#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002399 case PSA_ALG_SHA_512:
2400 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2401 break;
2402#endif
2403 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002404 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002405 }
itayzafrir40835d42018-08-02 13:14:17 +03002406 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002407
itayzafrir40835d42018-08-02 13:14:17 +03002408exit:
2409 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002411 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002412 return( psa_hash_abort( operation ) );
2413 }
2414 else
2415 {
2416 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002417 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002418 }
2419}
2420
Gilles Peskine2d277862018-06-18 15:41:12 +02002421psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2422 const uint8_t *hash,
2423 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002424{
2425 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2426 size_t actual_hash_length;
2427 psa_status_t status = psa_hash_finish( operation,
2428 actual_hash, sizeof( actual_hash ),
2429 &actual_hash_length );
2430 if( status != PSA_SUCCESS )
2431 return( status );
2432 if( actual_hash_length != hash_length )
2433 return( PSA_ERROR_INVALID_SIGNATURE );
2434 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2435 return( PSA_ERROR_INVALID_SIGNATURE );
2436 return( PSA_SUCCESS );
2437}
2438
Gilles Peskine0a749c82019-11-28 19:33:58 +01002439psa_status_t psa_hash_compute( psa_algorithm_t alg,
2440 const uint8_t *input, size_t input_length,
2441 uint8_t *hash, size_t hash_size,
2442 size_t *hash_length )
2443{
2444 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2446
2447 *hash_length = hash_size;
2448 status = psa_hash_setup( &operation, alg );
2449 if( status != PSA_SUCCESS )
2450 goto exit;
2451 status = psa_hash_update( &operation, input, input_length );
2452 if( status != PSA_SUCCESS )
2453 goto exit;
2454 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2455 if( status != PSA_SUCCESS )
2456 goto exit;
2457
2458exit:
2459 if( status == PSA_SUCCESS )
2460 status = psa_hash_abort( &operation );
2461 else
2462 psa_hash_abort( &operation );
2463 return( status );
2464}
2465
2466psa_status_t psa_hash_compare( psa_algorithm_t alg,
2467 const uint8_t *input, size_t input_length,
2468 const uint8_t *hash, size_t hash_length )
2469{
2470 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2471 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2472
2473 status = psa_hash_setup( &operation, alg );
2474 if( status != PSA_SUCCESS )
2475 goto exit;
2476 status = psa_hash_update( &operation, input, input_length );
2477 if( status != PSA_SUCCESS )
2478 goto exit;
2479 status = psa_hash_verify( &operation, hash, hash_length );
2480 if( status != PSA_SUCCESS )
2481 goto exit;
2482
2483exit:
2484 if( status == PSA_SUCCESS )
2485 status = psa_hash_abort( &operation );
2486 else
2487 psa_hash_abort( &operation );
2488 return( status );
2489}
2490
Gilles Peskineeb35d782019-01-22 17:56:16 +01002491psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2492 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002493{
2494 if( target_operation->alg != 0 )
2495 return( PSA_ERROR_BAD_STATE );
2496
2497 switch( source_operation->alg )
2498 {
2499 case 0:
2500 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002501#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002502 case PSA_ALG_MD2:
2503 mbedtls_md2_clone( &target_operation->ctx.md2,
2504 &source_operation->ctx.md2 );
2505 break;
2506#endif
John Durkopee4e6602020-11-27 08:48:46 -08002507#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002508 case PSA_ALG_MD4:
2509 mbedtls_md4_clone( &target_operation->ctx.md4,
2510 &source_operation->ctx.md4 );
2511 break;
2512#endif
John Durkopee4e6602020-11-27 08:48:46 -08002513#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002514 case PSA_ALG_MD5:
2515 mbedtls_md5_clone( &target_operation->ctx.md5,
2516 &source_operation->ctx.md5 );
2517 break;
2518#endif
John Durkopee4e6602020-11-27 08:48:46 -08002519#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002520 case PSA_ALG_RIPEMD160:
2521 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2522 &source_operation->ctx.ripemd160 );
2523 break;
2524#endif
John Durkopee4e6602020-11-27 08:48:46 -08002525#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002526 case PSA_ALG_SHA_1:
2527 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2528 &source_operation->ctx.sha1 );
2529 break;
2530#endif
John Durkop6ca23272020-12-03 06:01:32 -08002531#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002532 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002533 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2534 &source_operation->ctx.sha256 );
2535 break;
2536#endif
2537#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002538 case PSA_ALG_SHA_256:
2539 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2540 &source_operation->ctx.sha256 );
2541 break;
2542#endif
John Durkop6ca23272020-12-03 06:01:32 -08002543#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002544 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002545 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2546 &source_operation->ctx.sha512 );
2547 break;
2548#endif
2549#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002550 case PSA_ALG_SHA_512:
2551 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2552 &source_operation->ctx.sha512 );
2553 break;
2554#endif
2555 default:
2556 return( PSA_ERROR_NOT_SUPPORTED );
2557 }
2558
2559 target_operation->alg = source_operation->alg;
2560 return( PSA_SUCCESS );
2561}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002562
2563
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002564/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565/* MAC */
2566/****************************************************************/
2567
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002568static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 psa_algorithm_t alg,
2570 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002571 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002572 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002573{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002574 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002575 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002577 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002578 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002579
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2581 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002582 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002584 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585 mode = MBEDTLS_MODE_STREAM;
2586 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002587 case PSA_ALG_CTR:
2588 mode = MBEDTLS_MODE_CTR;
2589 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002590 case PSA_ALG_CFB:
2591 mode = MBEDTLS_MODE_CFB;
2592 break;
2593 case PSA_ALG_OFB:
2594 mode = MBEDTLS_MODE_OFB;
2595 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002596 case PSA_ALG_ECB_NO_PADDING:
2597 mode = MBEDTLS_MODE_ECB;
2598 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002599 case PSA_ALG_CBC_NO_PADDING:
2600 mode = MBEDTLS_MODE_CBC;
2601 break;
2602 case PSA_ALG_CBC_PKCS7:
2603 mode = MBEDTLS_MODE_CBC;
2604 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002605 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606 mode = MBEDTLS_MODE_CCM;
2607 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002608 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609 mode = MBEDTLS_MODE_GCM;
2610 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002611 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2612 mode = MBEDTLS_MODE_CHACHAPOLY;
2613 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614 default:
2615 return( NULL );
2616 }
2617 }
2618 else if( alg == PSA_ALG_CMAC )
2619 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620 else
2621 return( NULL );
2622
2623 switch( key_type )
2624 {
2625 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002626 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002627 break;
2628 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002629 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2630 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002632 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002634 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002635 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2636 * but two-key Triple-DES is functionally three-key Triple-DES
2637 * with K1=K3, so that's how we present it to mbedtls. */
2638 if( key_bits == 128 )
2639 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640 break;
2641 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002642 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643 break;
2644 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002645 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002646 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002647 case PSA_KEY_TYPE_CHACHA20:
2648 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2649 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650 default:
2651 return( NULL );
2652 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002653 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002654 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002655
Jaeden Amero23bbb752018-06-26 14:16:54 +01002656 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2657 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658}
2659
John Durkop6ba40d12020-11-10 08:50:04 -08002660#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002661static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002662{
Gilles Peskine2d277862018-06-18 15:41:12 +02002663 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002664 {
2665 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002666 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002667 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002668 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002669 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002670 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002671 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002672 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002673 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002674 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002675 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002676 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002677 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002678 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002679 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002680 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002681 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002682 return( 128 );
2683 default:
2684 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002685 }
2686}
John Durkop07cc04a2020-11-16 22:08:34 -08002687#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002688
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002689/* Initialize the MAC operation structure. Once this function has been
2690 * called, psa_mac_abort can run and will do the right thing. */
2691static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2692 psa_algorithm_t alg )
2693{
2694 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2695
2696 operation->alg = alg;
2697 operation->key_set = 0;
2698 operation->iv_set = 0;
2699 operation->iv_required = 0;
2700 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002701 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002702
2703#if defined(MBEDTLS_CMAC_C)
2704 if( alg == PSA_ALG_CMAC )
2705 {
2706 operation->iv_required = 0;
2707 mbedtls_cipher_init( &operation->ctx.cmac );
2708 status = PSA_SUCCESS;
2709 }
2710 else
2711#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002712#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002713 if( PSA_ALG_IS_HMAC( operation->alg ) )
2714 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002715 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2716 operation->ctx.hmac.hash_ctx.alg = 0;
2717 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002718 }
2719 else
John Durkopd0321952020-10-29 21:37:36 -07002720#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002721 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002722 if( ! PSA_ALG_IS_MAC( alg ) )
2723 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002724 }
2725
2726 if( status != PSA_SUCCESS )
2727 memset( operation, 0, sizeof( *operation ) );
2728 return( status );
2729}
2730
John Durkop6ba40d12020-11-10 08:50:04 -08002731#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002732static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2733{
Gilles Peskine3f108122018-12-07 18:14:53 +01002734 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002735 return( psa_hash_abort( &hmac->hash_ctx ) );
2736}
John Durkop6ba40d12020-11-10 08:50:04 -08002737#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002738
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2740{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002741 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002743 /* The object has (apparently) been initialized but it is not
2744 * in use. It's ok to call abort on such an object, and there's
2745 * nothing to do. */
2746 return( PSA_SUCCESS );
2747 }
2748 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002749#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002750 if( operation->alg == PSA_ALG_CMAC )
2751 {
2752 mbedtls_cipher_free( &operation->ctx.cmac );
2753 }
2754 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002755#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002756#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002757 if( PSA_ALG_IS_HMAC( operation->alg ) )
2758 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002759 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002760 }
2761 else
John Durkopd0321952020-10-29 21:37:36 -07002762#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002763 {
2764 /* Sanity check (shouldn't happen: operation->alg should
2765 * always have been initialized to a valid value). */
2766 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002767 }
Moran Peker41deec42018-04-04 15:43:05 +03002768
Gilles Peskine8c9def32018-02-08 10:02:12 +01002769 operation->alg = 0;
2770 operation->key_set = 0;
2771 operation->iv_set = 0;
2772 operation->iv_required = 0;
2773 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002774 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002775
Gilles Peskine8c9def32018-02-08 10:02:12 +01002776 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002777
2778bad_state:
2779 /* If abort is called on an uninitialized object, we can't trust
2780 * anything. Wipe the object in case it contains confidential data.
2781 * This may result in a memory leak if a pointer gets overwritten,
2782 * but it's too late to do anything about this. */
2783 memset( operation, 0, sizeof( *operation ) );
2784 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002785}
2786
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002787#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002788static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002789 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002790 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002791 const mbedtls_cipher_info_t *cipher_info )
2792{
Janos Follath24eed8d2019-11-22 13:21:35 +00002793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002794
2795 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002796
2797 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2798 if( ret != 0 )
2799 return( ret );
2800
2801 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002802 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002803 key_bits );
2804 return( ret );
2805}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002806#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002807
John Durkop6ba40d12020-11-10 08:50:04 -08002808#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002809static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2810 const uint8_t *key,
2811 size_t key_length,
2812 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002813{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002814 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002815 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002816 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002817 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002818 psa_status_t status;
2819
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002820 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2821 * overflow below. This should never trigger if the hash algorithm
2822 * is implemented correctly. */
2823 /* The size checks against the ipad and opad buffers cannot be written
2824 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2825 * because that triggers -Wlogical-op on GCC 7.3. */
2826 if( block_size > sizeof( ipad ) )
2827 return( PSA_ERROR_NOT_SUPPORTED );
2828 if( block_size > sizeof( hmac->opad ) )
2829 return( PSA_ERROR_NOT_SUPPORTED );
2830 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002831 return( PSA_ERROR_NOT_SUPPORTED );
2832
Gilles Peskined223b522018-06-11 18:12:58 +02002833 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002834 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002835 status = psa_hash_compute( hash_alg, key, key_length,
2836 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002837 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002838 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002839 }
Gilles Peskine96889972018-07-12 17:07:03 +02002840 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2841 * but it is permitted. It is common when HMAC is used in HKDF, for
2842 * example. Don't call `memcpy` in the 0-length because `key` could be
2843 * an invalid pointer which would make the behavior undefined. */
2844 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002845 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002846
Gilles Peskined223b522018-06-11 18:12:58 +02002847 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2848 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002849 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002850 ipad[i] ^= 0x36;
2851 memset( ipad + key_length, 0x36, block_size - key_length );
2852
2853 /* Copy the key material from ipad to opad, flipping the requisite bits,
2854 * and filling the rest of opad with the requisite constant. */
2855 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002856 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2857 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002858
Gilles Peskine01126fa2018-07-12 17:04:55 +02002859 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002860 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002861 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002862
Gilles Peskine01126fa2018-07-12 17:04:55 +02002863 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002864
2865cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002866 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002867
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002868 return( status );
2869}
John Durkop6ba40d12020-11-10 08:50:04 -08002870#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002871
Gilles Peskine89167cb2018-07-08 20:12:23 +02002872static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002873 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002874 psa_algorithm_t alg,
2875 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002877 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002878 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002879 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002880 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002881 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002882 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002883 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002884 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002885
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002886 /* A context must be freshly initialized before it can be set up. */
2887 if( operation->alg != 0 )
2888 {
2889 return( PSA_ERROR_BAD_STATE );
2890 }
2891
Gilles Peskined911eb72018-08-14 15:18:45 +02002892 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002893 if( status != PSA_SUCCESS )
2894 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002895 if( is_sign )
2896 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002897
Ronald Cron5c522922020-11-14 16:35:34 +01002898 status = psa_get_and_lock_transparent_key_slot_with_policy(
2899 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002900 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002901 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002902 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002903
Gilles Peskine8c9def32018-02-08 10:02:12 +01002904#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002905 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002906 {
2907 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002908 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002909 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002911 if( cipher_info == NULL )
2912 {
2913 status = PSA_ERROR_NOT_SUPPORTED;
2914 goto exit;
2915 }
2916 operation->mac_size = cipher_info->block_size;
2917 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2918 status = mbedtls_to_psa_error( ret );
2919 }
2920 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002922#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02002923 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002924 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002925 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002926 if( hash_alg == 0 )
2927 {
2928 status = PSA_ERROR_NOT_SUPPORTED;
2929 goto exit;
2930 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002931
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002932 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002933 /* Sanity check. This shouldn't fail on a valid configuration. */
2934 if( operation->mac_size == 0 ||
2935 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2936 {
2937 status = PSA_ERROR_NOT_SUPPORTED;
2938 goto exit;
2939 }
2940
Gilles Peskine8e338702019-07-30 20:06:31 +02002941 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002942 {
2943 status = PSA_ERROR_INVALID_ARGUMENT;
2944 goto exit;
2945 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002946
Gilles Peskine01126fa2018-07-12 17:04:55 +02002947 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002948 slot->key.data,
2949 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02002950 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002951 }
2952 else
John Durkopd0321952020-10-29 21:37:36 -07002953#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002954 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002955 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002956 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002958
Gilles Peskined911eb72018-08-14 15:18:45 +02002959 if( truncated == 0 )
2960 {
2961 /* The "normal" case: untruncated algorithm. Nothing to do. */
2962 }
2963 else if( truncated < 4 )
2964 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002965 /* A very short MAC is too short for security since it can be
2966 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2967 * so we make this our minimum, even though 32 bits is still
2968 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002969 status = PSA_ERROR_NOT_SUPPORTED;
2970 }
2971 else if( truncated > operation->mac_size )
2972 {
2973 /* It's impossible to "truncate" to a larger length. */
2974 status = PSA_ERROR_INVALID_ARGUMENT;
2975 }
2976 else
2977 operation->mac_size = truncated;
2978
Gilles Peskinefbfac682018-07-08 20:51:54 +02002979exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002980 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002981 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002982 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002983 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002984 else
2985 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002986 operation->key_set = 1;
2987 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002988
Ronald Cron5c522922020-11-14 16:35:34 +01002989 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002990
Ronald Cron5c522922020-11-14 16:35:34 +01002991 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002992}
2993
Gilles Peskine89167cb2018-07-08 20:12:23 +02002994psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002995 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002996 psa_algorithm_t alg )
2997{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002998 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002999}
3000
3001psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003002 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003003 psa_algorithm_t alg )
3004{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003005 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003006}
3007
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3009 const uint8_t *input,
3010 size_t input_length )
3011{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003012 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003014 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003016 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003017 operation->has_input = 1;
3018
Gilles Peskine8c9def32018-02-08 10:02:12 +01003019#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003020 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003021 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003022 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3023 input, input_length );
3024 status = mbedtls_to_psa_error( ret );
3025 }
3026 else
3027#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003028#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003029 if( PSA_ALG_IS_HMAC( operation->alg ) )
3030 {
3031 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3032 input_length );
3033 }
3034 else
John Durkopd0321952020-10-29 21:37:36 -07003035#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003036 {
3037 /* This shouldn't happen if `operation` was initialized by
3038 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003039 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003040 }
3041
Gilles Peskinefbfac682018-07-08 20:51:54 +02003042 if( status != PSA_SUCCESS )
3043 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003044 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003045}
3046
John Durkop6ba40d12020-11-10 08:50:04 -08003047#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003048static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3049 uint8_t *mac,
3050 size_t mac_size )
3051{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003052 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003053 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3054 size_t hash_size = 0;
3055 size_t block_size = psa_get_hash_block_size( hash_alg );
3056 psa_status_t status;
3057
Gilles Peskine01126fa2018-07-12 17:04:55 +02003058 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3059 if( status != PSA_SUCCESS )
3060 return( status );
3061 /* From here on, tmp needs to be wiped. */
3062
3063 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3064 if( status != PSA_SUCCESS )
3065 goto exit;
3066
3067 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3068 if( status != PSA_SUCCESS )
3069 goto exit;
3070
3071 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3072 if( status != PSA_SUCCESS )
3073 goto exit;
3074
Gilles Peskined911eb72018-08-14 15:18:45 +02003075 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3076 if( status != PSA_SUCCESS )
3077 goto exit;
3078
3079 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003080
3081exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003082 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003083 return( status );
3084}
John Durkop6ba40d12020-11-10 08:50:04 -08003085#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003086
mohammad16036df908f2018-04-02 08:34:15 -07003087static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003088 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003089 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003090{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003091 if( ! operation->key_set )
3092 return( PSA_ERROR_BAD_STATE );
3093 if( operation->iv_required && ! operation->iv_set )
3094 return( PSA_ERROR_BAD_STATE );
3095
Gilles Peskine8c9def32018-02-08 10:02:12 +01003096 if( mac_size < operation->mac_size )
3097 return( PSA_ERROR_BUFFER_TOO_SMALL );
3098
Gilles Peskine8c9def32018-02-08 10:02:12 +01003099#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003100 if( operation->alg == PSA_ALG_CMAC )
3101 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003102 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003103 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3104 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003105 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003106 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003107 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003108 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003109 else
3110#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003111#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003112 if( PSA_ALG_IS_HMAC( operation->alg ) )
3113 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003114 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003115 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003116 }
3117 else
John Durkopd0321952020-10-29 21:37:36 -07003118#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003119 {
3120 /* This shouldn't happen if `operation` was initialized by
3121 * a setup function. */
3122 return( PSA_ERROR_BAD_STATE );
3123 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003124}
3125
Gilles Peskineacd4be32018-07-08 19:56:25 +02003126psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3127 uint8_t *mac,
3128 size_t mac_size,
3129 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003130{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003131 psa_status_t status;
3132
Jaeden Amero252ef282019-02-15 14:05:35 +00003133 if( operation->alg == 0 )
3134 {
3135 return( PSA_ERROR_BAD_STATE );
3136 }
3137
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003138 /* Fill the output buffer with something that isn't a valid mac
3139 * (barring an attack on the mac and deliberately-crafted input),
3140 * in case the caller doesn't check the return status properly. */
3141 *mac_length = mac_size;
3142 /* If mac_size is 0 then mac may be NULL and then the
3143 * call to memset would have undefined behavior. */
3144 if( mac_size != 0 )
3145 memset( mac, '!', mac_size );
3146
Gilles Peskine89167cb2018-07-08 20:12:23 +02003147 if( ! operation->is_sign )
3148 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003149 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003150 }
mohammad16036df908f2018-04-02 08:34:15 -07003151
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003152 status = psa_mac_finish_internal( operation, mac, mac_size );
3153
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003154 if( status == PSA_SUCCESS )
3155 {
3156 status = psa_mac_abort( operation );
3157 if( status == PSA_SUCCESS )
3158 *mac_length = operation->mac_size;
3159 else
3160 memset( mac, '!', mac_size );
3161 }
3162 else
3163 psa_mac_abort( operation );
3164 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003165}
3166
Gilles Peskineacd4be32018-07-08 19:56:25 +02003167psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3168 const uint8_t *mac,
3169 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003170{
Gilles Peskine828ed142018-06-18 23:25:51 +02003171 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003172 psa_status_t status;
3173
Jaeden Amero252ef282019-02-15 14:05:35 +00003174 if( operation->alg == 0 )
3175 {
3176 return( PSA_ERROR_BAD_STATE );
3177 }
3178
Gilles Peskine89167cb2018-07-08 20:12:23 +02003179 if( operation->is_sign )
3180 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003181 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003182 }
3183 if( operation->mac_size != mac_length )
3184 {
3185 status = PSA_ERROR_INVALID_SIGNATURE;
3186 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003187 }
mohammad16036df908f2018-04-02 08:34:15 -07003188
3189 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003190 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003191 if( status != PSA_SUCCESS )
3192 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003193
3194 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3195 status = PSA_ERROR_INVALID_SIGNATURE;
3196
3197cleanup:
3198 if( status == PSA_SUCCESS )
3199 status = psa_mac_abort( operation );
3200 else
3201 psa_mac_abort( operation );
3202
Gilles Peskine3f108122018-12-07 18:14:53 +01003203 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003204
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003205 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003206}
3207
3208
Gilles Peskine20035e32018-02-03 22:44:14 +01003209
Gilles Peskine20035e32018-02-03 22:44:14 +01003210/****************************************************************/
3211/* Asymmetric cryptography */
3212/****************************************************************/
3213
John Durkop6ba40d12020-11-10 08:50:04 -08003214#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3215 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003216/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003217 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003218static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3219 size_t hash_length,
3220 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003221{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003222 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003223 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003224 *md_alg = mbedtls_md_get_type( md_info );
3225
3226 /* The Mbed TLS RSA module uses an unsigned int for hash length
3227 * parameters. Validate that it fits so that we don't risk an
3228 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003229#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003230 if( hash_length > UINT_MAX )
3231 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003232#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003233
John Durkop0e005192020-10-31 22:06:54 -07003234#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003235 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3236 * must be correct. */
3237 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3238 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003239 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003240 if( md_info == NULL )
3241 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003242 if( mbedtls_md_get_size( md_info ) != hash_length )
3243 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003244 }
John Durkop0e005192020-10-31 22:06:54 -07003245#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003246
John Durkop0e005192020-10-31 22:06:54 -07003247#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003248 /* PSS requires a hash internally. */
3249 if( PSA_ALG_IS_RSA_PSS( alg ) )
3250 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003251 if( md_info == NULL )
3252 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003253 }
John Durkop0e005192020-10-31 22:06:54 -07003254#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003255
Gilles Peskine61b91d42018-06-08 16:09:36 +02003256 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003257}
3258
Gilles Peskine2b450e32018-06-27 15:42:46 +02003259static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3260 psa_algorithm_t alg,
3261 const uint8_t *hash,
3262 size_t hash_length,
3263 uint8_t *signature,
3264 size_t signature_size,
3265 size_t *signature_length )
3266{
3267 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003269 mbedtls_md_type_t md_alg;
3270
3271 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3272 if( status != PSA_SUCCESS )
3273 return( status );
3274
Gilles Peskine630a18a2018-06-29 17:49:35 +02003275 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003276 return( PSA_ERROR_BUFFER_TOO_SMALL );
3277
John Durkop0e005192020-10-31 22:06:54 -07003278#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003279 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3280 {
3281 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3282 MBEDTLS_MD_NONE );
3283 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003284 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003285 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003286 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003287 md_alg,
3288 (unsigned int) hash_length,
3289 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003290 signature );
3291 }
3292 else
John Durkop0e005192020-10-31 22:06:54 -07003293#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3294#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003295 if( PSA_ALG_IS_RSA_PSS( alg ) )
3296 {
3297 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3298 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003299 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003300 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003301 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003302 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003303 (unsigned int) hash_length,
3304 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003305 signature );
3306 }
3307 else
John Durkop0e005192020-10-31 22:06:54 -07003308#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003309 {
3310 return( PSA_ERROR_INVALID_ARGUMENT );
3311 }
3312
3313 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003314 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003315 return( mbedtls_to_psa_error( ret ) );
3316}
3317
3318static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3319 psa_algorithm_t alg,
3320 const uint8_t *hash,
3321 size_t hash_length,
3322 const uint8_t *signature,
3323 size_t signature_length )
3324{
3325 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003327 mbedtls_md_type_t md_alg;
3328
3329 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3330 if( status != PSA_SUCCESS )
3331 return( status );
3332
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003333 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3334 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003335
John Durkop0e005192020-10-31 22:06:54 -07003336#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003337 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3338 {
3339 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3340 MBEDTLS_MD_NONE );
3341 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003342 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003343 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003344 MBEDTLS_RSA_PUBLIC,
3345 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003346 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003347 hash,
3348 signature );
3349 }
3350 else
John Durkop0e005192020-10-31 22:06:54 -07003351#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3352#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003353 if( PSA_ALG_IS_RSA_PSS( alg ) )
3354 {
3355 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3356 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003357 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003358 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003359 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003360 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003361 (unsigned int) hash_length,
3362 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003363 signature );
3364 }
3365 else
John Durkop0e005192020-10-31 22:06:54 -07003366#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003367 {
3368 return( PSA_ERROR_INVALID_ARGUMENT );
3369 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003370
3371 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3372 * the rest of the signature is invalid". This has little use in
3373 * practice and PSA doesn't report this distinction. */
3374 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3375 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003376 return( mbedtls_to_psa_error( ret ) );
3377}
John Durkop6ba40d12020-11-10 08:50:04 -08003378#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3379 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003380
John Durkop6ba40d12020-11-10 08:50:04 -08003381#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3382 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003383/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3384 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3385 * (even though these functions don't modify it). */
3386static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3387 psa_algorithm_t alg,
3388 const uint8_t *hash,
3389 size_t hash_length,
3390 uint8_t *signature,
3391 size_t signature_size,
3392 size_t *signature_length )
3393{
Janos Follath24eed8d2019-11-22 13:21:35 +00003394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003395 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003396 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003397 mbedtls_mpi_init( &r );
3398 mbedtls_mpi_init( &s );
3399
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003400 if( signature_size < 2 * curve_bytes )
3401 {
3402 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3403 goto cleanup;
3404 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003405
John Durkop0ea39e02020-10-13 19:58:20 -07003406#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003407 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3408 {
3409 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3410 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3411 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003412 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3413 &ecp->d, hash,
3414 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003415 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003416 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003417 }
3418 else
John Durkop0ea39e02020-10-13 19:58:20 -07003419#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003420 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003421 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003422 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3423 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003424 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003425 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003426 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003427
3428 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3429 signature,
3430 curve_bytes ) );
3431 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3432 signature + curve_bytes,
3433 curve_bytes ) );
3434
3435cleanup:
3436 mbedtls_mpi_free( &r );
3437 mbedtls_mpi_free( &s );
3438 if( ret == 0 )
3439 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003440 return( mbedtls_to_psa_error( ret ) );
3441}
3442
3443static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3444 const uint8_t *hash,
3445 size_t hash_length,
3446 const uint8_t *signature,
3447 size_t signature_length )
3448{
Janos Follath24eed8d2019-11-22 13:21:35 +00003449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003450 mbedtls_mpi r, s;
3451 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3452 mbedtls_mpi_init( &r );
3453 mbedtls_mpi_init( &s );
3454
3455 if( signature_length != 2 * curve_bytes )
3456 return( PSA_ERROR_INVALID_SIGNATURE );
3457
3458 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3459 signature,
3460 curve_bytes ) );
3461 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3462 signature + curve_bytes,
3463 curve_bytes ) );
3464
Steven Cooremanacda8342020-07-24 23:09:52 +02003465 /* Check whether the public part is loaded. If not, load it. */
3466 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3467 {
3468 MBEDTLS_MPI_CHK(
3469 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003470 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003471 }
3472
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003473 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3474 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003475
3476cleanup:
3477 mbedtls_mpi_free( &r );
3478 mbedtls_mpi_free( &s );
3479 return( mbedtls_to_psa_error( ret ) );
3480}
John Durkop6ba40d12020-11-10 08:50:04 -08003481#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3482 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003483
Ronald Croncf56a0a2020-08-04 09:51:30 +02003484psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003485 psa_algorithm_t alg,
3486 const uint8_t *hash,
3487 size_t hash_length,
3488 uint8_t *signature,
3489 size_t signature_size,
3490 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003491{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003492 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003493 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003494 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003495
3496 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003497 /* Immediately reject a zero-length signature buffer. This guarantees
3498 * that signature must be a valid pointer. (On the other hand, the hash
3499 * buffer can in principle be empty since it doesn't actually have
3500 * to be a hash.) */
3501 if( signature_size == 0 )
3502 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003503
Ronald Cron5c522922020-11-14 16:35:34 +01003504 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3505 PSA_KEY_USAGE_SIGN_HASH,
3506 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003507 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003508 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003509 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003510 {
3511 status = PSA_ERROR_INVALID_ARGUMENT;
3512 goto exit;
3513 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003514
Steven Cooremancd84cb42020-07-16 20:28:36 +02003515 /* Try any of the available accelerators first */
3516 status = psa_driver_wrapper_sign_hash( slot,
3517 alg,
3518 hash,
3519 hash_length,
3520 signature,
3521 signature_size,
3522 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003523 if( status != PSA_ERROR_NOT_SUPPORTED ||
3524 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003525 goto exit;
3526
Steven Cooreman7a250572020-07-17 16:43:05 +02003527 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003528#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3529 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003530 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003531 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003532 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003533
Ronald Cron90857082020-11-25 14:58:33 +01003534 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3535 slot->key.data,
3536 slot->key.bytes,
3537 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003538 if( status != PSA_SUCCESS )
3539 goto exit;
3540
Steven Cooremana2371e52020-07-28 14:30:39 +02003541 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003542 alg,
3543 hash, hash_length,
3544 signature, signature_size,
3545 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003546
Steven Cooremana2371e52020-07-28 14:30:39 +02003547 mbedtls_rsa_free( rsa );
3548 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003549 }
3550 else
John Durkop6ba40d12020-11-10 08:50:04 -08003551#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3552 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003553 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003554 {
John Durkop9814fa22020-11-04 12:28:15 -08003555#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3556 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003557 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003558#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003559 PSA_ALG_IS_ECDSA( alg )
3560#else
3561 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3562#endif
3563 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003564 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003565 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003566 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3567 slot->key.data,
3568 slot->key.bytes,
3569 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003570 if( status != PSA_SUCCESS )
3571 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003572 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003573 alg,
3574 hash, hash_length,
3575 signature, signature_size,
3576 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003577 mbedtls_ecp_keypair_free( ecp );
3578 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003579 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003580 else
John Durkop9814fa22020-11-04 12:28:15 -08003581#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3582 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003583 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003584 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003585 }
itayzafrir5c753392018-05-08 11:18:38 +03003586 }
3587 else
itayzafrir5c753392018-05-08 11:18:38 +03003588 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003589 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003590 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003591
3592exit:
3593 /* Fill the unused part of the output buffer (the whole buffer on error,
3594 * the trailing part on success) with something that isn't a valid mac
3595 * (barring an attack on the mac and deliberately-crafted input),
3596 * in case the caller doesn't check the return status properly. */
3597 if( status == PSA_SUCCESS )
3598 memset( signature + *signature_length, '!',
3599 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003600 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003601 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003602 /* If signature_size is 0 then we have nothing to do. We must not call
3603 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003604
Ronald Cron5c522922020-11-14 16:35:34 +01003605 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003606
Ronald Cron5c522922020-11-14 16:35:34 +01003607 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003608}
3609
Ronald Croncf56a0a2020-08-04 09:51:30 +02003610psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003611 psa_algorithm_t alg,
3612 const uint8_t *hash,
3613 size_t hash_length,
3614 const uint8_t *signature,
3615 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003616{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003617 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003618 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003619 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003620
Ronald Cron5c522922020-11-14 16:35:34 +01003621 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3622 PSA_KEY_USAGE_VERIFY_HASH,
3623 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003624 if( status != PSA_SUCCESS )
3625 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003626
Steven Cooreman55ae2172020-07-17 19:46:15 +02003627 /* Try any of the available accelerators first */
3628 status = psa_driver_wrapper_verify_hash( slot,
3629 alg,
3630 hash,
3631 hash_length,
3632 signature,
3633 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003634 if( status != PSA_ERROR_NOT_SUPPORTED ||
3635 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003636 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003637
John Durkop6ba40d12020-11-10 08:50:04 -08003638#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3639 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003640 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003642 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003643
Ronald Cron90857082020-11-25 14:58:33 +01003644 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3645 slot->key.data,
3646 slot->key.bytes,
3647 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003648 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003649 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003650
Steven Cooremana2371e52020-07-28 14:30:39 +02003651 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003652 alg,
3653 hash, hash_length,
3654 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003655 mbedtls_rsa_free( rsa );
3656 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003657 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003658 }
3659 else
John Durkop6ba40d12020-11-10 08:50:04 -08003660#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3661 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003662 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003663 {
John Durkop9814fa22020-11-04 12:28:15 -08003664#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3665 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003666 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003667 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003668 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003669 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3670 slot->key.data,
3671 slot->key.bytes,
3672 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003673 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003674 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003675 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003676 hash, hash_length,
3677 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003678 mbedtls_ecp_keypair_free( ecp );
3679 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003680 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003681 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003682 else
John Durkop9814fa22020-11-04 12:28:15 -08003683#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3684 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003685 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003686 status = PSA_ERROR_INVALID_ARGUMENT;
3687 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003688 }
itayzafrir5c753392018-05-08 11:18:38 +03003689 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003690 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003691 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003692 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003693 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003694
3695exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003696 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003697
Ronald Cron5c522922020-11-14 16:35:34 +01003698 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003699}
3700
John Durkop0e005192020-10-31 22:06:54 -07003701#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003702static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3703 mbedtls_rsa_context *rsa )
3704{
3705 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3706 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3707 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3708 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3709}
John Durkop0e005192020-10-31 22:06:54 -07003710#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003711
Ronald Croncf56a0a2020-08-04 09:51:30 +02003712psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003713 psa_algorithm_t alg,
3714 const uint8_t *input,
3715 size_t input_length,
3716 const uint8_t *salt,
3717 size_t salt_length,
3718 uint8_t *output,
3719 size_t output_size,
3720 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003721{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003722 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003723 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003724 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003725
Darryl Green5cc689a2018-07-24 15:34:10 +01003726 (void) input;
3727 (void) input_length;
3728 (void) salt;
3729 (void) output;
3730 (void) output_size;
3731
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003732 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003733
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003734 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3735 return( PSA_ERROR_INVALID_ARGUMENT );
3736
Ronald Cron5c522922020-11-14 16:35:34 +01003737 status = psa_get_and_lock_transparent_key_slot_with_policy(
3738 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003739 if( status != PSA_SUCCESS )
3740 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003741 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3742 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003743 {
3744 status = PSA_ERROR_INVALID_ARGUMENT;
3745 goto exit;
3746 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003747
John Durkop6ba40d12020-11-10 08:50:04 -08003748#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3749 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003750 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003751 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003752 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003753 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3754 slot->key.data,
3755 slot->key.bytes,
3756 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003757 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003758 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003759
3760 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003761 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003762 status = PSA_ERROR_BUFFER_TOO_SMALL;
3763 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003764 }
John Durkop0e005192020-10-31 22:06:54 -07003765#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003766 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003767 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003768 status = mbedtls_to_psa_error(
3769 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003770 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003771 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003772 MBEDTLS_RSA_PUBLIC,
3773 input_length,
3774 input,
3775 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003776 }
3777 else
John Durkop0e005192020-10-31 22:06:54 -07003778#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3779#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003780 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003781 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003782 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003783 status = mbedtls_to_psa_error(
3784 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003785 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003786 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003787 MBEDTLS_RSA_PUBLIC,
3788 salt, salt_length,
3789 input_length,
3790 input,
3791 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003792 }
3793 else
John Durkop0e005192020-10-31 22:06:54 -07003794#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003795 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003796 status = PSA_ERROR_INVALID_ARGUMENT;
3797 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003798 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003799rsa_exit:
3800 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003801 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003802
Steven Cooremana2371e52020-07-28 14:30:39 +02003803 mbedtls_rsa_free( rsa );
3804 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003805 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003806 else
John Durkop9814fa22020-11-04 12:28:15 -08003807#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003808 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003809 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003810 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003811 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003812
3813exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003814 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003815
Ronald Cron5c522922020-11-14 16:35:34 +01003816 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003817}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003818
Ronald Croncf56a0a2020-08-04 09:51:30 +02003819psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003820 psa_algorithm_t alg,
3821 const uint8_t *input,
3822 size_t input_length,
3823 const uint8_t *salt,
3824 size_t salt_length,
3825 uint8_t *output,
3826 size_t output_size,
3827 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003828{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003829 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003830 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003831 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003832
Darryl Green5cc689a2018-07-24 15:34:10 +01003833 (void) input;
3834 (void) input_length;
3835 (void) salt;
3836 (void) output;
3837 (void) output_size;
3838
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003839 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003840
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003841 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3842 return( PSA_ERROR_INVALID_ARGUMENT );
3843
Ronald Cron5c522922020-11-14 16:35:34 +01003844 status = psa_get_and_lock_transparent_key_slot_with_policy(
3845 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003846 if( status != PSA_SUCCESS )
3847 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003848 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003849 {
3850 status = PSA_ERROR_INVALID_ARGUMENT;
3851 goto exit;
3852 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003853
John Durkop6ba40d12020-11-10 08:50:04 -08003854#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3855 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003856 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003857 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003858 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003859 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3860 slot->key.data,
3861 slot->key.bytes,
3862 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003863 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003864 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865
Steven Cooremana2371e52020-07-28 14:30:39 +02003866 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003867 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003868 status = PSA_ERROR_INVALID_ARGUMENT;
3869 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003870 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871
John Durkop0e005192020-10-31 22:06:54 -07003872#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003873 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003875 status = mbedtls_to_psa_error(
3876 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003877 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003878 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003879 MBEDTLS_RSA_PRIVATE,
3880 output_length,
3881 input,
3882 output,
3883 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884 }
3885 else
John Durkop0e005192020-10-31 22:06:54 -07003886#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3887#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003888 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003889 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003890 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003891 status = mbedtls_to_psa_error(
3892 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003893 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003894 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003895 MBEDTLS_RSA_PRIVATE,
3896 salt, salt_length,
3897 output_length,
3898 input,
3899 output,
3900 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901 }
3902 else
John Durkop0e005192020-10-31 22:06:54 -07003903#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003904 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003905 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003907
Steven Cooreman4fed4552020-08-03 14:46:03 +02003908rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003909 mbedtls_rsa_free( rsa );
3910 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003911 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003912 else
John Durkop6ba40d12020-11-10 08:50:04 -08003913#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3914 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003915 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003916 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003917 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003918
3919exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003920 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003921
Ronald Cron5c522922020-11-14 16:35:34 +01003922 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003923}
Gilles Peskine20035e32018-02-03 22:44:14 +01003924
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003925
3926
mohammad1603503973b2018-03-12 15:59:30 +02003927/****************************************************************/
3928/* Symmetric cryptography */
3929/****************************************************************/
3930
Gilles Peskinee553c652018-06-04 16:22:46 +02003931static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003932 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003933 psa_algorithm_t alg,
3934 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003935{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003936 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003937 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003938 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003939 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003940 size_t key_bits;
3941 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003942 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3943 PSA_KEY_USAGE_ENCRYPT :
3944 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003945
Steven Cooremand3feccd2020-09-01 15:56:14 +02003946 /* A context must be freshly initialized before it can be set up. */
3947 if( operation->alg != 0 )
3948 return( PSA_ERROR_BAD_STATE );
3949
Steven Cooremana07b9972020-09-10 14:54:14 +02003950 /* The requested algorithm must be one that can be processed by cipher. */
3951 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003952 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003953
Steven Cooremanef8575e2020-09-11 11:44:50 +02003954 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003955 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003956 if( status != PSA_SUCCESS )
3957 goto exit;
3958
3959 /* Initialize the operation struct members, except for alg. The alg member
3960 * is used to indicate to psa_cipher_abort that there are resources to free,
3961 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003962 operation->key_set = 0;
3963 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003964 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003965 operation->iv_size = 0;
3966 operation->block_size = 0;
3967 if( alg == PSA_ALG_ECB_NO_PADDING )
3968 operation->iv_required = 0;
3969 else
3970 operation->iv_required = 1;
3971
Steven Cooremana07b9972020-09-10 14:54:14 +02003972 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003973 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003974 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003975 slot,
3976 alg );
3977 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003978 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003979 slot,
3980 alg );
3981
Steven Cooremanef8575e2020-09-11 11:44:50 +02003982 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003983 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003984 /* Once the driver context is initialised, it needs to be freed using
3985 * psa_cipher_abort. Indicate this through setting alg. */
3986 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003987 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02003988
Steven Cooremand3feccd2020-09-01 15:56:14 +02003989 if( status != PSA_ERROR_NOT_SUPPORTED ||
3990 psa_key_lifetime_is_external( slot->attr.lifetime ) )
3991 goto exit;
3992
Steven Cooremana07b9972020-09-10 14:54:14 +02003993 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02003994 * available for the given algorithm & key. */
3995 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02003996
Steven Cooremana07b9972020-09-10 14:54:14 +02003997 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02003998 * psa_cipher_abort. Indicate there is something to be freed through setting
3999 * alg, and indicate the operation is being done using mbedtls crypto through
4000 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004001 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004002 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004003
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004004 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004005 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004006 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004007 {
4008 status = PSA_ERROR_NOT_SUPPORTED;
4009 goto exit;
4010 }
mohammad1603503973b2018-03-12 15:59:30 +02004011
mohammad1603503973b2018-03-12 15:59:30 +02004012 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004013 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004014 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004015
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004016#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004017 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004018 {
4019 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004020 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004021 memcpy( keys, slot->key.data, 16 );
4022 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004023 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4024 keys,
4025 192, cipher_operation );
4026 }
4027 else
4028#endif
4029 {
4030 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004031 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004032 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004033 }
Moran Peker41deec42018-04-04 15:43:05 +03004034 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004035 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004036
mohammad16038481e742018-03-18 13:57:31 +02004037#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004038 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004039 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004040 case PSA_ALG_CBC_NO_PADDING:
4041 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4042 MBEDTLS_PADDING_NONE );
4043 break;
4044 case PSA_ALG_CBC_PKCS7:
4045 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4046 MBEDTLS_PADDING_PKCS7 );
4047 break;
4048 default:
4049 /* The algorithm doesn't involve padding. */
4050 ret = 0;
4051 break;
4052 }
4053 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004054 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004055#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4056
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004057 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004058 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004059 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4060 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004061 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004062 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004063 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004064#if defined(MBEDTLS_CHACHA20_C)
4065 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004066 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004067 operation->iv_size = 12;
4068#endif
mohammad1603503973b2018-03-12 15:59:30 +02004069
Steven Cooreman7df02922020-09-09 15:28:49 +02004070 status = PSA_SUCCESS;
4071
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004072exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004073 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004074 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004075 if( status == PSA_SUCCESS )
4076 {
4077 /* Update operation flags for both driver and software implementations */
4078 operation->key_set = 1;
4079 }
4080 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004081 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004082
Ronald Cron5c522922020-11-14 16:35:34 +01004083 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004084
Ronald Cron5c522922020-11-14 16:35:34 +01004085 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004086}
4087
Gilles Peskinefe119512018-07-08 21:39:34 +02004088psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004089 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004090 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004091{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004092 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004093}
4094
Gilles Peskinefe119512018-07-08 21:39:34 +02004095psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004096 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004097 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004098{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004099 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004100}
4101
Gilles Peskinefe119512018-07-08 21:39:34 +02004102psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004103 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004104 size_t iv_size,
4105 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004106{
itayzafrir534bd7c2018-08-02 13:56:32 +03004107 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004109 if( operation->iv_set || ! operation->iv_required )
4110 {
4111 return( PSA_ERROR_BAD_STATE );
4112 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004113
Steven Cooremanef8575e2020-09-11 11:44:50 +02004114 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004115 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004116 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004117 iv,
4118 iv_size,
4119 iv_length );
4120 goto exit;
4121 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004122
Moran Peker41deec42018-04-04 15:43:05 +03004123 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004124 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004125 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004126 goto exit;
4127 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004128 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004129 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004130 if( ret != 0 )
4131 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004132 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004133 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004134 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004135
mohammad16038481e742018-03-18 13:57:31 +02004136 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004137 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004138
Moran Peker395db872018-05-31 14:07:14 +03004139exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004140 if( status == PSA_SUCCESS )
4141 operation->iv_set = 1;
4142 else
Moran Peker395db872018-05-31 14:07:14 +03004143 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004144 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004145}
4146
Gilles Peskinefe119512018-07-08 21:39:34 +02004147psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004148 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004149 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004150{
itayzafrir534bd7c2018-08-02 13:56:32 +03004151 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004153 if( operation->iv_set || ! operation->iv_required )
4154 {
4155 return( PSA_ERROR_BAD_STATE );
4156 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004157
Steven Cooremanef8575e2020-09-11 11:44:50 +02004158 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004159 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004160 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004161 iv,
4162 iv_length );
4163 goto exit;
4164 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004165
Moran Pekera28258c2018-05-29 16:25:04 +03004166 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004167 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004168 status = PSA_ERROR_INVALID_ARGUMENT;
4169 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004170 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004171 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4172 status = mbedtls_to_psa_error( ret );
4173exit:
4174 if( status == PSA_SUCCESS )
4175 operation->iv_set = 1;
4176 else
mohammad1603503973b2018-03-12 15:59:30 +02004177 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004178 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004179}
4180
Steven Cooreman177deba2020-09-07 17:14:14 +02004181/* Process input for which the algorithm is set to ECB mode. This requires
4182 * manual processing, since the PSA API is defined as being able to process
4183 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4184 * underlying mbedtls_cipher_update only takes full blocks. */
4185static psa_status_t psa_cipher_update_ecb_internal(
4186 mbedtls_cipher_context_t *ctx,
4187 const uint8_t *input,
4188 size_t input_length,
4189 uint8_t *output,
4190 size_t output_size,
4191 size_t *output_length )
4192{
4193 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4194 size_t block_size = ctx->cipher_info->block_size;
4195 size_t internal_output_length = 0;
4196 *output_length = 0;
4197
4198 if( input_length == 0 )
4199 {
4200 status = PSA_SUCCESS;
4201 goto exit;
4202 }
4203
4204 if( ctx->unprocessed_len > 0 )
4205 {
4206 /* Fill up to block size, and run the block if there's a full one. */
4207 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4208
4209 if( input_length < bytes_to_copy )
4210 bytes_to_copy = input_length;
4211
4212 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4213 input, bytes_to_copy );
4214 input_length -= bytes_to_copy;
4215 input += bytes_to_copy;
4216 ctx->unprocessed_len += bytes_to_copy;
4217
4218 if( ctx->unprocessed_len == block_size )
4219 {
4220 status = mbedtls_to_psa_error(
4221 mbedtls_cipher_update( ctx,
4222 ctx->unprocessed_data,
4223 block_size,
4224 output, &internal_output_length ) );
4225
4226 if( status != PSA_SUCCESS )
4227 goto exit;
4228
4229 output += internal_output_length;
4230 output_size -= internal_output_length;
4231 *output_length += internal_output_length;
4232 ctx->unprocessed_len = 0;
4233 }
4234 }
4235
4236 while( input_length >= block_size )
4237 {
4238 /* Run all full blocks we have, one by one */
4239 status = mbedtls_to_psa_error(
4240 mbedtls_cipher_update( ctx, input,
4241 block_size,
4242 output, &internal_output_length ) );
4243
4244 if( status != PSA_SUCCESS )
4245 goto exit;
4246
4247 input_length -= block_size;
4248 input += block_size;
4249
4250 output += internal_output_length;
4251 output_size -= internal_output_length;
4252 *output_length += internal_output_length;
4253 }
4254
4255 if( input_length > 0 )
4256 {
4257 /* Save unprocessed bytes for later processing */
4258 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4259 input, input_length );
4260 ctx->unprocessed_len += input_length;
4261 }
4262
4263 status = PSA_SUCCESS;
4264
4265exit:
4266 return( status );
4267}
4268
Gilles Peskinee553c652018-06-04 16:22:46 +02004269psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4270 const uint8_t *input,
4271 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004272 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004273 size_t output_size,
4274 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004275{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004276 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004277 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004278 if( operation->alg == 0 )
4279 {
4280 return( PSA_ERROR_BAD_STATE );
4281 }
4282 if( operation->iv_required && ! operation->iv_set )
4283 {
4284 return( PSA_ERROR_BAD_STATE );
4285 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004286
Steven Cooremanef8575e2020-09-11 11:44:50 +02004287 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004288 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004289 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004290 input,
4291 input_length,
4292 output,
4293 output_size,
4294 output_length );
4295 goto exit;
4296 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004297
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004298 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004299 {
4300 /* Take the unprocessed partial block left over from previous
4301 * update calls, if any, plus the input to this call. Remove
4302 * the last partial block, if any. You get the data that will be
4303 * output in this call. */
4304 expected_output_size =
4305 ( operation->ctx.cipher.unprocessed_len + input_length )
4306 / operation->block_size * operation->block_size;
4307 }
4308 else
4309 {
4310 expected_output_size = input_length;
4311 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004312
Gilles Peskine89d789c2018-06-04 17:17:16 +02004313 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004314 {
4315 status = PSA_ERROR_BUFFER_TOO_SMALL;
4316 goto exit;
4317 }
mohammad160382759612018-03-12 18:16:40 +02004318
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004319 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4320 {
4321 /* mbedtls_cipher_update has an API inconsistency: it will only
4322 * process a single block at a time in ECB mode. Abstract away that
4323 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004324 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4325 input,
4326 input_length,
4327 output,
4328 output_size,
4329 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004330 }
4331 else
4332 {
4333 status = mbedtls_to_psa_error(
4334 mbedtls_cipher_update( &operation->ctx.cipher, input,
4335 input_length, output, output_length ) );
4336 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004337exit:
4338 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004339 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004340 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004341}
4342
Gilles Peskinee553c652018-06-04 16:22:46 +02004343psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4344 uint8_t *output,
4345 size_t output_size,
4346 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004347{
David Saadab4ecc272019-02-14 13:48:10 +02004348 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004349 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004350 if( operation->alg == 0 )
4351 {
4352 return( PSA_ERROR_BAD_STATE );
4353 }
4354 if( operation->iv_required && ! operation->iv_set )
4355 {
4356 return( PSA_ERROR_BAD_STATE );
4357 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004358
Steven Cooremanef8575e2020-09-11 11:44:50 +02004359 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004360 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004361 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004362 output,
4363 output_size,
4364 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004365 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004366 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004367
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004368 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004369 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004370 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004371 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004372 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004373 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004374 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004375 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004376 }
4377
Steven Cooremanef8575e2020-09-11 11:44:50 +02004378 status = mbedtls_to_psa_error(
4379 mbedtls_cipher_finish( &operation->ctx.cipher,
4380 temp_output_buffer,
4381 output_length ) );
4382 if( status != PSA_SUCCESS )
4383 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004384
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004385 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004386 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004387 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004388 memcpy( output, temp_output_buffer, *output_length );
4389 else
Janos Follath315b51c2018-07-09 16:04:51 +01004390 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004391
Steven Cooremanef8575e2020-09-11 11:44:50 +02004392exit:
4393 if( operation->mbedtls_in_use == 1 )
4394 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004395
Steven Cooremanef8575e2020-09-11 11:44:50 +02004396 if( status == PSA_SUCCESS )
4397 return( psa_cipher_abort( operation ) );
4398 else
4399 {
4400 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004401 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004402
Steven Cooremanef8575e2020-09-11 11:44:50 +02004403 return( status );
4404 }
mohammad1603503973b2018-03-12 15:59:30 +02004405}
4406
Gilles Peskinee553c652018-06-04 16:22:46 +02004407psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4408{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004409 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004410 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004411 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004412 * in use. It's ok to call abort on such an object, and there's
4413 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004414 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004415 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004416
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004417 /* Sanity check (shouldn't happen: operation->alg should
4418 * always have been initialized to a valid value). */
4419 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4420 return( PSA_ERROR_BAD_STATE );
4421
Steven Cooremanef8575e2020-09-11 11:44:50 +02004422 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004423 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004424 else
4425 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004426
Moran Peker41deec42018-04-04 15:43:05 +03004427 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004428 operation->key_set = 0;
4429 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004430 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004431 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004432 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004433 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004434
Moran Peker395db872018-05-31 14:07:14 +03004435 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004436}
4437
Gilles Peskinea0655c32018-04-30 17:06:50 +02004438
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004439
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004440
mohammad16035955c982018-04-26 00:53:03 +03004441/****************************************************************/
4442/* AEAD */
4443/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004444
Gilles Peskineedf9a652018-08-17 18:11:56 +02004445typedef struct
4446{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004447 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004448 const mbedtls_cipher_info_t *cipher_info;
4449 union
4450 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004451 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004452#if defined(MBEDTLS_CCM_C)
4453 mbedtls_ccm_context ccm;
4454#endif /* MBEDTLS_CCM_C */
4455#if defined(MBEDTLS_GCM_C)
4456 mbedtls_gcm_context gcm;
4457#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004458#if defined(MBEDTLS_CHACHAPOLY_C)
4459 mbedtls_chachapoly_context chachapoly;
4460#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004461 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004462 psa_algorithm_t core_alg;
4463 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004464 uint8_t tag_length;
4465} aead_operation_t;
4466
Ronald Cronf95a2b12020-10-22 15:24:49 +02004467#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4468
Gilles Peskine30a9e412019-01-14 18:36:12 +01004469static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004470{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004471 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004472 {
4473#if defined(MBEDTLS_CCM_C)
4474 case PSA_ALG_CCM:
4475 mbedtls_ccm_free( &operation->ctx.ccm );
4476 break;
4477#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004478#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004479 case PSA_ALG_GCM:
4480 mbedtls_gcm_free( &operation->ctx.gcm );
4481 break;
4482#endif /* MBEDTLS_GCM_C */
4483 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004484
Ronald Cron5c522922020-11-14 16:35:34 +01004485 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004486}
4487
4488static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004489 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004490 psa_key_usage_t usage,
4491 psa_algorithm_t alg )
4492{
4493 psa_status_t status;
4494 size_t key_bits;
4495 mbedtls_cipher_id_t cipher_id;
4496
Ronald Cron5c522922020-11-14 16:35:34 +01004497 status = psa_get_and_lock_transparent_key_slot_with_policy(
4498 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004499 if( status != PSA_SUCCESS )
4500 return( status );
4501
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004502 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004503
4504 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004505 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004506 &cipher_id );
4507 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004508 {
4509 status = PSA_ERROR_NOT_SUPPORTED;
4510 goto cleanup;
4511 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004512
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004513 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004514 {
4515#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004516 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4517 operation->core_alg = PSA_ALG_CCM;
4518 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004519 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4520 * The call to mbedtls_ccm_encrypt_and_tag or
4521 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004522 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004523 {
4524 status = PSA_ERROR_INVALID_ARGUMENT;
4525 goto cleanup;
4526 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004527 mbedtls_ccm_init( &operation->ctx.ccm );
4528 status = mbedtls_to_psa_error(
4529 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004530 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004531 (unsigned int) key_bits ) );
4532 if( status != 0 )
4533 goto cleanup;
4534 break;
4535#endif /* MBEDTLS_CCM_C */
4536
4537#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004538 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4539 operation->core_alg = PSA_ALG_GCM;
4540 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004541 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4542 * The call to mbedtls_gcm_crypt_and_tag or
4543 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004544 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004545 {
4546 status = PSA_ERROR_INVALID_ARGUMENT;
4547 goto cleanup;
4548 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004549 mbedtls_gcm_init( &operation->ctx.gcm );
4550 status = mbedtls_to_psa_error(
4551 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004552 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004553 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004554 if( status != 0 )
4555 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004556 break;
4557#endif /* MBEDTLS_GCM_C */
4558
Gilles Peskine26869f22019-05-06 15:25:00 +02004559#if defined(MBEDTLS_CHACHAPOLY_C)
4560 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4561 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4562 operation->full_tag_length = 16;
4563 /* We only support the default tag length. */
4564 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004565 {
4566 status = PSA_ERROR_NOT_SUPPORTED;
4567 goto cleanup;
4568 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004569 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4570 status = mbedtls_to_psa_error(
4571 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004572 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004573 if( status != 0 )
4574 goto cleanup;
4575 break;
4576#endif /* MBEDTLS_CHACHAPOLY_C */
4577
Gilles Peskineedf9a652018-08-17 18:11:56 +02004578 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004579 status = PSA_ERROR_NOT_SUPPORTED;
4580 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004581 }
4582
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004583 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4584 {
4585 status = PSA_ERROR_INVALID_ARGUMENT;
4586 goto cleanup;
4587 }
4588 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004589
Gilles Peskineedf9a652018-08-17 18:11:56 +02004590 return( PSA_SUCCESS );
4591
4592cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004593 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004594 return( status );
4595}
4596
Ronald Croncf56a0a2020-08-04 09:51:30 +02004597psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004598 psa_algorithm_t alg,
4599 const uint8_t *nonce,
4600 size_t nonce_length,
4601 const uint8_t *additional_data,
4602 size_t additional_data_length,
4603 const uint8_t *plaintext,
4604 size_t plaintext_length,
4605 uint8_t *ciphertext,
4606 size_t ciphertext_size,
4607 size_t *ciphertext_length )
4608{
mohammad16035955c982018-04-26 00:53:03 +03004609 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004610 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004611 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004612
mohammad1603f08a5502018-06-03 15:05:47 +03004613 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004614
Ronald Croncf56a0a2020-08-04 09:51:30 +02004615 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004616 if( status != PSA_SUCCESS )
4617 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004618
Gilles Peskineedf9a652018-08-17 18:11:56 +02004619 /* For all currently supported modes, the tag is at the end of the
4620 * ciphertext. */
4621 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4622 {
4623 status = PSA_ERROR_BUFFER_TOO_SMALL;
4624 goto exit;
4625 }
4626 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004627
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004628#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004629 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004630 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004631 status = mbedtls_to_psa_error(
4632 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4633 MBEDTLS_GCM_ENCRYPT,
4634 plaintext_length,
4635 nonce, nonce_length,
4636 additional_data, additional_data_length,
4637 plaintext, ciphertext,
4638 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004639 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004640 else
4641#endif /* MBEDTLS_GCM_C */
4642#if defined(MBEDTLS_CCM_C)
4643 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004644 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004645 status = mbedtls_to_psa_error(
4646 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4647 plaintext_length,
4648 nonce, nonce_length,
4649 additional_data,
4650 additional_data_length,
4651 plaintext, ciphertext,
4652 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004653 }
mohammad16035c8845f2018-05-09 05:40:09 -07004654 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004655#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004656#if defined(MBEDTLS_CHACHAPOLY_C)
4657 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4658 {
4659 if( nonce_length != 12 || operation.tag_length != 16 )
4660 {
4661 status = PSA_ERROR_NOT_SUPPORTED;
4662 goto exit;
4663 }
4664 status = mbedtls_to_psa_error(
4665 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4666 plaintext_length,
4667 nonce,
4668 additional_data,
4669 additional_data_length,
4670 plaintext,
4671 ciphertext,
4672 tag ) );
4673 }
4674 else
4675#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004676 {
mohammad1603554faad2018-06-03 15:07:38 +03004677 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004678 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004679
Gilles Peskineedf9a652018-08-17 18:11:56 +02004680 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4681 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004682
Gilles Peskineedf9a652018-08-17 18:11:56 +02004683exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004684 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004685 if( status == PSA_SUCCESS )
4686 *ciphertext_length = plaintext_length + operation.tag_length;
4687 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004688}
4689
Gilles Peskineee652a32018-06-01 19:23:52 +02004690/* Locate the tag in a ciphertext buffer containing the encrypted data
4691 * followed by the tag. Return the length of the part preceding the tag in
4692 * *plaintext_length. This is the size of the plaintext in modes where
4693 * the encrypted data has the same size as the plaintext, such as
4694 * CCM and GCM. */
4695static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4696 const uint8_t *ciphertext,
4697 size_t ciphertext_length,
4698 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004699 const uint8_t **p_tag )
4700{
4701 size_t payload_length;
4702 if( tag_length > ciphertext_length )
4703 return( PSA_ERROR_INVALID_ARGUMENT );
4704 payload_length = ciphertext_length - tag_length;
4705 if( payload_length > plaintext_size )
4706 return( PSA_ERROR_BUFFER_TOO_SMALL );
4707 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004708 return( PSA_SUCCESS );
4709}
4710
Ronald Croncf56a0a2020-08-04 09:51:30 +02004711psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004712 psa_algorithm_t alg,
4713 const uint8_t *nonce,
4714 size_t nonce_length,
4715 const uint8_t *additional_data,
4716 size_t additional_data_length,
4717 const uint8_t *ciphertext,
4718 size_t ciphertext_length,
4719 uint8_t *plaintext,
4720 size_t plaintext_size,
4721 size_t *plaintext_length )
4722{
mohammad16035955c982018-04-26 00:53:03 +03004723 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004724 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004725 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004726
Gilles Peskineee652a32018-06-01 19:23:52 +02004727 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004728
Ronald Croncf56a0a2020-08-04 09:51:30 +02004729 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004730 if( status != PSA_SUCCESS )
4731 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004732
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004733 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4734 ciphertext, ciphertext_length,
4735 plaintext_size, &tag );
4736 if( status != PSA_SUCCESS )
4737 goto exit;
4738
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004739#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004740 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004741 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004742 status = mbedtls_to_psa_error(
4743 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4744 ciphertext_length - operation.tag_length,
4745 nonce, nonce_length,
4746 additional_data,
4747 additional_data_length,
4748 tag, operation.tag_length,
4749 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004750 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004751 else
4752#endif /* MBEDTLS_GCM_C */
4753#if defined(MBEDTLS_CCM_C)
4754 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004755 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004756 status = mbedtls_to_psa_error(
4757 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4758 ciphertext_length - operation.tag_length,
4759 nonce, nonce_length,
4760 additional_data,
4761 additional_data_length,
4762 ciphertext, plaintext,
4763 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004764 }
mohammad160339574652018-06-01 04:39:53 -07004765 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004766#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004767#if defined(MBEDTLS_CHACHAPOLY_C)
4768 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4769 {
4770 if( nonce_length != 12 || operation.tag_length != 16 )
4771 {
4772 status = PSA_ERROR_NOT_SUPPORTED;
4773 goto exit;
4774 }
4775 status = mbedtls_to_psa_error(
4776 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4777 ciphertext_length - operation.tag_length,
4778 nonce,
4779 additional_data,
4780 additional_data_length,
4781 tag,
4782 ciphertext,
4783 plaintext ) );
4784 }
4785 else
4786#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004787 {
mohammad1603554faad2018-06-03 15:07:38 +03004788 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004789 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004790
Gilles Peskineedf9a652018-08-17 18:11:56 +02004791 if( status != PSA_SUCCESS && plaintext_size != 0 )
4792 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004793
Gilles Peskineedf9a652018-08-17 18:11:56 +02004794exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004795 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004796 if( status == PSA_SUCCESS )
4797 *plaintext_length = ciphertext_length - operation.tag_length;
4798 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004799}
4800
Gilles Peskinea0655c32018-04-30 17:06:50 +02004801
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004802
Gilles Peskine20035e32018-02-03 22:44:14 +01004803/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004804/* Generators */
4805/****************************************************************/
4806
John Durkop07cc04a2020-11-16 22:08:34 -08004807#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4808 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4809 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4810#define AT_LEAST_ONE_BUILTIN_KDF
4811#endif
4812
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004813#define HKDF_STATE_INIT 0 /* no input yet */
4814#define HKDF_STATE_STARTED 1 /* got salt */
4815#define HKDF_STATE_KEYED 2 /* got key */
4816#define HKDF_STATE_OUTPUT 3 /* output started */
4817
Gilles Peskinecbe66502019-05-16 16:59:18 +02004818static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004819 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004820{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4822 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004823 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004825}
4826
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004828{
4829 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004831 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004832 {
4833 /* The object has (apparently) been initialized but it is not
4834 * in use. It's ok to call abort on such an object, and there's
4835 * nothing to do. */
4836 }
4837 else
John Durkopd0321952020-10-29 21:37:36 -07004838#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004839 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004840 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004841 mbedtls_free( operation->ctx.hkdf.info );
4842 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004843 }
John Durkop07cc04a2020-11-16 22:08:34 -08004844 else
4845#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4846#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4847 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4848 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004850 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004851 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004852 if( operation->ctx.tls12_prf.seed != NULL )
4853 {
4854 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4855 operation->ctx.tls12_prf.seed_length );
4856 mbedtls_free( operation->ctx.tls12_prf.seed );
4857 }
4858
4859 if( operation->ctx.tls12_prf.label != NULL )
4860 {
4861 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4862 operation->ctx.tls12_prf.label_length );
4863 mbedtls_free( operation->ctx.tls12_prf.label );
4864 }
4865
4866 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4867
4868 /* We leave the fields Ai and output_block to be erased safely by the
4869 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004870 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004871 else
John Durkop07cc04a2020-11-16 22:08:34 -08004872#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4873 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004874 {
4875 status = PSA_ERROR_BAD_STATE;
4876 }
Janos Follath083036a2019-06-11 10:22:26 +01004877 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004878 return( status );
4879}
4880
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004882 size_t *capacity)
4883{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004884 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004885 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004886 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004887 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004888 }
4889
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004891 return( PSA_SUCCESS );
4892}
4893
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895 size_t capacity )
4896{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004898 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004899 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004900 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004902 return( PSA_SUCCESS );
4903}
4904
John Durkopd0321952020-10-29 21:37:36 -07004905#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004907 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004908static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004909 psa_algorithm_t hash_alg,
4910 uint8_t *output,
4911 size_t output_length )
4912{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004913 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004914 psa_status_t status;
4915
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004916 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4917 return( PSA_ERROR_BAD_STATE );
4918 hkdf->state = HKDF_STATE_OUTPUT;
4919
Gilles Peskinebef7f142018-07-12 17:22:21 +02004920 while( output_length != 0 )
4921 {
4922 /* Copy what remains of the current block */
4923 uint8_t n = hash_length - hkdf->offset_in_block;
4924 if( n > output_length )
4925 n = (uint8_t) output_length;
4926 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4927 output += n;
4928 output_length -= n;
4929 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004930 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004931 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004932 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004933 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004934 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004935 * object was corrupted or if this function is called directly
4936 * inside the library. */
4937 if( hkdf->block_number == 0xff )
4938 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004939
4940 /* We need a new block */
4941 ++hkdf->block_number;
4942 hkdf->offset_in_block = 0;
4943 status = psa_hmac_setup_internal( &hkdf->hmac,
4944 hkdf->prk, hash_length,
4945 hash_alg );
4946 if( status != PSA_SUCCESS )
4947 return( status );
4948 if( hkdf->block_number != 1 )
4949 {
4950 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4951 hkdf->output_block,
4952 hash_length );
4953 if( status != PSA_SUCCESS )
4954 return( status );
4955 }
4956 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4957 hkdf->info,
4958 hkdf->info_length );
4959 if( status != PSA_SUCCESS )
4960 return( status );
4961 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4962 &hkdf->block_number, 1 );
4963 if( status != PSA_SUCCESS )
4964 return( status );
4965 status = psa_hmac_finish_internal( &hkdf->hmac,
4966 hkdf->output_block,
4967 sizeof( hkdf->output_block ) );
4968 if( status != PSA_SUCCESS )
4969 return( status );
4970 }
4971
4972 return( PSA_SUCCESS );
4973}
John Durkop07cc04a2020-11-16 22:08:34 -08004974#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004975
John Durkop07cc04a2020-11-16 22:08:34 -08004976#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4977 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004978static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4979 psa_tls12_prf_key_derivation_t *tls12_prf,
4980 psa_algorithm_t alg )
4981{
4982 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004983 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004984 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4985 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004986
Janos Follath7742fee2019-06-17 12:58:10 +01004987 /* We can't be wanting more output after block 0xff, otherwise
4988 * the capacity check in psa_key_derivation_output_bytes() would have
4989 * prevented this call. It could happen only if the operation
4990 * object was corrupted or if this function is called directly
4991 * inside the library. */
4992 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004993 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004994
4995 /* We need a new block */
4996 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004997 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004998
4999 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5000 *
5001 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5002 *
5003 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5004 * HMAC_hash(secret, A(2) + seed) +
5005 * HMAC_hash(secret, A(3) + seed) + ...
5006 *
5007 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005008 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005009 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005010 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005011 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005012 * is currently extracted as `output_block` and where i is
5013 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005014 */
5015
Janos Follathea29bfb2019-06-19 12:21:20 +01005016 /* Save the hash context before using it, to preserve the hash state with
5017 * only the inner padding in it. We need this, because inner padding depends
5018 * on the key (secret in the RFC's terminology). */
5019 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5020 if( status != PSA_SUCCESS )
5021 goto cleanup;
5022
5023 /* Calculate A(i) where i = tls12_prf->block_number. */
5024 if( tls12_prf->block_number == 1 )
5025 {
5026 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5027 * the variable seed and in this instance means it in the context of the
5028 * P_hash function, where seed = label + seed.) */
5029 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5030 tls12_prf->label, tls12_prf->label_length );
5031 if( status != PSA_SUCCESS )
5032 goto cleanup;
5033 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5034 tls12_prf->seed, tls12_prf->seed_length );
5035 if( status != PSA_SUCCESS )
5036 goto cleanup;
5037 }
5038 else
5039 {
5040 /* A(i) = HMAC_hash(secret, A(i-1)) */
5041 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5042 tls12_prf->Ai, hash_length );
5043 if( status != PSA_SUCCESS )
5044 goto cleanup;
5045 }
5046
5047 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5048 tls12_prf->Ai, hash_length );
5049 if( status != PSA_SUCCESS )
5050 goto cleanup;
5051 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5052 if( status != PSA_SUCCESS )
5053 goto cleanup;
5054
5055 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5056 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5057 tls12_prf->Ai, hash_length );
5058 if( status != PSA_SUCCESS )
5059 goto cleanup;
5060 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5061 tls12_prf->label, tls12_prf->label_length );
5062 if( status != PSA_SUCCESS )
5063 goto cleanup;
5064 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5065 tls12_prf->seed, tls12_prf->seed_length );
5066 if( status != PSA_SUCCESS )
5067 goto cleanup;
5068 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5069 tls12_prf->output_block, hash_length );
5070 if( status != PSA_SUCCESS )
5071 goto cleanup;
5072 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5073 if( status != PSA_SUCCESS )
5074 goto cleanup;
5075
Janos Follath7742fee2019-06-17 12:58:10 +01005076
5077cleanup:
5078
Janos Follathea29bfb2019-06-19 12:21:20 +01005079 cleanup_status = psa_hash_abort( &backup );
5080 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5081 status = cleanup_status;
5082
5083 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005084}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005085
Janos Follath844eb0e2019-06-19 12:10:49 +01005086static psa_status_t psa_key_derivation_tls12_prf_read(
5087 psa_tls12_prf_key_derivation_t *tls12_prf,
5088 psa_algorithm_t alg,
5089 uint8_t *output,
5090 size_t output_length )
5091{
5092 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005093 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005094 psa_status_t status;
5095 uint8_t offset, length;
5096
5097 while( output_length != 0 )
5098 {
5099 /* Check if we have fully processed the current block. */
5100 if( tls12_prf->left_in_block == 0 )
5101 {
5102 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5103 alg );
5104 if( status != PSA_SUCCESS )
5105 return( status );
5106
5107 continue;
5108 }
5109
5110 if( tls12_prf->left_in_block > output_length )
5111 length = (uint8_t) output_length;
5112 else
5113 length = tls12_prf->left_in_block;
5114
5115 offset = hash_length - tls12_prf->left_in_block;
5116 memcpy( output, tls12_prf->output_block + offset, length );
5117 output += length;
5118 output_length -= length;
5119 tls12_prf->left_in_block -= length;
5120 }
5121
5122 return( PSA_SUCCESS );
5123}
John Durkop07cc04a2020-11-16 22:08:34 -08005124#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5125 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005126
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005127psa_status_t psa_key_derivation_output_bytes(
5128 psa_key_derivation_operation_t *operation,
5129 uint8_t *output,
5130 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005131{
5132 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005133 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005134
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005136 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005138 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005139 }
5140
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005141 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005142 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005144 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005145 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005146 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005147 goto exit;
5148 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005149 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005150 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005151 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005152 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005153 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5154 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005156 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005157 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005158 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005159 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005160
John Durkopd0321952020-10-29 21:37:36 -07005161#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005162 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005163 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005164 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005165 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005166 output, output_length );
5167 }
Janos Follathadbec812019-06-14 11:05:39 +01005168 else
John Durkop07cc04a2020-11-16 22:08:34 -08005169#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5170#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5171 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005172 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005173 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005174 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005175 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005176 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005177 output_length );
5178 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005179 else
John Durkop07cc04a2020-11-16 22:08:34 -08005180#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5181 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005182 {
5183 return( PSA_ERROR_BAD_STATE );
5184 }
5185
5186exit:
5187 if( status != PSA_SUCCESS )
5188 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005189 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 * This allows us to differentiate between exhausted operations and
5191 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5192 * operations. */
5193 psa_algorithm_t alg = operation->alg;
5194 psa_key_derivation_abort( operation );
5195 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005196 memset( output, '!', output_length );
5197 }
5198 return( status );
5199}
5200
Gilles Peskine08542d82018-07-19 17:05:42 +02005201#if defined(MBEDTLS_DES_C)
5202static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5203{
5204 if( data_size >= 8 )
5205 mbedtls_des_key_set_parity( data );
5206 if( data_size >= 16 )
5207 mbedtls_des_key_set_parity( data + 8 );
5208 if( data_size >= 24 )
5209 mbedtls_des_key_set_parity( data + 16 );
5210}
5211#endif /* MBEDTLS_DES_C */
5212
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005213static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005214 psa_key_slot_t *slot,
5215 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005217{
5218 uint8_t *data = NULL;
5219 size_t bytes = PSA_BITS_TO_BYTES( bits );
5220 psa_status_t status;
5221
Gilles Peskine8e338702019-07-30 20:06:31 +02005222 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005223 return( PSA_ERROR_INVALID_ARGUMENT );
5224 if( bits % 8 != 0 )
5225 return( PSA_ERROR_INVALID_ARGUMENT );
5226 data = mbedtls_calloc( 1, bytes );
5227 if( data == NULL )
5228 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5229
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005231 if( status != PSA_SUCCESS )
5232 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005233#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005234 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005235 psa_des_set_key_parity( data, bytes );
5236#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005237 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005238
5239exit:
5240 mbedtls_free( data );
5241 return( status );
5242}
5243
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005244psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005246 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005247{
5248 psa_status_t status;
5249 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005250 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005251
Ronald Cron81709fc2020-11-14 12:10:32 +01005252 *key = MBEDTLS_SVC_KEY_ID_INIT;
5253
Gilles Peskine0f84d622019-09-12 19:03:13 +02005254 /* Reject any attempt to create a zero-length key so that we don't
5255 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5256 if( psa_get_key_bits( attributes ) == 0 )
5257 return( PSA_ERROR_INVALID_ARGUMENT );
5258
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005259 if( ! operation->can_output_key )
5260 return( PSA_ERROR_NOT_PERMITTED );
5261
Ronald Cron81709fc2020-11-14 12:10:32 +01005262 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5263 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005264#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5265 if( driver != NULL )
5266 {
5267 /* Deriving a key in a secure element is not implemented yet. */
5268 status = PSA_ERROR_NOT_SUPPORTED;
5269 }
5270#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005271 if( status == PSA_SUCCESS )
5272 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005273 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005274 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005276 }
5277 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005278 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005279 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005280 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005281
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005282 return( status );
5283}
5284
Gilles Peskineeab56e42018-07-12 17:12:33 +02005285
5286
5287/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005288/* Key derivation */
5289/****************************************************************/
5290
John Durkop07cc04a2020-11-16 22:08:34 -08005291#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005292static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005294 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005295{
John Durkop07cc04a2020-11-16 22:08:34 -08005296 int is_kdf_alg_supported;
5297
Janos Follath5fe19732019-06-20 15:09:30 +01005298 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5299 * initialisers for this union leave some bytes unspecified.) */
5300 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5301
Gilles Peskine969c5d62019-01-16 15:53:06 +01005302 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005303#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005304 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5305 is_kdf_alg_supported = 1;
5306 else
5307#endif
5308#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5309 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5310 is_kdf_alg_supported = 1;
5311 else
5312#endif
5313#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5314 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5315 is_kdf_alg_supported = 1;
5316 else
5317#endif
5318 is_kdf_alg_supported = 0;
5319
5320 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005322 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005323 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005324 if( hash_size == 0 )
5325 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005326 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5327 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005328 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005329 {
5330 return( PSA_ERROR_NOT_SUPPORTED );
5331 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005332 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005333 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005334 }
John Durkop07cc04a2020-11-16 22:08:34 -08005335
5336 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005337}
John Durkop07cc04a2020-11-16 22:08:34 -08005338#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005339
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005340psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005341 psa_algorithm_t alg )
5342{
5343 psa_status_t status;
5344
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005346 return( PSA_ERROR_BAD_STATE );
5347
Gilles Peskine6843c292019-01-18 16:44:49 +01005348 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5349 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005350#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005351 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005352 {
5353 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005354 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005355 }
5356 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5357 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005359 }
John Durkop07cc04a2020-11-16 22:08:34 -08005360#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005361 else
5362 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005363
5364 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005365 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005366 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005367}
5368
John Durkopd0321952020-10-29 21:37:36 -07005369#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005370static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005371 psa_algorithm_t hash_alg,
5372 psa_key_derivation_step_t step,
5373 const uint8_t *data,
5374 size_t data_length )
5375{
5376 psa_status_t status;
5377 switch( step )
5378 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005379 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005380 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005381 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005382 status = psa_hmac_setup_internal( &hkdf->hmac,
5383 data, data_length,
5384 hash_alg );
5385 if( status != PSA_SUCCESS )
5386 return( status );
5387 hkdf->state = HKDF_STATE_STARTED;
5388 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005389 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005390 /* If no salt was provided, use an empty salt. */
5391 if( hkdf->state == HKDF_STATE_INIT )
5392 {
5393 status = psa_hmac_setup_internal( &hkdf->hmac,
5394 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005395 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005396 if( status != PSA_SUCCESS )
5397 return( status );
5398 hkdf->state = HKDF_STATE_STARTED;
5399 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005400 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005401 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005402 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5403 data, data_length );
5404 if( status != PSA_SUCCESS )
5405 return( status );
5406 status = psa_hmac_finish_internal( &hkdf->hmac,
5407 hkdf->prk,
5408 sizeof( hkdf->prk ) );
5409 if( status != PSA_SUCCESS )
5410 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005411 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005412 hkdf->block_number = 0;
5413 hkdf->state = HKDF_STATE_KEYED;
5414 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005415 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005416 if( hkdf->state == HKDF_STATE_OUTPUT )
5417 return( PSA_ERROR_BAD_STATE );
5418 if( hkdf->info_set )
5419 return( PSA_ERROR_BAD_STATE );
5420 hkdf->info_length = data_length;
5421 if( data_length != 0 )
5422 {
5423 hkdf->info = mbedtls_calloc( 1, data_length );
5424 if( hkdf->info == NULL )
5425 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5426 memcpy( hkdf->info, data, data_length );
5427 }
5428 hkdf->info_set = 1;
5429 return( PSA_SUCCESS );
5430 default:
5431 return( PSA_ERROR_INVALID_ARGUMENT );
5432 }
5433}
John Durkop07cc04a2020-11-16 22:08:34 -08005434#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005435
John Durkop07cc04a2020-11-16 22:08:34 -08005436#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5437 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005438static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5439 const uint8_t *data,
5440 size_t data_length )
5441{
5442 if( prf->state != TLS12_PRF_STATE_INIT )
5443 return( PSA_ERROR_BAD_STATE );
5444
Janos Follathd6dce9f2019-07-04 09:11:38 +01005445 if( data_length != 0 )
5446 {
5447 prf->seed = mbedtls_calloc( 1, data_length );
5448 if( prf->seed == NULL )
5449 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005450
Janos Follathd6dce9f2019-07-04 09:11:38 +01005451 memcpy( prf->seed, data, data_length );
5452 prf->seed_length = data_length;
5453 }
Janos Follathf08e2652019-06-13 09:05:41 +01005454
5455 prf->state = TLS12_PRF_STATE_SEED_SET;
5456
5457 return( PSA_SUCCESS );
5458}
5459
Janos Follath81550542019-06-13 14:26:34 +01005460static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5461 psa_algorithm_t hash_alg,
5462 const uint8_t *data,
5463 size_t data_length )
5464{
5465 psa_status_t status;
5466 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5467 return( PSA_ERROR_BAD_STATE );
5468
5469 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5470 if( status != PSA_SUCCESS )
5471 return( status );
5472
5473 prf->state = TLS12_PRF_STATE_KEY_SET;
5474
5475 return( PSA_SUCCESS );
5476}
5477
Janos Follath63028dd2019-06-13 09:15:47 +01005478static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5479 const uint8_t *data,
5480 size_t data_length )
5481{
5482 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5483 return( PSA_ERROR_BAD_STATE );
5484
Janos Follathd6dce9f2019-07-04 09:11:38 +01005485 if( data_length != 0 )
5486 {
5487 prf->label = mbedtls_calloc( 1, data_length );
5488 if( prf->label == NULL )
5489 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005490
Janos Follathd6dce9f2019-07-04 09:11:38 +01005491 memcpy( prf->label, data, data_length );
5492 prf->label_length = data_length;
5493 }
Janos Follath63028dd2019-06-13 09:15:47 +01005494
5495 prf->state = TLS12_PRF_STATE_LABEL_SET;
5496
5497 return( PSA_SUCCESS );
5498}
5499
Janos Follathb03233e2019-06-11 15:30:30 +01005500static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5501 psa_algorithm_t hash_alg,
5502 psa_key_derivation_step_t step,
5503 const uint8_t *data,
5504 size_t data_length )
5505{
Janos Follathb03233e2019-06-11 15:30:30 +01005506 switch( step )
5507 {
Janos Follathf08e2652019-06-13 09:05:41 +01005508 case PSA_KEY_DERIVATION_INPUT_SEED:
5509 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005510 case PSA_KEY_DERIVATION_INPUT_SECRET:
5511 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005512 case PSA_KEY_DERIVATION_INPUT_LABEL:
5513 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005514 default:
5515 return( PSA_ERROR_INVALID_ARGUMENT );
5516 }
5517}
John Durkop07cc04a2020-11-16 22:08:34 -08005518#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5519 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5520
5521#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5522static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5523 psa_tls12_prf_key_derivation_t *prf,
5524 psa_algorithm_t hash_alg,
5525 const uint8_t *data,
5526 size_t data_length )
5527{
5528 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005529 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005530 uint8_t *cur = pms;
5531
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005532 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005533 return( PSA_ERROR_INVALID_ARGUMENT );
5534
5535 /* Quoting RFC 4279, Section 2:
5536 *
5537 * The premaster secret is formed as follows: if the PSK is N octets
5538 * long, concatenate a uint16 with the value N, N zero octets, a second
5539 * uint16 with the value N, and the PSK itself.
5540 */
5541
5542 *cur++ = ( data_length >> 8 ) & 0xff;
5543 *cur++ = ( data_length >> 0 ) & 0xff;
5544 memset( cur, 0, data_length );
5545 cur += data_length;
5546 *cur++ = pms[0];
5547 *cur++ = pms[1];
5548 memcpy( cur, data, data_length );
5549 cur += data_length;
5550
5551 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5552
5553 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5554 return( status );
5555}
Janos Follath6660f0e2019-06-17 08:44:03 +01005556
5557static psa_status_t psa_tls12_prf_psk_to_ms_input(
5558 psa_tls12_prf_key_derivation_t *prf,
5559 psa_algorithm_t hash_alg,
5560 psa_key_derivation_step_t step,
5561 const uint8_t *data,
5562 size_t data_length )
5563{
5564 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005565 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005566 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5567 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005568 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005569
5570 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5571}
John Durkop07cc04a2020-11-16 22:08:34 -08005572#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005573
Gilles Peskineb8965192019-09-24 16:21:10 +02005574/** Check whether the given key type is acceptable for the given
5575 * input step of a key derivation.
5576 *
5577 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5578 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5579 * Both secret and non-secret inputs can alternatively have the type
5580 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5581 * that the input was passed as a buffer rather than via a key object.
5582 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005583static int psa_key_derivation_check_input_type(
5584 psa_key_derivation_step_t step,
5585 psa_key_type_t key_type )
5586{
5587 switch( step )
5588 {
5589 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005590 if( key_type == PSA_KEY_TYPE_DERIVE )
5591 return( PSA_SUCCESS );
5592 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005593 return( PSA_SUCCESS );
5594 break;
5595 case PSA_KEY_DERIVATION_INPUT_LABEL:
5596 case PSA_KEY_DERIVATION_INPUT_SALT:
5597 case PSA_KEY_DERIVATION_INPUT_INFO:
5598 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005599 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5600 return( PSA_SUCCESS );
5601 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005602 return( PSA_SUCCESS );
5603 break;
5604 }
5605 return( PSA_ERROR_INVALID_ARGUMENT );
5606}
5607
Janos Follathb80a94e2019-06-12 15:54:46 +01005608static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005609 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005610 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005611 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005612 const uint8_t *data,
5613 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005614{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005615 psa_status_t status;
5616 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5617
5618 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005619 if( status != PSA_SUCCESS )
5620 goto exit;
5621
John Durkopd0321952020-10-29 21:37:36 -07005622#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005623 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005624 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005625 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005626 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005627 step, data, data_length );
5628 }
John Durkop07cc04a2020-11-16 22:08:34 -08005629 else
5630#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5631#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5632 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005633 {
Janos Follathb03233e2019-06-11 15:30:30 +01005634 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5635 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5636 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005637 }
John Durkop07cc04a2020-11-16 22:08:34 -08005638 else
5639#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5640#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5641 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005642 {
5643 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5644 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5645 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005646 }
5647 else
John Durkop07cc04a2020-11-16 22:08:34 -08005648#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005649 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005650 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005651 return( PSA_ERROR_BAD_STATE );
5652 }
5653
Gilles Peskine224b0d62019-09-23 18:13:17 +02005654exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005655 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005656 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005657 return( status );
5658}
5659
Janos Follath51f4a0f2019-06-14 11:35:55 +01005660psa_status_t psa_key_derivation_input_bytes(
5661 psa_key_derivation_operation_t *operation,
5662 psa_key_derivation_step_t step,
5663 const uint8_t *data,
5664 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005665{
Gilles Peskineb8965192019-09-24 16:21:10 +02005666 return( psa_key_derivation_input_internal( operation, step,
5667 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005668 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005669}
5670
Janos Follath51f4a0f2019-06-14 11:35:55 +01005671psa_status_t psa_key_derivation_input_key(
5672 psa_key_derivation_operation_t *operation,
5673 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005674 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005675{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005677 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005678 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005679
Ronald Cron5c522922020-11-14 16:35:34 +01005680 status = psa_get_and_lock_transparent_key_slot_with_policy(
5681 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005682 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005683 {
5684 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005685 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005686 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005687
5688 /* Passing a key object as a SECRET input unlocks the permission
5689 * to output to a key object. */
5690 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5691 operation->can_output_key = 1;
5692
Ronald Cronf95a2b12020-10-22 15:24:49 +02005693 status = psa_key_derivation_input_internal( operation,
5694 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005695 slot->key.data,
5696 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005697
Ronald Cron5c522922020-11-14 16:35:34 +01005698 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005699
Ronald Cron5c522922020-11-14 16:35:34 +01005700 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005701}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005702
5703
5704
5705/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005706/* Key agreement */
5707/****************************************************************/
5708
John Durkop6ba40d12020-11-10 08:50:04 -08005709#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005710static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5711 size_t peer_key_length,
5712 const mbedtls_ecp_keypair *our_key,
5713 uint8_t *shared_secret,
5714 size_t shared_secret_size,
5715 size_t *shared_secret_length )
5716{
Steven Cooremand4867872020-08-05 16:31:39 +02005717 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005718 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005719 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005720 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005721 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005722 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005723
Ronald Cron90857082020-11-25 14:58:33 +01005724 status = mbedtls_psa_ecp_load_representation(
5725 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5726 peer_key,
5727 peer_key_length,
5728 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005729 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005730 goto exit;
5731
Jaeden Amero97271b32019-01-10 19:38:51 +00005732 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005733 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005734 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005735 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005736 status = mbedtls_to_psa_error(
5737 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5738 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005739 goto exit;
5740
Jaeden Amero97271b32019-01-10 19:38:51 +00005741 status = mbedtls_to_psa_error(
5742 mbedtls_ecdh_calc_secret( &ecdh,
5743 shared_secret_length,
5744 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005745 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005746 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005747 if( status != PSA_SUCCESS )
5748 goto exit;
5749 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5750 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005751
5752exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005753 if( status != PSA_SUCCESS )
5754 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005755 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005756 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005757 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005758
Jaeden Amero97271b32019-01-10 19:38:51 +00005759 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005760}
John Durkop6ba40d12020-11-10 08:50:04 -08005761#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005762
Gilles Peskine01d718c2018-09-18 12:01:02 +02005763#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5764
Gilles Peskine0216fe12019-04-11 21:23:21 +02005765static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5766 psa_key_slot_t *private_key,
5767 const uint8_t *peer_key,
5768 size_t peer_key_length,
5769 uint8_t *shared_secret,
5770 size_t shared_secret_size,
5771 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005772{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005773 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005774 {
John Durkop6ba40d12020-11-10 08:50:04 -08005775#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005776 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005777 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005778 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005779 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005780 psa_status_t status = mbedtls_psa_ecp_load_representation(
5781 private_key->attr.type,
5782 private_key->key.data,
5783 private_key->key.bytes,
5784 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005785 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005786 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005787 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005788 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005789 shared_secret, shared_secret_size,
5790 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005791 mbedtls_ecp_keypair_free( ecp );
5792 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005793 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005794#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005795 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005796 (void) private_key;
5797 (void) peer_key;
5798 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005799 (void) shared_secret;
5800 (void) shared_secret_size;
5801 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005802 return( PSA_ERROR_NOT_SUPPORTED );
5803 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005804}
5805
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005806/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005807 * to potentially free embedded data structures and wipe confidential data.
5808 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005809static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005810 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005811 psa_key_slot_t *private_key,
5812 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005813 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005814{
5815 psa_status_t status;
5816 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5817 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005818 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005819
5820 /* Step 1: run the secret agreement algorithm to generate the shared
5821 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005822 status = psa_key_agreement_raw_internal( ka_alg,
5823 private_key,
5824 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005825 shared_secret,
5826 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005827 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005828 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005829 goto exit;
5830
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005831 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005832 * the shared secret. A shared secret is permitted wherever a key
5833 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005834 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005835 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005836 shared_secret,
5837 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005838exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005839 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005840 return( status );
5841}
5842
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005843psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005844 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005845 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005846 const uint8_t *peer_key,
5847 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005848{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005850 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005851 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005852
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005853 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005854 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005855 status = psa_get_and_lock_transparent_key_slot_with_policy(
5856 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005857 if( status != PSA_SUCCESS )
5858 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005859 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005860 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005861 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005862 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005863 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005864 else
5865 {
5866 /* If a private key has been added as SECRET, we allow the derived
5867 * key material to be used as a key in PSA Crypto. */
5868 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5869 operation->can_output_key = 1;
5870 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005871
Ronald Cron5c522922020-11-14 16:35:34 +01005872 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005873
Ronald Cron5c522922020-11-14 16:35:34 +01005874 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005875}
5876
Gilles Peskinebe697d82019-05-16 18:00:41 +02005877psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005878 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005879 const uint8_t *peer_key,
5880 size_t peer_key_length,
5881 uint8_t *output,
5882 size_t output_size,
5883 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005884{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005885 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005886 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005887 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005888
5889 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5890 {
5891 status = PSA_ERROR_INVALID_ARGUMENT;
5892 goto exit;
5893 }
Ronald Cron5c522922020-11-14 16:35:34 +01005894 status = psa_get_and_lock_transparent_key_slot_with_policy(
5895 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005896 if( status != PSA_SUCCESS )
5897 goto exit;
5898
5899 status = psa_key_agreement_raw_internal( alg, slot,
5900 peer_key, peer_key_length,
5901 output, output_size,
5902 output_length );
5903
5904exit:
5905 if( status != PSA_SUCCESS )
5906 {
5907 /* If an error happens and is not handled properly, the output
5908 * may be used as a key to protect sensitive data. Arrange for such
5909 * a key to be random, which is likely to result in decryption or
5910 * verification errors. This is better than filling the buffer with
5911 * some constant data such as zeros, which would result in the data
5912 * being protected with a reproducible, easily knowable key.
5913 */
5914 psa_generate_random( output, output_size );
5915 *output_length = output_size;
5916 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005917
Ronald Cron5c522922020-11-14 16:35:34 +01005918 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005919
Ronald Cron5c522922020-11-14 16:35:34 +01005920 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005921}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005922
5923
Gilles Peskine30524eb2020-11-13 17:02:26 +01005924
Gilles Peskine86a440b2018-11-12 18:39:40 +01005925/****************************************************************/
5926/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005927/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005928
Gilles Peskine30524eb2020-11-13 17:02:26 +01005929/** Initialize the PSA random generator.
5930 */
5931static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5932{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005933#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5934 memset( rng, 0, sizeof( *rng ) );
5935#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5936
Gilles Peskine30524eb2020-11-13 17:02:26 +01005937 /* Set default configuration if
5938 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5939 if( rng->entropy_init == NULL )
5940 rng->entropy_init = mbedtls_entropy_init;
5941 if( rng->entropy_free == NULL )
5942 rng->entropy_free = mbedtls_entropy_free;
5943
5944 rng->entropy_init( &rng->entropy );
5945#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5946 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5947 /* The PSA entropy injection feature depends on using NV seed as an entropy
5948 * source. Add NV seed as an entropy source for PSA entropy injection. */
5949 mbedtls_entropy_add_source( &rng->entropy,
5950 mbedtls_nv_seed_poll, NULL,
5951 MBEDTLS_ENTROPY_BLOCK_SIZE,
5952 MBEDTLS_ENTROPY_SOURCE_STRONG );
5953#endif
5954
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005955 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005956#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005957}
5958
5959/** Deinitialize the PSA random generator.
5960 */
5961static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5962{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005963#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5964 memset( rng, 0, sizeof( *rng ) );
5965#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005966 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005967 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005968#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005969}
5970
5971/** Seed the PSA random generator.
5972 */
5973static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5974{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005975#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5976 /* Do nothing: the external RNG seeds itself. */
5977 (void) rng;
5978 return( PSA_SUCCESS );
5979#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005980 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005981 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5982 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005983 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005984#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005985}
5986
Gilles Peskinee59236f2018-01-27 23:32:46 +01005987psa_status_t psa_generate_random( uint8_t *output,
5988 size_t output_size )
5989{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005990 GUARD_MODULE_INITIALIZED;
5991
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005992#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5993
5994 size_t output_length = 0;
5995 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5996 output, output_size,
5997 &output_length );
5998 if( status != PSA_SUCCESS )
5999 return( status );
6000 /* Breaking up a request into smaller chunks is currently not supported
6001 * for the extrernal RNG interface. */
6002 if( output_length != output_size )
6003 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6004 return( PSA_SUCCESS );
6005
6006#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6007
Gilles Peskine71ddab92021-01-04 21:01:07 +01006008 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006009 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006010 size_t request_size =
6011 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6012 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6013 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006014 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6015 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006016 if( ret != 0 )
6017 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006018 output_size -= request_size;
6019 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006020 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006021 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006022#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006023}
6024
Gilles Peskine8814fc42020-12-14 15:33:44 +01006025/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006026 *
6027 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6028 * `psa_generate_random(...)`. The state parameter is ignored since the
6029 * PSA API doesn't support passing an explicit state.
6030 *
6031 * In the non-external case, psa_generate_random() calls an
6032 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6033 * and semantics as mbedtls_psa_get_random(). As an optimization,
6034 * instead of doing this back-and-forth between the PSA API and the
6035 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6036 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006037 */
6038#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6039int mbedtls_psa_get_random( void *p_rng,
6040 unsigned char *output,
6041 size_t output_size )
6042{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006043 /* This function takes a pointer to the RNG state because that's what
6044 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6045 * its own state internally and doesn't let the caller access that state.
6046 * So we just ignore the state parameter, and in practice we'll pass
6047 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006048 (void) p_rng;
6049 psa_status_t status = psa_generate_random( output, output_size );
6050 if( status == PSA_SUCCESS )
6051 return( 0 );
6052 else
6053 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6054}
6055#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6056
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006057#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6058#include "mbedtls/entropy_poll.h"
6059
Gilles Peskine7228da22019-07-15 11:06:15 +02006060psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006061 size_t seed_size )
6062{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006063 if( global_data.initialized )
6064 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006065
6066 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6067 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6068 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6069 return( PSA_ERROR_INVALID_ARGUMENT );
6070
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006071 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006072}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006073#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006074
John Durkop07cc04a2020-11-16 22:08:34 -08006075#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006076static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6077 size_t domain_parameters_size,
6078 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006079{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006080 size_t i;
6081 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006082
Gilles Peskinee56e8782019-04-26 17:34:02 +02006083 if( domain_parameters_size == 0 )
6084 {
6085 *exponent = 65537;
6086 return( PSA_SUCCESS );
6087 }
6088
6089 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6090 * support values that fit in a 32-bit integer, which is larger than
6091 * int on just about every platform anyway. */
6092 if( domain_parameters_size > sizeof( acc ) )
6093 return( PSA_ERROR_NOT_SUPPORTED );
6094 for( i = 0; i < domain_parameters_size; i++ )
6095 acc = ( acc << 8 ) | domain_parameters[i];
6096 if( acc > INT_MAX )
6097 return( PSA_ERROR_NOT_SUPPORTED );
6098 *exponent = acc;
6099 return( PSA_SUCCESS );
6100}
John Durkop07cc04a2020-11-16 22:08:34 -08006101#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006102
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006103static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006104 psa_key_slot_t *slot, size_t bits,
6105 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006106{
Gilles Peskine8e338702019-07-30 20:06:31 +02006107 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006108
Gilles Peskinee56e8782019-04-26 17:34:02 +02006109 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006110 return( PSA_ERROR_INVALID_ARGUMENT );
6111
Gilles Peskinee59236f2018-01-27 23:32:46 +01006112 if( key_type_is_raw_bytes( type ) )
6113 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006114 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006115
6116 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006117 if( status != PSA_SUCCESS )
6118 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006119
6120 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006121 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6122 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006123 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006124
Ronald Cronea0f8a62020-11-25 17:52:23 +01006125 status = psa_generate_random( slot->key.data,
6126 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006127 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006128 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006129
6130 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006131#if defined(MBEDTLS_DES_C)
6132 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006133 psa_des_set_key_parity( slot->key.data,
6134 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006135#endif /* MBEDTLS_DES_C */
6136 }
6137 else
6138
John Durkop07cc04a2020-11-16 22:08:34 -08006139#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006140 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006141 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006142 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006144 int exponent;
6145 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006146 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6147 return( PSA_ERROR_NOT_SUPPORTED );
6148 /* Accept only byte-aligned keys, for the same reasons as
Ronald Cronb6420e32020-11-22 16:15:38 +01006149 * in mbedtls_psa_rsa_import_key(). */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006150 if( bits % 8 != 0 )
6151 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006152 status = psa_read_rsa_exponent( domain_parameters,
6153 domain_parameters_size,
6154 &exponent );
6155 if( status != PSA_SUCCESS )
6156 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006157 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6158 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006159 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006160 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006161 (unsigned int) bits,
6162 exponent );
6163 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006164 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006165
6166 /* Make sure to always have an export representation available */
6167 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6168
Steven Cooreman75b74362020-07-28 14:30:13 +02006169 status = psa_allocate_buffer_to_slot( slot, bytes );
6170 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006171 {
6172 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006173 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006174 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006175
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01006176 status = mbedtls_psa_rsa_export_key( type,
6177 &rsa,
6178 slot->key.data,
6179 bytes,
6180 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006181 mbedtls_rsa_free( &rsa );
6182 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006183 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006184 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006185 }
6186 else
John Durkop07cc04a2020-11-16 22:08:34 -08006187#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006188
John Durkop9814fa22020-11-04 12:28:15 -08006189#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006190 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006191 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006192 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006193 mbedtls_ecp_group_id grp_id =
6194 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006195 const mbedtls_ecp_curve_info *curve_info =
6196 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006197 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006199 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006200 return( PSA_ERROR_NOT_SUPPORTED );
6201 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6202 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006203 mbedtls_ecp_keypair_init( &ecp );
6204 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006205 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006206 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006207 if( ret != 0 )
6208 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006209 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006210 return( mbedtls_to_psa_error( ret ) );
6211 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006212
6213
6214 /* Make sure to always have an export representation available */
6215 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006216 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6217 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006218 {
6219 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006220 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006221 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006222
6223 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006224 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006225
6226 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006227 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006228 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006229 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006230 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006231 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006232 }
6233 else
John Durkop9814fa22020-11-04 12:28:15 -08006234#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006235 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006236 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006237 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006238
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006239 return( PSA_SUCCESS );
6240}
Darryl Green0c6575a2018-11-07 16:05:30 +00006241
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006242psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006243 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006244{
6245 psa_status_t status;
6246 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006247 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006248
Ronald Cron81709fc2020-11-14 12:10:32 +01006249 *key = MBEDTLS_SVC_KEY_ID_INIT;
6250
Gilles Peskine0f84d622019-09-12 19:03:13 +02006251 /* Reject any attempt to create a zero-length key so that we don't
6252 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6253 if( psa_get_key_bits( attributes ) == 0 )
6254 return( PSA_ERROR_INVALID_ARGUMENT );
6255
Ronald Cron81709fc2020-11-14 12:10:32 +01006256 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6257 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006258 if( status != PSA_SUCCESS )
6259 goto exit;
6260
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006261 status = psa_driver_wrapper_generate_key( attributes,
6262 slot );
6263 if( status != PSA_ERROR_NOT_SUPPORTED ||
6264 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6265 goto exit;
6266
6267 status = psa_generate_key_internal(
6268 slot, attributes->core.bits,
6269 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006270
6271exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006272 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006273 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006274 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006275 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006276
Darryl Green0c6575a2018-11-07 16:05:30 +00006277 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006278}
6279
6280
Gilles Peskinee59236f2018-01-27 23:32:46 +01006281
6282/****************************************************************/
6283/* Module setup */
6284/****************************************************************/
6285
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006286#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006287psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6288 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6289 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6290{
6291 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6292 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006293 global_data.rng.entropy_init = entropy_init;
6294 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006295 return( PSA_SUCCESS );
6296}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006297#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006298
Gilles Peskinee59236f2018-01-27 23:32:46 +01006299void mbedtls_psa_crypto_free( void )
6300{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006301 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006302 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6303 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006304 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006305 }
6306 /* Wipe all remaining data, including configuration.
6307 * In particular, this sets all state indicator to the value
6308 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006309 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006311 /* Unregister all secure element drivers, so that we restart from
6312 * a pristine state. */
6313 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006314#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006315}
6316
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006317#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6318/** Recover a transaction that was interrupted by a power failure.
6319 *
6320 * This function is called during initialization, before psa_crypto_init()
6321 * returns. If this function returns a failure status, the initialization
6322 * fails.
6323 */
6324static psa_status_t psa_crypto_recover_transaction(
6325 const psa_crypto_transaction_t *transaction )
6326{
6327 switch( transaction->unknown.type )
6328 {
6329 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6330 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006331 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006332 * is implemented.
6333 * https://github.com/ARMmbed/mbed-crypto/issues/218
6334 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006335 default:
6336 /* We found an unsupported transaction in the storage.
6337 * We don't know what state the storage is in. Give up. */
6338 return( PSA_ERROR_STORAGE_FAILURE );
6339 }
6340}
6341#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6342
Gilles Peskinee59236f2018-01-27 23:32:46 +01006343psa_status_t psa_crypto_init( void )
6344{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006345 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006346
Gilles Peskinec6b69072018-11-20 21:42:52 +01006347 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006348 if( global_data.initialized != 0 )
6349 return( PSA_SUCCESS );
6350
Gilles Peskine30524eb2020-11-13 17:02:26 +01006351 /* Initialize and seed the random generator. */
6352 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006353 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006354 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006355 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006356 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006357 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006358
Gilles Peskine66fb1262018-12-10 16:29:04 +01006359 status = psa_initialize_key_slots( );
6360 if( status != PSA_SUCCESS )
6361 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006362
Gilles Peskined9348f22019-10-01 15:22:29 +02006363#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6364 status = psa_init_all_se_drivers( );
6365 if( status != PSA_SUCCESS )
6366 goto exit;
6367#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6368
Gilles Peskine4b734222019-07-24 15:56:31 +02006369#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006370 status = psa_crypto_load_transaction( );
6371 if( status == PSA_SUCCESS )
6372 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006373 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6374 if( status != PSA_SUCCESS )
6375 goto exit;
6376 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006377 }
6378 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6379 {
6380 /* There's no transaction to complete. It's all good. */
6381 status = PSA_SUCCESS;
6382 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006383#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006384
Gilles Peskinec6b69072018-11-20 21:42:52 +01006385 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006386 global_data.initialized = 1;
6387
6388exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006389 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006390 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006391 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006392}
6393
6394#endif /* MBEDTLS_PSA_CRYPTO_C */