blob: 960c69824dc8cd35ec036c692abffafb12edf666 [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
John Durkop6ba40d12020-11-10 08:50:04 -0800536#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
537 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
538
Steven Cooremana01795d2020-07-24 22:48:15 +0200539/** Import an RSA key from import representation to a slot
540 *
541 * \param[in,out] slot The slot where to store the export representation to
542 * \param[in] data The buffer containing the import representation
543 * \param[in] data_length The amount of bytes in \p data
Ronald Cron8f813ee2020-11-07 18:22:09 +0100544 * \param[out] key_buffer The buffer containing the export representation
545 * \param[in] key_buffer_size The size of \p key_buffer in bytes
546 * \param[out] key_buffer_length The length of the data written in the key
547 * buffer in bytes.
Steven Cooremana01795d2020-07-24 22:48:15 +0200548 */
549static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
550 const uint8_t *data,
Ronald Cron8f813ee2020-11-07 18:22:09 +0100551 size_t data_length,
552 uint8_t *key_buffer,
553 size_t key_buffer_size,
554 size_t *key_buffer_length )
Steven Cooremana01795d2020-07-24 22:48:15 +0200555{
556 psa_status_t status;
Steven Cooremana2371e52020-07-28 14:30:39 +0200557 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200558
Steven Cooremana01795d2020-07-24 22:48:15 +0200559 /* Parse input */
Ronald Cron90857082020-11-25 14:58:33 +0100560 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
561 data,
562 data_length,
563 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200564 if( status != PSA_SUCCESS )
565 goto exit;
566
567 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200568 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200569
Steven Cooreman75b74362020-07-28 14:30:13 +0200570 /* Re-export the data to PSA export format, such that we can store export
571 * representation in the key slot. Export representation in case of RSA is
572 * the smallest representation that's allowed as input, so a straight-up
573 * allocation of the same size as the input buffer will be large enough. */
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100574 status = mbedtls_psa_rsa_export_key( slot->attr.type,
575 rsa,
Ronald Cron8f813ee2020-11-07 18:22:09 +0100576 key_buffer,
577 key_buffer_size,
578 key_buffer_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200579exit:
580 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200581 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200582 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200583
Ronald Cron8f813ee2020-11-07 18:22:09 +0100584 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200585}
John Durkop6ba40d12020-11-10 08:50:04 -0800586
587#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800588 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589
John Durkop9814fa22020-11-04 12:28:15 -0800590#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800591 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200592
Steven Cooreman4fed4552020-08-03 14:46:03 +0200593/** Import an ECP key from import representation to a slot
594 *
595 * \param[in,out] slot The slot where to store the export representation to
596 * \param[in] data The buffer containing the import representation
597 * \param[in] data_length The amount of bytes in \p data
598 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200599static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
600 const uint8_t *data,
601 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100602{
Steven Cooremanacda8342020-07-24 23:09:52 +0200603 psa_status_t status;
604 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200605 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100606
Steven Cooremanacda8342020-07-24 23:09:52 +0200607 /* Parse input */
Ronald Cron90857082020-11-25 14:58:33 +0100608 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
609 data,
610 data_length,
611 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100612 if( status != PSA_SUCCESS )
613 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100614
Steven Cooremanacda8342020-07-24 23:09:52 +0200615 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200616 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200617 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200618 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200619
Steven Cooremanacda8342020-07-24 23:09:52 +0200620 /* Re-export the data to PSA export format. There is currently no support
621 * for other input formats then the export format, so this is a 1-1
622 * copy operation. */
623 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200624 if( output == NULL )
625 {
626 status = PSA_ERROR_INSUFFICIENT_MEMORY;
627 goto exit;
628 }
629
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100630 status = mbedtls_psa_ecp_export_key( slot->attr.type,
631 ecp,
632 output,
633 data_length,
634 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100635exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200636 /* Always free the PK object (will also free contained ECP context) */
637 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200638 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200639
640 /* Free the allocated buffer only on error. */
641 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100642 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200643 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200644 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100645 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200646
647 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100648 slot->key.data = output;
649 slot->key.bytes = data_length;
Steven Cooremanacda8342020-07-24 23:09:52 +0200650
651 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652}
John Durkop9814fa22020-11-04 12:28:15 -0800653#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
654 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100655
Gilles Peskineb46bef22019-07-30 21:32:04 +0200656/** Return the size of the key in the given slot, in bits.
657 *
658 * \param[in] slot A key slot.
659 *
660 * \return The key size in bits, read from the metadata in the slot.
661 */
662static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
663{
664 return( slot->attr.bits );
665}
666
Steven Cooreman75b74362020-07-28 14:30:13 +0200667/** Try to allocate a buffer to an empty key slot.
668 *
669 * \param[in,out] slot Key slot to attach buffer to.
670 * \param[in] buffer_length Requested size of the buffer.
671 *
672 * \retval #PSA_SUCCESS
673 * The buffer has been successfully allocated.
674 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
675 * Not enough memory was available for allocation.
676 * \retval #PSA_ERROR_ALREADY_EXISTS
677 * Trying to allocate a buffer to a non-empty key slot.
678 */
679static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
680 size_t buffer_length )
681{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100682 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200683 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200684
Ronald Cronea0f8a62020-11-25 17:52:23 +0100685 slot->key.data = mbedtls_calloc( 1, buffer_length );
686 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200687 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200688
Ronald Cronea0f8a62020-11-25 17:52:23 +0100689 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200690 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200691}
692
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200693psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
694 const uint8_t* data,
695 size_t data_length )
696{
697 psa_status_t status = psa_allocate_buffer_to_slot( slot,
698 data_length );
699 if( status != PSA_SUCCESS )
700 return( status );
701
Ronald Cronea0f8a62020-11-25 17:52:23 +0100702 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200703 return( PSA_SUCCESS );
704}
705
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200706/** Import key data into a slot.
707 *
708 * `slot->type` must have been set previously.
709 * This function assumes that the slot does not contain any key material yet.
710 * On failure, the slot content is unchanged.
711 *
712 * Persistent storage is not affected.
713 *
714 * \param[in,out] slot The key slot to import data into.
715 * Its `type` field must have previously been set to
716 * the desired key type.
717 * It must not contain any key material yet.
718 * \param[in] data Buffer containing the key material to parse and import.
719 * \param data_length Size of \p data in bytes.
720 *
721 * \retval #PSA_SUCCESS
722 * \retval #PSA_ERROR_INVALID_ARGUMENT
723 * \retval #PSA_ERROR_NOT_SUPPORTED
724 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
725 */
726static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
727 const uint8_t *data,
728 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100729{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200730 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +0200731 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100732
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200733 /* zero-length keys are never supported. */
734 if( data_length == 0 )
735 return( PSA_ERROR_NOT_SUPPORTED );
736
Gilles Peskine8e338702019-07-30 20:06:31 +0200737 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 {
Steven Cooreman04524762020-10-13 17:43:44 +0200739 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200740
Steven Cooreman75b74362020-07-28 14:30:13 +0200741 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
742 if( data_length > SIZE_MAX / 8 )
743 return( PSA_ERROR_NOT_SUPPORTED );
744
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200745 /* Enforce a size limit, and in particular ensure that the bit
746 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200747 if( bit_size > PSA_MAX_KEY_BITS )
748 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200749
750 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200751 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200752 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200753
754 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200755 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +0200756 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200757 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200758
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200759 /* Write the actual key size to the slot.
760 * psa_start_key_creation() wrote the size declared by the
761 * caller, which may be 0 (meaning unspecified) or wrong. */
762 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +0100763
764 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200766 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200767 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200768 /* Try validation through accelerators first. */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200769 psa_key_attributes_t attributes = {
770 .core = slot->attr
771 };
Ronald Cron83282872020-11-22 14:02:39 +0100772
773 status = psa_allocate_buffer_to_slot( slot, data_length );
774 if( status != PSA_SUCCESS )
775 return( status );
776
777 bit_size = slot->attr.bits;
778 status = psa_driver_wrapper_import_key( &attributes,
779 data, data_length,
780 slot->key.data,
781 slot->key.bytes,
782 &slot->key.bytes,
783 &bit_size );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200784 if( status == PSA_SUCCESS )
785 {
Ronald Cron83282872020-11-22 14:02:39 +0100786 if( slot->attr.bits == 0 )
787 slot->attr.bits = (psa_key_bits_t) bit_size;
788 else if( bit_size != slot->attr.bits )
789 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200790
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200791 return( PSA_SUCCESS );
792 }
Ronald Cron83282872020-11-22 14:02:39 +0100793 else
794 {
795 if( status != PSA_ERROR_NOT_SUPPORTED )
796 return( status );
797 }
798
799 mbedtls_platform_zeroize( slot->key.data, data_length );
800 mbedtls_free( slot->key.data );
801 slot->key.data = NULL;
802 slot->key.bytes = 0;
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200803
804 /* Key format is not supported by any accelerator, try software fallback
805 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -0800806#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
807 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200808 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
809 {
Steven Cooreman40120f62020-10-29 11:42:22 +0100810 return( psa_import_ecp_key( slot, data, data_length ) );
811 }
John Durkop9814fa22020-11-04 12:28:15 -0800812#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
813 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700814#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
815 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +0100816 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200817 {
Ronald Cron8f813ee2020-11-07 18:22:09 +0100818 status = psa_allocate_buffer_to_slot( slot, data_length );
819 if( status != PSA_SUCCESS )
820 return( status );
821
822 status = psa_import_rsa_key( slot,
823 data, data_length,
824 slot->key.data, data_length,
825 &slot->key.bytes );
826 return( status );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200827 }
John Durkop9814fa22020-11-04 12:28:15 -0800828#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
829 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +0100830
831 /* Fell through the fallback as well, so have nothing else to try. */
832 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100833 }
834 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100835 {
Steven Cooreman19fd5742020-07-24 23:31:01 +0200836 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +0100838 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100839}
840
Gilles Peskinef603c712019-01-19 13:40:11 +0100841/** Calculate the intersection of two algorithm usage policies.
842 *
843 * Return 0 (which allows no operation) on incompatibility.
844 */
845static psa_algorithm_t psa_key_policy_algorithm_intersection(
846 psa_algorithm_t alg1,
847 psa_algorithm_t alg2 )
848{
Gilles Peskine549ea862019-05-22 11:45:59 +0200849 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100850 if( alg1 == alg2 )
851 return( alg1 );
852 /* If the policies are from the same hash-and-sign family, check
853 * if one is a wildcard. If so the other has the specific algorithm. */
854 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
855 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
856 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
857 {
858 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
859 return( alg2 );
860 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
861 return( alg1 );
862 }
863 /* If the policies are incompatible, allow nothing. */
864 return( 0 );
865}
866
Gilles Peskined6f371b2019-05-10 19:33:38 +0200867static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
868 psa_algorithm_t requested_alg )
869{
Gilles Peskine549ea862019-05-22 11:45:59 +0200870 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200871 if( requested_alg == policy_alg )
872 return( 1 );
873 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200874 * and requested_alg is the same hash-and-sign family with any hash,
875 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200876 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
877 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
878 {
879 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
880 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
881 }
Steven Cooremance48e852020-10-05 16:02:45 +0200882 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200883 * a key derivation with that key agreement should also be allowed. This
884 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200885 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
886 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
887 {
888 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
889 policy_alg );
890 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200891 /* If it isn't permitted, it's forbidden. */
892 return( 0 );
893}
894
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100895/** Test whether a policy permits an algorithm.
896 *
897 * The caller must test usage flags separately.
898 */
899static int psa_key_policy_permits( const psa_key_policy_t *policy,
900 psa_algorithm_t alg )
901{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200902 return( psa_key_algorithm_permits( policy->alg, alg ) ||
903 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100904}
905
Gilles Peskinef603c712019-01-19 13:40:11 +0100906/** Restrict a key policy based on a constraint.
907 *
908 * \param[in,out] policy The policy to restrict.
909 * \param[in] constraint The policy constraint to apply.
910 *
911 * \retval #PSA_SUCCESS
912 * \c *policy contains the intersection of the original value of
913 * \c *policy and \c *constraint.
914 * \retval #PSA_ERROR_INVALID_ARGUMENT
915 * \c *policy and \c *constraint are incompatible.
916 * \c *policy is unchanged.
917 */
918static psa_status_t psa_restrict_key_policy(
919 psa_key_policy_t *policy,
920 const psa_key_policy_t *constraint )
921{
922 psa_algorithm_t intersection_alg =
923 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200924 psa_algorithm_t intersection_alg2 =
925 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100926 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
927 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200928 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
929 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100930 policy->usage &= constraint->usage;
931 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200932 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100933 return( PSA_SUCCESS );
934}
935
Ronald Cron5c522922020-11-14 16:35:34 +0100936/** Get the description of a key given its identifier and policy constraints
937 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200938 *
Ronald Cron5c522922020-11-14 16:35:34 +0100939 * The key must have allow all the usage flags set in \p usage. If \p alg is
940 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +0100941 *
Ronald Cron5c522922020-11-14 16:35:34 +0100942 * In case of a persistent key, the function loads the description of the key
943 * into a key slot if not already done.
944 *
945 * On success, the returned key slot is locked. It is the responsibility of
946 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200947 */
Ronald Cron5c522922020-11-14 16:35:34 +0100948static psa_status_t psa_get_and_lock_key_slot_with_policy(
949 mbedtls_svc_key_id_t key,
950 psa_key_slot_t **p_slot,
951 psa_key_usage_t usage,
952 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100953{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200954 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
955 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100956
Ronald Cron5c522922020-11-14 16:35:34 +0100957 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100958 if( status != PSA_SUCCESS )
959 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200960 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100961
962 /* Enforce that usage policy for the key slot contains all the flags
963 * required by the usage parameter. There is one exception: public
964 * keys can always be exported, so we treat public key objects as
965 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200966 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100967 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200968
969 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +0200970 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200971 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100972
973 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200975 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +0100976
Darryl Green06fd18d2018-07-16 11:21:11 +0100977 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200978
979error:
980 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100981 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200982
983 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100984}
Darryl Green940d72c2018-07-13 13:18:51 +0100985
Ronald Cron5c522922020-11-14 16:35:34 +0100986/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200987 *
988 * A transparent key is a key for which the key material is directly
989 * available, as opposed to a key in a secure element.
990 *
Ronald Cron5c522922020-11-14 16:35:34 +0100991 * This is a temporary function to use instead of
992 * psa_get_and_lock_key_slot_with_policy() until secure element support is
993 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200994 *
Ronald Cron5c522922020-11-14 16:35:34 +0100995 * On success, the returned key slot is locked. It is the responsibility of the
996 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200997 */
998#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +0100999static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1000 mbedtls_svc_key_id_t key,
1001 psa_key_slot_t **p_slot,
1002 psa_key_usage_t usage,
1003 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001004{
Ronald Cron5c522922020-11-14 16:35:34 +01001005 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1006 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001007 if( status != PSA_SUCCESS )
1008 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001009
Gilles Peskineadad8132019-07-25 11:31:23 +02001010 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001011 {
Ronald Cron5c522922020-11-14 16:35:34 +01001012 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001013 *p_slot = NULL;
1014 return( PSA_ERROR_NOT_SUPPORTED );
1015 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001016
Gilles Peskine28f8f302019-07-24 13:30:31 +02001017 return( PSA_SUCCESS );
1018}
1019#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1020/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001021#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1022 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001023#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1024
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001025/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001026static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001027{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001028 /* Data pointer will always be either a valid pointer or NULL in an
1029 * initialized slot, so we can just free it. */
1030 if( slot->key.data != NULL )
1031 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1032
1033 mbedtls_free( slot->key.data );
1034 slot->key.data = NULL;
1035 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001036
1037 return( PSA_SUCCESS );
1038}
1039
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001040/** Completely wipe a slot in memory, including its policy.
1041 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001042psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001043{
1044 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001045
1046 /*
1047 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001048 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001049 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1050 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001051 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001052 */
Ronald Cron5c522922020-11-14 16:35:34 +01001053 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001054 {
1055#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001056 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001057#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001058 status = PSA_ERROR_CORRUPTION_DETECTED;
1059 }
1060
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001061 /* Multipart operations may still be using the key. This is safe
1062 * because all multipart operation objects are independent from
1063 * the key slot: if they need to access the key after the setup
1064 * phase, they have a copy of the key. Note that this means that
1065 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001066 /* At this point, key material and other type-specific content has
1067 * been wiped. Clear remaining metadata. We can call memset and not
1068 * zeroize because the metadata is not particularly sensitive. */
1069 memset( slot, 0, sizeof( *slot ) );
1070 return( status );
1071}
1072
Ronald Croncf56a0a2020-08-04 09:51:30 +02001073psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001074{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001075 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001076 psa_status_t status; /* status of the last operation */
1077 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001078#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1079 psa_se_drv_table_entry_t *driver;
1080#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081
Ronald Croncf56a0a2020-08-04 09:51:30 +02001082 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001083 return( PSA_SUCCESS );
1084
Ronald Cronf2911112020-10-29 17:51:10 +01001085 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001086 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001087 * key, this will load the key description from persistent memory if not
1088 * done yet. We cannot avoid this loading as without it we don't know if
1089 * the key is operated by an SE or not and this information is needed by
1090 * the current implementation.
1091 */
Ronald Cron5c522922020-11-14 16:35:34 +01001092 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001093 if( status != PSA_SUCCESS )
1094 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001095
Ronald Cronf2911112020-10-29 17:51:10 +01001096 /*
1097 * If the key slot containing the key description is under access by the
1098 * library (apart from the present access), the key cannot be destroyed
1099 * yet. For the time being, just return in error. Eventually (to be
1100 * implemented), the key should be destroyed when all accesses have
1101 * stopped.
1102 */
Ronald Cron5c522922020-11-14 16:35:34 +01001103 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001104 {
Ronald Cron5c522922020-11-14 16:35:34 +01001105 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001106 return( PSA_ERROR_GENERIC_ERROR );
1107 }
1108
Gilles Peskine354f7672019-07-12 23:46:38 +02001109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001110 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001111 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001112 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001113 /* For a key in a secure element, we need to do three things:
1114 * remove the key file in internal storage, destroy the
1115 * key inside the secure element, and update the driver's
1116 * persistent data. Start a transaction that will encompass these
1117 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001118 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001119 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001120 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001121 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001122 status = psa_crypto_save_transaction( );
1123 if( status != PSA_SUCCESS )
1124 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001125 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001126 /* We should still try to destroy the key in the secure
1127 * element and the key metadata in storage. This is especially
1128 * important if the error is that the storage is full.
1129 * But how to do it exactly without risking an inconsistent
1130 * state after a reset?
1131 * https://github.com/ARMmbed/mbed-crypto/issues/215
1132 */
1133 overall_status = status;
1134 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001135 }
1136
Ronald Cronea0f8a62020-11-25 17:52:23 +01001137 status = psa_destroy_se_key( driver,
1138 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001139 if( overall_status == PSA_SUCCESS )
1140 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001141 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001142#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1143
Darryl Greend49a4992018-06-18 17:27:26 +01001144#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001145 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001146 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001147 status = psa_destroy_persistent_key( slot->attr.id );
1148 if( overall_status == PSA_SUCCESS )
1149 overall_status = status;
1150
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001151 /* TODO: other slots may have a copy of the same key. We should
1152 * invalidate them.
1153 * https://github.com/ARMmbed/mbed-crypto/issues/214
1154 */
Darryl Greend49a4992018-06-18 17:27:26 +01001155 }
1156#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001157
Gilles Peskinefc762652019-07-22 19:30:34 +02001158#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1159 if( driver != NULL )
1160 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001161 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001162 if( overall_status == PSA_SUCCESS )
1163 overall_status = status;
1164 status = psa_crypto_stop_transaction( );
1165 if( overall_status == PSA_SUCCESS )
1166 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001167 }
1168#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1169
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001170#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1171exit:
1172#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001173 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001174 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1175 if( overall_status == PSA_SUCCESS )
1176 overall_status = status;
1177 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001178}
1179
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001180void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001181{
Gilles Peskineb699f072019-04-26 16:06:02 +02001182 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001184}
1185
Gilles Peskineb699f072019-04-26 16:06:02 +02001186psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1187 psa_key_type_t type,
1188 const uint8_t *data,
1189 size_t data_length )
1190{
1191 uint8_t *copy = NULL;
1192
1193 if( data_length != 0 )
1194 {
1195 copy = mbedtls_calloc( 1, data_length );
1196 if( copy == NULL )
1197 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1198 memcpy( copy, data, data_length );
1199 }
1200 /* After this point, this function is guaranteed to succeed, so it
1201 * can start modifying `*attributes`. */
1202
1203 if( attributes->domain_parameters != NULL )
1204 {
1205 mbedtls_free( attributes->domain_parameters );
1206 attributes->domain_parameters = NULL;
1207 attributes->domain_parameters_size = 0;
1208 }
1209
1210 attributes->domain_parameters = copy;
1211 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001212 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001213 return( PSA_SUCCESS );
1214}
1215
1216psa_status_t psa_get_key_domain_parameters(
1217 const psa_key_attributes_t *attributes,
1218 uint8_t *data, size_t data_size, size_t *data_length )
1219{
1220 if( attributes->domain_parameters_size > data_size )
1221 return( PSA_ERROR_BUFFER_TOO_SMALL );
1222 *data_length = attributes->domain_parameters_size;
1223 if( attributes->domain_parameters_size != 0 )
1224 memcpy( data, attributes->domain_parameters,
1225 attributes->domain_parameters_size );
1226 return( PSA_SUCCESS );
1227}
1228
John Durkop07cc04a2020-11-16 22:08:34 -08001229#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001230 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001231static psa_status_t psa_get_rsa_public_exponent(
1232 const mbedtls_rsa_context *rsa,
1233 psa_key_attributes_t *attributes )
1234{
1235 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001237 uint8_t *buffer = NULL;
1238 size_t buflen;
1239 mbedtls_mpi_init( &mpi );
1240
1241 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1242 if( ret != 0 )
1243 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001244 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1245 {
1246 /* It's the default value, which is reported as an empty string,
1247 * so there's nothing to do. */
1248 goto exit;
1249 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001250
1251 buflen = mbedtls_mpi_size( &mpi );
1252 buffer = mbedtls_calloc( 1, buflen );
1253 if( buffer == NULL )
1254 {
1255 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1256 goto exit;
1257 }
1258 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1259 if( ret != 0 )
1260 goto exit;
1261 attributes->domain_parameters = buffer;
1262 attributes->domain_parameters_size = buflen;
1263
1264exit:
1265 mbedtls_mpi_free( &mpi );
1266 if( ret != 0 )
1267 mbedtls_free( buffer );
1268 return( mbedtls_to_psa_error( ret ) );
1269}
John Durkop07cc04a2020-11-16 22:08:34 -08001270#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001271 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001272
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001273/** Retrieve all the publicly-accessible attributes of a key.
1274 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001275psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001276 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001277{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001278 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001279 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001280 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001281
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001282 psa_reset_key_attributes( attributes );
1283
Ronald Cron5c522922020-11-14 16:35:34 +01001284 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001285 if( status != PSA_SUCCESS )
1286 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001287
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001288 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001289 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1290 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1291
1292#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1293 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001294 psa_set_key_slot_number( attributes,
1295 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001296#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001297
Gilles Peskine8e338702019-07-30 20:06:31 +02001298 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001299 {
John Durkop07cc04a2020-11-16 22:08:34 -08001300#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001301 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001302 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001303 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001304#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001305 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001306 * is not yet implemented.
1307 * https://github.com/ARMmbed/mbed-crypto/issues/216
1308 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001309 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001310 break;
1311#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001312 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001313 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001314
Ronald Cron90857082020-11-25 14:58:33 +01001315 status = mbedtls_psa_rsa_load_representation(
1316 slot->attr.type,
1317 slot->key.data,
1318 slot->key.bytes,
1319 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001320 if( status != PSA_SUCCESS )
1321 break;
1322
Steven Cooremana2371e52020-07-28 14:30:39 +02001323 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001324 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001325 mbedtls_rsa_free( rsa );
1326 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001327 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001328 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001329#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001330 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001331 default:
1332 /* Nothing else to do. */
1333 break;
1334 }
1335
1336 if( status != PSA_SUCCESS )
1337 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001338
Ronald Cron5c522922020-11-14 16:35:34 +01001339 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001340
Ronald Cron5c522922020-11-14 16:35:34 +01001341 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001342}
1343
Gilles Peskinec8000c02019-08-02 20:15:51 +02001344#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1345psa_status_t psa_get_key_slot_number(
1346 const psa_key_attributes_t *attributes,
1347 psa_key_slot_number_t *slot_number )
1348{
1349 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1350 {
1351 *slot_number = attributes->slot_number;
1352 return( PSA_SUCCESS );
1353 }
1354 else
1355 return( PSA_ERROR_INVALID_ARGUMENT );
1356}
1357#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1358
Ronald Crond18b5f82020-11-25 16:46:05 +01001359static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1360 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001361 uint8_t *data,
1362 size_t data_size,
1363 size_t *data_length )
1364{
Ronald Crond18b5f82020-11-25 16:46:05 +01001365 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001366 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001367 memcpy( data, key_buffer, key_buffer_size );
1368 memset( data + key_buffer_size, 0,
1369 data_size - key_buffer_size );
1370 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001371 return( PSA_SUCCESS );
1372}
1373
Ronald Cron7285cda2020-11-26 14:46:37 +01001374psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001375 const psa_key_attributes_t *attributes,
1376 const uint8_t *key_buffer, size_t key_buffer_size,
1377 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001378{
Ronald Crond18b5f82020-11-25 16:46:05 +01001379 psa_key_type_t type = attributes->core.type;
1380
Ronald Crond18b5f82020-11-25 16:46:05 +01001381 if( key_type_is_raw_bytes( type ) ||
1382 PSA_KEY_TYPE_IS_RSA( type ) ||
1383 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001384 {
1385 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001386 key_buffer, key_buffer_size,
1387 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 }
1389 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 }
1396}
1397
1398psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1399 uint8_t *data,
1400 size_t data_size,
1401 size_t *data_length )
1402{
1403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1404 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1405 psa_key_slot_t *slot;
1406
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. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001411 *data_length = 0;
1412
Ronald Cron9486f9d2020-11-26 13:36:23 +01001413 /* Export requires the EXPORT flag. There is an exception for public keys,
1414 * which don't require any flag, but
1415 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1416 */
1417 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1418 PSA_KEY_USAGE_EXPORT, 0 );
1419 if( status != PSA_SUCCESS )
1420 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001421
Gilles Peskinef9168942019-09-12 19:20:29 +02001422 /* Reject a zero-length output buffer now, since this can never be a
1423 * valid key representation. This way we know that data must be a valid
1424 * pointer and we can do things like memset(data, ..., data_size). */
1425 if( data_size == 0 )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001426 {
1427 status = PSA_ERROR_BUFFER_TOO_SMALL;
1428 goto exit;
1429 }
1430
Ronald Crond18b5f82020-11-25 16:46:05 +01001431 psa_key_attributes_t attributes = {
1432 .core = slot->attr
1433 };
Ronald Cron67227982020-11-26 15:16:05 +01001434 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001435 slot->key.data, slot->key.bytes,
1436 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001437
1438exit:
1439 unlock_status = psa_unlock_key_slot( slot );
1440
1441 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1442}
1443
Ronald Cron7285cda2020-11-26 14:46:37 +01001444psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001445 const psa_key_attributes_t *attributes,
1446 const uint8_t *key_buffer,
1447 size_t key_buffer_size,
1448 uint8_t *data,
1449 size_t data_size,
1450 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001451{
Ronald Crond18b5f82020-11-25 16:46:05 +01001452 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001453
Ronald Crond18b5f82020-11-25 16:46:05 +01001454 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001455 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001456 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001457 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001458 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001459 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001460 key_buffer, key_buffer_size,
1461 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001462 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001463
Ronald Crond18b5f82020-11-25 16:46:05 +01001464 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001465 {
John Durkop0e005192020-10-31 22:06:54 -07001466#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1467 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001468 return( mbedtls_psa_rsa_export_public_key( attributes,
1469 key_buffer,
1470 key_buffer_size,
1471 data,
1472 data_size,
1473 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001474#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001475 /* We don't know how to convert a private RSA key to public. */
1476 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001477#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1478 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001479 }
1480 else
Moran Pekera998bc62018-04-16 18:16:20 +03001481 {
John Durkop6ba40d12020-11-10 08:50:04 -08001482#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1483 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001484 return( mbedtls_psa_ecp_export_public_key( attributes,
1485 key_buffer,
1486 key_buffer_size,
1487 data,
1488 data_size,
1489 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001490#else
1491 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001492 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001493#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1494 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001495 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001496 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001497 else
1498 {
1499 /* This shouldn't happen in the reference implementation, but
1500 it is valid for a special-purpose implementation to omit
1501 support for exporting certain key types. */
1502 return( PSA_ERROR_NOT_SUPPORTED );
1503 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001504}
Ronald Crond18b5f82020-11-25 16:46:05 +01001505
Ronald Croncf56a0a2020-08-04 09:51:30 +02001506psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001507 uint8_t *data,
1508 size_t data_size,
1509 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001510{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001511 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001512 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001513 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001514
1515 /* Set the key to empty now, so that even when there are errors, we always
1516 * set data_length to a value between 0 and data_size. On error, setting
1517 * the key to empty is a good choice because an empty key representation is
1518 * unlikely to be accepted anywhere. */
1519 *data_length = 0;
1520
1521 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001522 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001523 if( status != PSA_SUCCESS )
1524 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001525
Ronald Cron9486f9d2020-11-26 13:36:23 +01001526 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1527 {
1528 status = PSA_ERROR_INVALID_ARGUMENT;
1529 goto exit;
1530 }
1531
1532 /* Reject a zero-length output buffer now, since this can never be a
1533 * valid key representation. This way we know that data must be a valid
1534 * pointer and we can do things like memset(data, ..., data_size). */
1535 if( data_size == 0 )
1536 {
1537 status = PSA_ERROR_BUFFER_TOO_SMALL;
1538 goto exit;
1539 }
1540
Ronald Crond18b5f82020-11-25 16:46:05 +01001541 psa_key_attributes_t attributes = {
1542 .core = slot->attr
1543 };
Ronald Cron67227982020-11-26 15:16:05 +01001544 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001545 &attributes, slot->key.data, slot->key.bytes,
1546 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001547
1548exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001549 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001550
Ronald Cron5c522922020-11-14 16:35:34 +01001551 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001552}
1553
Gilles Peskine91e8c332019-08-02 19:19:39 +02001554#if defined(static_assert)
1555static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1556 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001557static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001558 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001559static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001560 "One or more key attribute flag is listed as both internal-only and external-only" );
1561#endif
1562
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001563/** Validate that a key policy is internally well-formed.
1564 *
1565 * This function only rejects invalid policies. It does not validate the
1566 * consistency of the policy with respect to other attributes of the key
1567 * such as the key type.
1568 */
1569static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001570{
1571 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001572 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001573 PSA_KEY_USAGE_ENCRYPT |
1574 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001575 PSA_KEY_USAGE_SIGN_HASH |
1576 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001577 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1578 return( PSA_ERROR_INVALID_ARGUMENT );
1579
Gilles Peskine4747d192019-04-17 15:05:45 +02001580 return( PSA_SUCCESS );
1581}
1582
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001583/** Validate the internal consistency of key attributes.
1584 *
1585 * This function only rejects invalid attribute values. If does not
1586 * validate the consistency of the attributes with any key data that may
1587 * be involved in the creation of the key.
1588 *
1589 * Call this function early in the key creation process.
1590 *
1591 * \param[in] attributes Key attributes for the new key.
1592 * \param[out] p_drv On any return, the driver for the key, if any.
1593 * NULL for a transparent key.
1594 *
1595 */
1596static psa_status_t psa_validate_key_attributes(
1597 const psa_key_attributes_t *attributes,
1598 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001599{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001600 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001601 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001602 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001603
Ronald Cron54b90082020-10-29 15:26:43 +01001604 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001605 if( status != PSA_SUCCESS )
1606 return( status );
1607
Ronald Crond2ed4812020-07-17 16:11:30 +02001608 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001609 if( status != PSA_SUCCESS )
1610 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001611
Ronald Cron65f38a32020-10-23 17:11:13 +02001612 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1613 {
1614 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1615 return( PSA_ERROR_INVALID_ARGUMENT );
1616 }
1617 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001618 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001619 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001620 if( status != PSA_SUCCESS )
1621 return( status );
1622 }
1623
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001624 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001625 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001626 return( status );
1627
1628 /* Refuse to create overly large keys.
1629 * Note that this doesn't trigger on import if the attributes don't
1630 * explicitly specify a size (so psa_get_key_bits returns 0), so
1631 * psa_import_key() needs its own checks. */
1632 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1633 return( PSA_ERROR_NOT_SUPPORTED );
1634
Gilles Peskine91e8c332019-08-02 19:19:39 +02001635 /* Reject invalid flags. These should not be reachable through the API. */
1636 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1637 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1638 return( PSA_ERROR_INVALID_ARGUMENT );
1639
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001640 return( PSA_SUCCESS );
1641}
1642
Gilles Peskine4747d192019-04-17 15:05:45 +02001643/** Prepare a key slot to receive key material.
1644 *
1645 * This function allocates a key slot and sets its metadata.
1646 *
1647 * If this function fails, call psa_fail_key_creation().
1648 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001649 * This function is intended to be used as follows:
1650 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001651 * it with the specified attributes, and in case of a volatile key assign it
1652 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001653 * -# Populate the slot with the key material.
1654 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1655 * In case of failure at any step, stop the sequence and call
1656 * psa_fail_key_creation().
1657 *
Ronald Cron5c522922020-11-14 16:35:34 +01001658 * On success, the key slot is locked. It is the responsibility of the caller
1659 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001660 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001661 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001662 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001663 * \param[out] p_slot On success, a pointer to the prepared slot.
1664 * \param[out] p_drv On any return, the driver for the key, if any.
1665 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001666 *
1667 * \retval #PSA_SUCCESS
1668 * The key slot is ready to receive key material.
1669 * \return If this function fails, the key slot is an invalid state.
1670 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001671 */
1672static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001673 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001674 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001675 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001676 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001677{
1678 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001679 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 psa_key_slot_t *slot;
1681
Gilles Peskinedf179142019-07-15 22:02:14 +02001682 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001683 *p_drv = NULL;
1684
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001685 status = psa_validate_key_attributes( attributes, p_drv );
1686 if( status != PSA_SUCCESS )
1687 return( status );
1688
Ronald Cronc4d1b512020-07-31 11:26:37 +02001689 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001690 if( status != PSA_SUCCESS )
1691 return( status );
1692 slot = *p_slot;
1693
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001694 /* We're storing the declared bit-size of the key. It's up to each
1695 * creation mechanism to verify that this information is correct.
1696 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001697 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001698 * is optional (import, copy). In case of a volatile key, assign it the
1699 * volatile key identifier associated to the slot returned to contain its
1700 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001701
1702 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001703 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1704 {
1705#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1706 slot->attr.id = volatile_key_id;
1707#else
1708 slot->attr.id.key_id = volatile_key_id;
1709#endif
1710 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001711
Gilles Peskine91e8c332019-08-02 19:19:39 +02001712 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001713 * external-only flags, query `attributes`. Thanks to the check
1714 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001715 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001716 * may have set. */
1717 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001718
Gilles Peskinecbaff462019-07-12 23:46:04 +02001719#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001720 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001721 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001722 * create the key file in internal storage, create the
1723 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001724 * persistent data. This is done by starting a transaction that will
1725 * encompass these three actions.
1726 * For registering a volatile key, we just need to find an appropriate
1727 * slot number inside the SE. Since the key is designated volatile, creating
1728 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001729 /* The first thing to do is to find a slot number for the new key.
1730 * We save the slot number in persistent storage as part of the
1731 * transaction data. It will be needed to recover if the power
1732 * fails during the key creation process, to clean up on the secure
1733 * element side after restarting. Obtaining a slot number from the
1734 * secure element driver updates its persistent state, but we do not yet
1735 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001736 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001737 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001738 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001739 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001740 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001741 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001742 if( status != PSA_SUCCESS )
1743 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001744
1745 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001746 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001747 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1748 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001749 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001750 psa_crypto_transaction.key.id = slot->attr.id;
1751 status = psa_crypto_save_transaction( );
1752 if( status != PSA_SUCCESS )
1753 {
1754 (void) psa_crypto_stop_transaction( );
1755 return( status );
1756 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001757 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001758
1759 status = psa_copy_key_material_into_slot(
1760 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001761 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001762
1763 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1764 {
1765 /* Key registration only makes sense with a secure element. */
1766 return( PSA_ERROR_INVALID_ARGUMENT );
1767 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001768#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1769
Ronald Cronc4d1b512020-07-31 11:26:37 +02001770 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001771}
Gilles Peskine4747d192019-04-17 15:05:45 +02001772
1773/** Finalize the creation of a key once its key material has been set.
1774 *
1775 * This entails writing the key to persistent storage.
1776 *
1777 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001778 * See the documentation of psa_start_key_creation() for the intended use
1779 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001780 *
Ronald Cron5c522922020-11-14 16:35:34 +01001781 * If the finalization succeeds, the function unlocks the key slot (it was
1782 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1783 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001784 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001785 * \param[in,out] slot Pointer to the slot with key material.
1786 * \param[in] driver The secure element driver for the key,
1787 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001788 * \param[out] key On success, identifier of the key. Note that the
1789 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001790 *
1791 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001792 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001793 * \return If this function fails, the key slot is an invalid state.
1794 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001795 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001796static psa_status_t psa_finish_key_creation(
1797 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001798 psa_se_drv_table_entry_t *driver,
1799 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001800{
1801 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001802 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001803 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001804
1805#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001806 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001807 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001808#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1809 if( driver != NULL )
1810 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001811 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001812 psa_key_slot_number_t slot_number =
1813 psa_key_slot_get_slot_number( slot ) ;
1814
Gilles Peskineb46bef22019-07-30 21:32:04 +02001815#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001816 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001817 sizeof( data.slot_number ),
1818 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001819#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001820 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001821 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001822 (uint8_t*) &data,
1823 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001824 }
1825 else
1826#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1827 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001828 /* Key material is saved in export representation in the slot, so
1829 * just pass the slot buffer for storage. */
1830 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001831 slot->key.data,
1832 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001833 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001834 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001835#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1836
Gilles Peskinecbaff462019-07-12 23:46:04 +02001837#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001838 /* Finish the transaction for a key creation. This does not
1839 * happen when registering an existing key. Detect this case
1840 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001841 * creation of a persistent key in a secure element requires a transaction,
1842 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001843 if( driver != NULL &&
1844 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001845 {
1846 status = psa_save_se_persistent_data( driver );
1847 if( status != PSA_SUCCESS )
1848 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001849 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001850 return( status );
1851 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001852 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001853 }
1854#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1855
Ronald Cron50972942020-11-14 11:28:25 +01001856 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001857 {
1858 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001859 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001860 if( status != PSA_SUCCESS )
1861 *key = MBEDTLS_SVC_KEY_ID_INIT;
1862 }
Ronald Cron50972942020-11-14 11:28:25 +01001863
Gilles Peskine4747d192019-04-17 15:05:45 +02001864 return( status );
1865}
1866
1867/** Abort the creation of a key.
1868 *
1869 * You may call this function after calling psa_start_key_creation(),
1870 * or after psa_finish_key_creation() fails. In other circumstances, this
1871 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001872 * See the documentation of psa_start_key_creation() for the intended use
1873 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001874 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001875 * \param[in,out] slot Pointer to the slot with key material.
1876 * \param[in] driver The secure element driver for the key,
1877 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001878 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001879static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001880 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001881{
Gilles Peskine011e4282019-06-26 18:34:38 +02001882 (void) driver;
1883
Gilles Peskine4747d192019-04-17 15:05:45 +02001884 if( slot == NULL )
1885 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001886
1887#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001888 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001889 * element, and the failure happened later (when saving metadata
1890 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001891 * element.
1892 * https://github.com/ARMmbed/mbed-crypto/issues/217
1893 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001894
Gilles Peskined7729582019-08-05 15:55:54 +02001895 /* Abort the ongoing transaction if any (there may not be one if
1896 * the creation process failed before starting one, or if the
1897 * key creation is a registration of a key in a secure element).
1898 * Earlier functions must already have done what it takes to undo any
1899 * partial creation. All that's left is to update the transaction data
1900 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001901 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001902#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1903
Gilles Peskine4747d192019-04-17 15:05:45 +02001904 psa_wipe_key_slot( slot );
1905}
1906
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001907/** Validate optional attributes during key creation.
1908 *
1909 * Some key attributes are optional during key creation. If they are
1910 * specified in the attributes structure, check that they are consistent
1911 * with the data in the slot.
1912 *
1913 * This function should be called near the end of key creation, after
1914 * the slot in memory is fully populated but before saving persistent data.
1915 */
1916static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001917 const psa_key_slot_t *slot,
1918 const psa_key_attributes_t *attributes )
1919{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001920 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001921 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001922 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001923 return( PSA_ERROR_INVALID_ARGUMENT );
1924 }
1925
1926 if( attributes->domain_parameters_size != 0 )
1927 {
John Durkop0e005192020-10-31 22:06:54 -07001928#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1929 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001930 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001931 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001932 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001933 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001934 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001935
Ronald Cron90857082020-11-25 14:58:33 +01001936 psa_status_t status = mbedtls_psa_rsa_load_representation(
1937 slot->attr.type,
1938 slot->key.data,
1939 slot->key.bytes,
1940 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001941 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001942 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001943
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001944 mbedtls_mpi_init( &actual );
1945 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001946 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001948 mbedtls_rsa_free( rsa );
1949 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001950 if( ret != 0 )
1951 goto rsa_exit;
1952 ret = mbedtls_mpi_read_binary( &required,
1953 attributes->domain_parameters,
1954 attributes->domain_parameters_size );
1955 if( ret != 0 )
1956 goto rsa_exit;
1957 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1958 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1959 rsa_exit:
1960 mbedtls_mpi_free( &actual );
1961 mbedtls_mpi_free( &required );
1962 if( ret != 0)
1963 return( mbedtls_to_psa_error( ret ) );
1964 }
1965 else
John Durkop9814fa22020-11-04 12:28:15 -08001966#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1967 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001968 {
1969 return( PSA_ERROR_INVALID_ARGUMENT );
1970 }
1971 }
1972
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001973 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001974 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001975 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001976 return( PSA_ERROR_INVALID_ARGUMENT );
1977 }
1978
1979 return( PSA_SUCCESS );
1980}
1981
Gilles Peskine4747d192019-04-17 15:05:45 +02001982psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001983 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001984 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001985 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001986{
1987 psa_status_t status;
1988 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001989 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990
Ronald Cron81709fc2020-11-14 12:10:32 +01001991 *key = MBEDTLS_SVC_KEY_ID_INIT;
1992
Gilles Peskine0f84d622019-09-12 19:03:13 +02001993 /* Reject zero-length symmetric keys (including raw data key objects).
1994 * This also rejects any key which might be encoded as an empty string,
1995 * which is never valid. */
1996 if( data_length == 0 )
1997 return( PSA_ERROR_INVALID_ARGUMENT );
1998
Gilles Peskinedf179142019-07-15 22:02:14 +02001999 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002000 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002001 if( status != PSA_SUCCESS )
2002 goto exit;
2003
Gilles Peskine5d309672019-07-12 23:47:28 +02002004#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2005 if( driver != NULL )
2006 {
2007 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002008 /* The driver should set the number of key bits, however in
2009 * case it doesn't, we initialize bits to an invalid value. */
2010 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002011 if( drv->key_management == NULL ||
2012 drv->key_management->p_import == NULL )
2013 {
2014 status = PSA_ERROR_NOT_SUPPORTED;
2015 goto exit;
2016 }
2017 status = drv->key_management->p_import(
2018 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01002019 psa_key_slot_get_slot_number( slot ),
2020 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002021 if( status != PSA_SUCCESS )
2022 goto exit;
2023 if( bits > PSA_MAX_KEY_BITS )
2024 {
2025 status = PSA_ERROR_NOT_SUPPORTED;
2026 goto exit;
2027 }
2028 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002029 }
2030 else
2031#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002032 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2033 {
2034 /* Importing a key with external lifetime through the driver wrapper
2035 * interface is not yet supported. Return as if this was an invalid
2036 * lifetime. */
2037 status = PSA_ERROR_INVALID_ARGUMENT;
2038 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002039 }
2040 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002041 {
2042 status = psa_import_key_into_slot( slot, data, data_length );
2043 if( status != PSA_SUCCESS )
2044 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002045 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002046 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002047 if( status != PSA_SUCCESS )
2048 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049
Ronald Cron81709fc2020-11-14 12:10:32 +01002050 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002051exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002052 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002053 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002054
Gilles Peskine4747d192019-04-17 15:05:45 +02002055 return( status );
2056}
2057
Gilles Peskined7729582019-08-05 15:55:54 +02002058#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2059psa_status_t mbedtls_psa_register_se_key(
2060 const psa_key_attributes_t *attributes )
2061{
2062 psa_status_t status;
2063 psa_key_slot_t *slot = NULL;
2064 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002065 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002066
2067 /* Leaving attributes unspecified is not currently supported.
2068 * It could make sense to query the key type and size from the
2069 * secure element, but not all secure elements support this
2070 * and the driver HAL doesn't currently support it. */
2071 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2072 return( PSA_ERROR_NOT_SUPPORTED );
2073 if( psa_get_key_bits( attributes ) == 0 )
2074 return( PSA_ERROR_NOT_SUPPORTED );
2075
2076 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002077 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002078 if( status != PSA_SUCCESS )
2079 goto exit;
2080
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002082
2083exit:
2084 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002085 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002086
Gilles Peskined7729582019-08-05 15:55:54 +02002087 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002088 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002089 return( status );
2090}
2091#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2092
Gilles Peskinef603c712019-01-19 13:40:11 +01002093static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002095{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002096 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002097 source->key.data,
2098 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002099 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002100 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002101
Steven Cooreman398aee52020-10-13 14:35:45 +02002102 target->attr.type = source->attr.type;
2103 target->attr.bits = source->attr.bits;
2104
2105 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002106}
2107
Ronald Croncf56a0a2020-08-04 09:51:30 +02002108psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002109 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002110 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002111{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002113 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002114 psa_key_slot_t *source_slot = NULL;
2115 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002116 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002117 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002118
Ronald Cron81709fc2020-11-14 12:10:32 +01002119 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2120
Ronald Cron5c522922020-11-14 16:35:34 +01002121 status = psa_get_and_lock_transparent_key_slot_with_policy(
2122 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002123 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002124 goto exit;
2125
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002126 status = psa_validate_optional_attributes( source_slot,
2127 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128 if( status != PSA_SUCCESS )
2129 goto exit;
2130
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002131 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002132 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002133 if( status != PSA_SUCCESS )
2134 goto exit;
2135
Ronald Cron81709fc2020-11-14 12:10:32 +01002136 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2137 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002138 if( status != PSA_SUCCESS )
2139 goto exit;
2140
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002141#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2142 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002143 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002144 /* Copying to a secure element is not implemented yet. */
2145 status = PSA_ERROR_NOT_SUPPORTED;
2146 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002147 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002148#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002149
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002150 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002151 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002152 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002153
Ronald Cron81709fc2020-11-14 12:10:32 +01002154 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002155exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002156 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002157 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002158
Ronald Cron5c522922020-11-14 16:35:34 +01002159 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002160
Ronald Cron5c522922020-11-14 16:35:34 +01002161 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002162}
2163
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002164
2165
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002166/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002167/* Message digests */
2168/****************************************************************/
2169
John Durkop07cc04a2020-11-16 22:08:34 -08002170#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002171 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2172 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002173 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002174static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002175{
2176 switch( alg )
2177 {
John Durkopee4e6602020-11-27 08:48:46 -08002178#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002179 case PSA_ALG_MD2:
2180 return( &mbedtls_md2_info );
2181#endif
John Durkopee4e6602020-11-27 08:48:46 -08002182#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002183 case PSA_ALG_MD4:
2184 return( &mbedtls_md4_info );
2185#endif
John Durkopee4e6602020-11-27 08:48:46 -08002186#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002187 case PSA_ALG_MD5:
2188 return( &mbedtls_md5_info );
2189#endif
John Durkopee4e6602020-11-27 08:48:46 -08002190#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002191 case PSA_ALG_RIPEMD160:
2192 return( &mbedtls_ripemd160_info );
2193#endif
John Durkopee4e6602020-11-27 08:48:46 -08002194#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002195 case PSA_ALG_SHA_1:
2196 return( &mbedtls_sha1_info );
2197#endif
John Durkopee4e6602020-11-27 08:48:46 -08002198#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002199 case PSA_ALG_SHA_224:
2200 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002201#endif
2202#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002203 case PSA_ALG_SHA_256:
2204 return( &mbedtls_sha256_info );
2205#endif
John Durkopee4e6602020-11-27 08:48:46 -08002206#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002207 case PSA_ALG_SHA_384:
2208 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002209#endif
John Durkopee4e6602020-11-27 08:48:46 -08002210#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002211 case PSA_ALG_SHA_512:
2212 return( &mbedtls_sha512_info );
2213#endif
2214 default:
2215 return( NULL );
2216 }
2217}
John Durkop07cc04a2020-11-16 22:08:34 -08002218#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002219 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2220 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2221 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002222
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2224{
2225 switch( operation->alg )
2226 {
Gilles Peskine81736312018-06-26 15:04:31 +02002227 case 0:
2228 /* The object has (apparently) been initialized but it is not
2229 * in use. It's ok to call abort on such an object, and there's
2230 * nothing to do. */
2231 break;
John Durkopee4e6602020-11-27 08:48:46 -08002232#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002233 case PSA_ALG_MD2:
2234 mbedtls_md2_free( &operation->ctx.md2 );
2235 break;
2236#endif
John Durkopee4e6602020-11-27 08:48:46 -08002237#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238 case PSA_ALG_MD4:
2239 mbedtls_md4_free( &operation->ctx.md4 );
2240 break;
2241#endif
John Durkopee4e6602020-11-27 08:48:46 -08002242#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002243 case PSA_ALG_MD5:
2244 mbedtls_md5_free( &operation->ctx.md5 );
2245 break;
2246#endif
John Durkopee4e6602020-11-27 08:48:46 -08002247#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002248 case PSA_ALG_RIPEMD160:
2249 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2250 break;
2251#endif
John Durkopee4e6602020-11-27 08:48:46 -08002252#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002253 case PSA_ALG_SHA_1:
2254 mbedtls_sha1_free( &operation->ctx.sha1 );
2255 break;
2256#endif
John Durkop6ca23272020-12-03 06:01:32 -08002257#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002259 mbedtls_sha256_free( &operation->ctx.sha256 );
2260 break;
2261#endif
2262#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263 case PSA_ALG_SHA_256:
2264 mbedtls_sha256_free( &operation->ctx.sha256 );
2265 break;
2266#endif
John Durkop6ca23272020-12-03 06:01:32 -08002267#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002268 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002269 mbedtls_sha512_free( &operation->ctx.sha512 );
2270 break;
2271#endif
2272#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273 case PSA_ALG_SHA_512:
2274 mbedtls_sha512_free( &operation->ctx.sha512 );
2275 break;
2276#endif
2277 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002278 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279 }
2280 operation->alg = 0;
2281 return( PSA_SUCCESS );
2282}
2283
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002284psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285 psa_algorithm_t alg )
2286{
Janos Follath24eed8d2019-11-22 13:21:35 +00002287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002288
2289 /* A context must be freshly initialized before it can be set up. */
2290 if( operation->alg != 0 )
2291 {
2292 return( PSA_ERROR_BAD_STATE );
2293 }
2294
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295 switch( alg )
2296 {
John Durkopee4e6602020-11-27 08:48:46 -08002297#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002298 case PSA_ALG_MD2:
2299 mbedtls_md2_init( &operation->ctx.md2 );
2300 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2301 break;
2302#endif
John Durkopee4e6602020-11-27 08:48:46 -08002303#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002304 case PSA_ALG_MD4:
2305 mbedtls_md4_init( &operation->ctx.md4 );
2306 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2307 break;
2308#endif
John Durkopee4e6602020-11-27 08:48:46 -08002309#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310 case PSA_ALG_MD5:
2311 mbedtls_md5_init( &operation->ctx.md5 );
2312 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2313 break;
2314#endif
John Durkopee4e6602020-11-27 08:48:46 -08002315#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316 case PSA_ALG_RIPEMD160:
2317 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2318 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2319 break;
2320#endif
John Durkopee4e6602020-11-27 08:48:46 -08002321#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322 case PSA_ALG_SHA_1:
2323 mbedtls_sha1_init( &operation->ctx.sha1 );
2324 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2325 break;
2326#endif
John Durkopee4e6602020-11-27 08:48:46 -08002327#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328 case PSA_ALG_SHA_224:
2329 mbedtls_sha256_init( &operation->ctx.sha256 );
2330 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2331 break;
John Durkopee4e6602020-11-27 08:48:46 -08002332#endif
2333#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334 case PSA_ALG_SHA_256:
2335 mbedtls_sha256_init( &operation->ctx.sha256 );
2336 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2337 break;
2338#endif
John Durkopee4e6602020-11-27 08:48:46 -08002339#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340 case PSA_ALG_SHA_384:
2341 mbedtls_sha512_init( &operation->ctx.sha512 );
2342 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2343 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002344#endif
John Durkopee4e6602020-11-27 08:48:46 -08002345#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 case PSA_ALG_SHA_512:
2347 mbedtls_sha512_init( &operation->ctx.sha512 );
2348 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2349 break;
2350#endif
2351 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002352 return( PSA_ALG_IS_HASH( alg ) ?
2353 PSA_ERROR_NOT_SUPPORTED :
2354 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355 }
2356 if( ret == 0 )
2357 operation->alg = alg;
2358 else
2359 psa_hash_abort( operation );
2360 return( mbedtls_to_psa_error( ret ) );
2361}
2362
2363psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2364 const uint8_t *input,
2365 size_t input_length )
2366{
Janos Follath24eed8d2019-11-22 13:21:35 +00002367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002368
2369 /* Don't require hash implementations to behave correctly on a
2370 * zero-length input, which may have an invalid pointer. */
2371 if( input_length == 0 )
2372 return( PSA_SUCCESS );
2373
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002374 switch( operation->alg )
2375 {
John Durkopee4e6602020-11-27 08:48:46 -08002376#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002377 case PSA_ALG_MD2:
2378 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2379 input, input_length );
2380 break;
2381#endif
John Durkopee4e6602020-11-27 08:48:46 -08002382#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002383 case PSA_ALG_MD4:
2384 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2385 input, input_length );
2386 break;
2387#endif
John Durkopee4e6602020-11-27 08:48:46 -08002388#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389 case PSA_ALG_MD5:
2390 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2391 input, input_length );
2392 break;
2393#endif
John Durkopee4e6602020-11-27 08:48:46 -08002394#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002395 case PSA_ALG_RIPEMD160:
2396 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2397 input, input_length );
2398 break;
2399#endif
John Durkopee4e6602020-11-27 08:48:46 -08002400#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401 case PSA_ALG_SHA_1:
2402 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2403 input, input_length );
2404 break;
2405#endif
John Durkop6ca23272020-12-03 06:01:32 -08002406#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002407 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002408 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2409 input, input_length );
2410 break;
2411#endif
2412#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002413 case PSA_ALG_SHA_256:
2414 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2415 input, input_length );
2416 break;
2417#endif
John Durkop6ca23272020-12-03 06:01:32 -08002418#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002419 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002420 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2421 input, input_length );
2422 break;
2423#endif
2424#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002425 case PSA_ALG_SHA_512:
2426 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2427 input, input_length );
2428 break;
2429#endif
2430 default:
John Durkopee4e6602020-11-27 08:48:46 -08002431 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002432 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002433 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002434
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002435 if( ret != 0 )
2436 psa_hash_abort( operation );
2437 return( mbedtls_to_psa_error( ret ) );
2438}
2439
2440psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2441 uint8_t *hash,
2442 size_t hash_size,
2443 size_t *hash_length )
2444{
itayzafrir40835d42018-08-02 13:14:17 +03002445 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002447 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002448
2449 /* Fill the output buffer with something that isn't a valid hash
2450 * (barring an attack on the hash and deliberately-crafted input),
2451 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002452 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002453 /* If hash_size is 0 then hash may be NULL and then the
2454 * call to memset would have undefined behavior. */
2455 if( hash_size != 0 )
2456 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002457
2458 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002459 {
2460 status = PSA_ERROR_BUFFER_TOO_SMALL;
2461 goto exit;
2462 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002463
2464 switch( operation->alg )
2465 {
John Durkopee4e6602020-11-27 08:48:46 -08002466#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002467 case PSA_ALG_MD2:
2468 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2469 break;
2470#endif
John Durkopee4e6602020-11-27 08:48:46 -08002471#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002472 case PSA_ALG_MD4:
2473 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2474 break;
2475#endif
John Durkopee4e6602020-11-27 08:48:46 -08002476#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002477 case PSA_ALG_MD5:
2478 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2479 break;
2480#endif
John Durkopee4e6602020-11-27 08:48:46 -08002481#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002482 case PSA_ALG_RIPEMD160:
2483 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2484 break;
2485#endif
John Durkopee4e6602020-11-27 08:48:46 -08002486#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002487 case PSA_ALG_SHA_1:
2488 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2489 break;
2490#endif
John Durkop6ca23272020-12-03 06:01:32 -08002491#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002492 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002493 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2494 break;
2495#endif
2496#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002497 case PSA_ALG_SHA_256:
2498 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2499 break;
2500#endif
John Durkop6ca23272020-12-03 06:01:32 -08002501#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002502 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002503 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2504 break;
2505#endif
2506#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002507 case PSA_ALG_SHA_512:
2508 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2509 break;
2510#endif
2511 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002512 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002513 }
itayzafrir40835d42018-08-02 13:14:17 +03002514 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002515
itayzafrir40835d42018-08-02 13:14:17 +03002516exit:
2517 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002518 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002519 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002520 return( psa_hash_abort( operation ) );
2521 }
2522 else
2523 {
2524 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002525 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002526 }
2527}
2528
Gilles Peskine2d277862018-06-18 15:41:12 +02002529psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2530 const uint8_t *hash,
2531 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002532{
2533 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2534 size_t actual_hash_length;
2535 psa_status_t status = psa_hash_finish( operation,
2536 actual_hash, sizeof( actual_hash ),
2537 &actual_hash_length );
2538 if( status != PSA_SUCCESS )
2539 return( status );
2540 if( actual_hash_length != hash_length )
2541 return( PSA_ERROR_INVALID_SIGNATURE );
2542 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2543 return( PSA_ERROR_INVALID_SIGNATURE );
2544 return( PSA_SUCCESS );
2545}
2546
Gilles Peskine0a749c82019-11-28 19:33:58 +01002547psa_status_t psa_hash_compute( psa_algorithm_t alg,
2548 const uint8_t *input, size_t input_length,
2549 uint8_t *hash, size_t hash_size,
2550 size_t *hash_length )
2551{
2552 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2554
2555 *hash_length = hash_size;
2556 status = psa_hash_setup( &operation, alg );
2557 if( status != PSA_SUCCESS )
2558 goto exit;
2559 status = psa_hash_update( &operation, input, input_length );
2560 if( status != PSA_SUCCESS )
2561 goto exit;
2562 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2563 if( status != PSA_SUCCESS )
2564 goto exit;
2565
2566exit:
2567 if( status == PSA_SUCCESS )
2568 status = psa_hash_abort( &operation );
2569 else
2570 psa_hash_abort( &operation );
2571 return( status );
2572}
2573
2574psa_status_t psa_hash_compare( psa_algorithm_t alg,
2575 const uint8_t *input, size_t input_length,
2576 const uint8_t *hash, size_t hash_length )
2577{
2578 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2579 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2580
2581 status = psa_hash_setup( &operation, alg );
2582 if( status != PSA_SUCCESS )
2583 goto exit;
2584 status = psa_hash_update( &operation, input, input_length );
2585 if( status != PSA_SUCCESS )
2586 goto exit;
2587 status = psa_hash_verify( &operation, hash, hash_length );
2588 if( status != PSA_SUCCESS )
2589 goto exit;
2590
2591exit:
2592 if( status == PSA_SUCCESS )
2593 status = psa_hash_abort( &operation );
2594 else
2595 psa_hash_abort( &operation );
2596 return( status );
2597}
2598
Gilles Peskineeb35d782019-01-22 17:56:16 +01002599psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2600 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002601{
2602 if( target_operation->alg != 0 )
2603 return( PSA_ERROR_BAD_STATE );
2604
2605 switch( source_operation->alg )
2606 {
2607 case 0:
2608 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002609#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002610 case PSA_ALG_MD2:
2611 mbedtls_md2_clone( &target_operation->ctx.md2,
2612 &source_operation->ctx.md2 );
2613 break;
2614#endif
John Durkopee4e6602020-11-27 08:48:46 -08002615#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002616 case PSA_ALG_MD4:
2617 mbedtls_md4_clone( &target_operation->ctx.md4,
2618 &source_operation->ctx.md4 );
2619 break;
2620#endif
John Durkopee4e6602020-11-27 08:48:46 -08002621#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002622 case PSA_ALG_MD5:
2623 mbedtls_md5_clone( &target_operation->ctx.md5,
2624 &source_operation->ctx.md5 );
2625 break;
2626#endif
John Durkopee4e6602020-11-27 08:48:46 -08002627#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002628 case PSA_ALG_RIPEMD160:
2629 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2630 &source_operation->ctx.ripemd160 );
2631 break;
2632#endif
John Durkopee4e6602020-11-27 08:48:46 -08002633#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002634 case PSA_ALG_SHA_1:
2635 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2636 &source_operation->ctx.sha1 );
2637 break;
2638#endif
John Durkop6ca23272020-12-03 06:01:32 -08002639#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002640 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002641 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2642 &source_operation->ctx.sha256 );
2643 break;
2644#endif
2645#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002646 case PSA_ALG_SHA_256:
2647 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2648 &source_operation->ctx.sha256 );
2649 break;
2650#endif
John Durkop6ca23272020-12-03 06:01:32 -08002651#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002652 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002653 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2654 &source_operation->ctx.sha512 );
2655 break;
2656#endif
2657#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002658 case PSA_ALG_SHA_512:
2659 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2660 &source_operation->ctx.sha512 );
2661 break;
2662#endif
2663 default:
2664 return( PSA_ERROR_NOT_SUPPORTED );
2665 }
2666
2667 target_operation->alg = source_operation->alg;
2668 return( PSA_SUCCESS );
2669}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002670
2671
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002672/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673/* MAC */
2674/****************************************************************/
2675
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002676static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677 psa_algorithm_t alg,
2678 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002679 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002680 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002682 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002683 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002685 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002686 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002687
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2689 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002690 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002692 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002693 mode = MBEDTLS_MODE_STREAM;
2694 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695 case PSA_ALG_CTR:
2696 mode = MBEDTLS_MODE_CTR;
2697 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002698 case PSA_ALG_CFB:
2699 mode = MBEDTLS_MODE_CFB;
2700 break;
2701 case PSA_ALG_OFB:
2702 mode = MBEDTLS_MODE_OFB;
2703 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002704 case PSA_ALG_ECB_NO_PADDING:
2705 mode = MBEDTLS_MODE_ECB;
2706 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002707 case PSA_ALG_CBC_NO_PADDING:
2708 mode = MBEDTLS_MODE_CBC;
2709 break;
2710 case PSA_ALG_CBC_PKCS7:
2711 mode = MBEDTLS_MODE_CBC;
2712 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002713 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002714 mode = MBEDTLS_MODE_CCM;
2715 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002716 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717 mode = MBEDTLS_MODE_GCM;
2718 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002719 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2720 mode = MBEDTLS_MODE_CHACHAPOLY;
2721 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 default:
2723 return( NULL );
2724 }
2725 }
2726 else if( alg == PSA_ALG_CMAC )
2727 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728 else
2729 return( NULL );
2730
2731 switch( key_type )
2732 {
2733 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002734 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735 break;
2736 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002737 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2738 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002740 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002742 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002743 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2744 * but two-key Triple-DES is functionally three-key Triple-DES
2745 * with K1=K3, so that's how we present it to mbedtls. */
2746 if( key_bits == 128 )
2747 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748 break;
2749 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002750 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751 break;
2752 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002753 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002755 case PSA_KEY_TYPE_CHACHA20:
2756 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2757 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002758 default:
2759 return( NULL );
2760 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002761 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002762 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002763
Jaeden Amero23bbb752018-06-26 14:16:54 +01002764 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2765 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002766}
2767
John Durkop6ba40d12020-11-10 08:50:04 -08002768#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002769static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002770{
Gilles Peskine2d277862018-06-18 15:41:12 +02002771 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002772 {
2773 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002774 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002775 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002776 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002777 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002778 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002779 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002780 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002781 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002782 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002783 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002784 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002785 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002786 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002787 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002788 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002789 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002790 return( 128 );
2791 default:
2792 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002793 }
2794}
John Durkop07cc04a2020-11-16 22:08:34 -08002795#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002796
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002797/* Initialize the MAC operation structure. Once this function has been
2798 * called, psa_mac_abort can run and will do the right thing. */
2799static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2800 psa_algorithm_t alg )
2801{
2802 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2803
2804 operation->alg = alg;
2805 operation->key_set = 0;
2806 operation->iv_set = 0;
2807 operation->iv_required = 0;
2808 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002809 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002810
2811#if defined(MBEDTLS_CMAC_C)
2812 if( alg == PSA_ALG_CMAC )
2813 {
2814 operation->iv_required = 0;
2815 mbedtls_cipher_init( &operation->ctx.cmac );
2816 status = PSA_SUCCESS;
2817 }
2818 else
2819#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002820#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002821 if( PSA_ALG_IS_HMAC( operation->alg ) )
2822 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002823 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2824 operation->ctx.hmac.hash_ctx.alg = 0;
2825 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002826 }
2827 else
John Durkopd0321952020-10-29 21:37:36 -07002828#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002829 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002830 if( ! PSA_ALG_IS_MAC( alg ) )
2831 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002832 }
2833
2834 if( status != PSA_SUCCESS )
2835 memset( operation, 0, sizeof( *operation ) );
2836 return( status );
2837}
2838
John Durkop6ba40d12020-11-10 08:50:04 -08002839#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002840static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2841{
Gilles Peskine3f108122018-12-07 18:14:53 +01002842 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002843 return( psa_hash_abort( &hmac->hash_ctx ) );
2844}
John Durkop6ba40d12020-11-10 08:50:04 -08002845#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002846
Gilles Peskine8c9def32018-02-08 10:02:12 +01002847psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2848{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002849 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002851 /* The object has (apparently) been initialized but it is not
2852 * in use. It's ok to call abort on such an object, and there's
2853 * nothing to do. */
2854 return( PSA_SUCCESS );
2855 }
2856 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002858 if( operation->alg == PSA_ALG_CMAC )
2859 {
2860 mbedtls_cipher_free( &operation->ctx.cmac );
2861 }
2862 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002863#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002864#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002865 if( PSA_ALG_IS_HMAC( operation->alg ) )
2866 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002867 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002868 }
2869 else
John Durkopd0321952020-10-29 21:37:36 -07002870#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002871 {
2872 /* Sanity check (shouldn't happen: operation->alg should
2873 * always have been initialized to a valid value). */
2874 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875 }
Moran Peker41deec42018-04-04 15:43:05 +03002876
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877 operation->alg = 0;
2878 operation->key_set = 0;
2879 operation->iv_set = 0;
2880 operation->iv_required = 0;
2881 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002882 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002883
Gilles Peskine8c9def32018-02-08 10:02:12 +01002884 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002885
2886bad_state:
2887 /* If abort is called on an uninitialized object, we can't trust
2888 * anything. Wipe the object in case it contains confidential data.
2889 * This may result in a memory leak if a pointer gets overwritten,
2890 * but it's too late to do anything about this. */
2891 memset( operation, 0, sizeof( *operation ) );
2892 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002893}
2894
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002895#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002896static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002897 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002898 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002899 const mbedtls_cipher_info_t *cipher_info )
2900{
Janos Follath24eed8d2019-11-22 13:21:35 +00002901 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002902
2903 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002904
2905 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2906 if( ret != 0 )
2907 return( ret );
2908
2909 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002910 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002911 key_bits );
2912 return( ret );
2913}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002914#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002915
John Durkop6ba40d12020-11-10 08:50:04 -08002916#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002917static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2918 const uint8_t *key,
2919 size_t key_length,
2920 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002921{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002922 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002923 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002924 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002925 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002926 psa_status_t status;
2927
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002928 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2929 * overflow below. This should never trigger if the hash algorithm
2930 * is implemented correctly. */
2931 /* The size checks against the ipad and opad buffers cannot be written
2932 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2933 * because that triggers -Wlogical-op on GCC 7.3. */
2934 if( block_size > sizeof( ipad ) )
2935 return( PSA_ERROR_NOT_SUPPORTED );
2936 if( block_size > sizeof( hmac->opad ) )
2937 return( PSA_ERROR_NOT_SUPPORTED );
2938 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002939 return( PSA_ERROR_NOT_SUPPORTED );
2940
Gilles Peskined223b522018-06-11 18:12:58 +02002941 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002942 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002943 status = psa_hash_compute( hash_alg, key, key_length,
2944 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002945 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002946 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002947 }
Gilles Peskine96889972018-07-12 17:07:03 +02002948 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2949 * but it is permitted. It is common when HMAC is used in HKDF, for
2950 * example. Don't call `memcpy` in the 0-length because `key` could be
2951 * an invalid pointer which would make the behavior undefined. */
2952 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002953 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002954
Gilles Peskined223b522018-06-11 18:12:58 +02002955 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2956 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002957 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002958 ipad[i] ^= 0x36;
2959 memset( ipad + key_length, 0x36, block_size - key_length );
2960
2961 /* Copy the key material from ipad to opad, flipping the requisite bits,
2962 * and filling the rest of opad with the requisite constant. */
2963 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002964 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2965 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002966
Gilles Peskine01126fa2018-07-12 17:04:55 +02002967 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002968 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002969 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002970
Gilles Peskine01126fa2018-07-12 17:04:55 +02002971 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002972
2973cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002974 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002975
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002976 return( status );
2977}
John Durkop6ba40d12020-11-10 08:50:04 -08002978#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002979
Gilles Peskine89167cb2018-07-08 20:12:23 +02002980static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002981 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002982 psa_algorithm_t alg,
2983 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002984{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002985 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002986 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002987 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002988 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002989 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002990 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002991 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002992 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002993
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002994 /* A context must be freshly initialized before it can be set up. */
2995 if( operation->alg != 0 )
2996 {
2997 return( PSA_ERROR_BAD_STATE );
2998 }
2999
Gilles Peskined911eb72018-08-14 15:18:45 +02003000 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003001 if( status != PSA_SUCCESS )
3002 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003003 if( is_sign )
3004 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003005
Ronald Cron5c522922020-11-14 16:35:34 +01003006 status = psa_get_and_lock_transparent_key_slot_with_policy(
3007 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003008 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003009 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003010 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003011
Gilles Peskine8c9def32018-02-08 10:02:12 +01003012#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003013 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003014 {
3015 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003016 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003017 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003018 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003019 if( cipher_info == NULL )
3020 {
3021 status = PSA_ERROR_NOT_SUPPORTED;
3022 goto exit;
3023 }
3024 operation->mac_size = cipher_info->block_size;
3025 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3026 status = mbedtls_to_psa_error( ret );
3027 }
3028 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003029#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003030#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003031 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003032 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003033 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003034 if( hash_alg == 0 )
3035 {
3036 status = PSA_ERROR_NOT_SUPPORTED;
3037 goto exit;
3038 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003039
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003040 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003041 /* Sanity check. This shouldn't fail on a valid configuration. */
3042 if( operation->mac_size == 0 ||
3043 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3044 {
3045 status = PSA_ERROR_NOT_SUPPORTED;
3046 goto exit;
3047 }
3048
Gilles Peskine8e338702019-07-30 20:06:31 +02003049 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003050 {
3051 status = PSA_ERROR_INVALID_ARGUMENT;
3052 goto exit;
3053 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003054
Gilles Peskine01126fa2018-07-12 17:04:55 +02003055 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003056 slot->key.data,
3057 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003058 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003059 }
3060 else
John Durkopd0321952020-10-29 21:37:36 -07003061#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003062 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003063 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003064 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003065 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003066
Gilles Peskined911eb72018-08-14 15:18:45 +02003067 if( truncated == 0 )
3068 {
3069 /* The "normal" case: untruncated algorithm. Nothing to do. */
3070 }
3071 else if( truncated < 4 )
3072 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003073 /* A very short MAC is too short for security since it can be
3074 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3075 * so we make this our minimum, even though 32 bits is still
3076 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003077 status = PSA_ERROR_NOT_SUPPORTED;
3078 }
3079 else if( truncated > operation->mac_size )
3080 {
3081 /* It's impossible to "truncate" to a larger length. */
3082 status = PSA_ERROR_INVALID_ARGUMENT;
3083 }
3084 else
3085 operation->mac_size = truncated;
3086
Gilles Peskinefbfac682018-07-08 20:51:54 +02003087exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003088 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003090 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003092 else
3093 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003094 operation->key_set = 1;
3095 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003096
Ronald Cron5c522922020-11-14 16:35:34 +01003097 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003098
Ronald Cron5c522922020-11-14 16:35:34 +01003099 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003100}
3101
Gilles Peskine89167cb2018-07-08 20:12:23 +02003102psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003103 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003104 psa_algorithm_t alg )
3105{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003106 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003107}
3108
3109psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003110 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003111 psa_algorithm_t alg )
3112{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003113 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003114}
3115
Gilles Peskine8c9def32018-02-08 10:02:12 +01003116psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3117 const uint8_t *input,
3118 size_t input_length )
3119{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003120 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003121 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003122 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003123 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003124 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125 operation->has_input = 1;
3126
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003128 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003129 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003130 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3131 input, input_length );
3132 status = mbedtls_to_psa_error( ret );
3133 }
3134 else
3135#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003136#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003137 if( PSA_ALG_IS_HMAC( operation->alg ) )
3138 {
3139 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3140 input_length );
3141 }
3142 else
John Durkopd0321952020-10-29 21:37:36 -07003143#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003144 {
3145 /* This shouldn't happen if `operation` was initialized by
3146 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003147 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003148 }
3149
Gilles Peskinefbfac682018-07-08 20:51:54 +02003150 if( status != PSA_SUCCESS )
3151 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003152 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003153}
3154
John Durkop6ba40d12020-11-10 08:50:04 -08003155#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003156static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3157 uint8_t *mac,
3158 size_t mac_size )
3159{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003160 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003161 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3162 size_t hash_size = 0;
3163 size_t block_size = psa_get_hash_block_size( hash_alg );
3164 psa_status_t status;
3165
Gilles Peskine01126fa2018-07-12 17:04:55 +02003166 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3167 if( status != PSA_SUCCESS )
3168 return( status );
3169 /* From here on, tmp needs to be wiped. */
3170
3171 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3172 if( status != PSA_SUCCESS )
3173 goto exit;
3174
3175 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3176 if( status != PSA_SUCCESS )
3177 goto exit;
3178
3179 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3180 if( status != PSA_SUCCESS )
3181 goto exit;
3182
Gilles Peskined911eb72018-08-14 15:18:45 +02003183 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3184 if( status != PSA_SUCCESS )
3185 goto exit;
3186
3187 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003188
3189exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003190 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003191 return( status );
3192}
John Durkop6ba40d12020-11-10 08:50:04 -08003193#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003194
mohammad16036df908f2018-04-02 08:34:15 -07003195static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003196 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003197 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003198{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003199 if( ! operation->key_set )
3200 return( PSA_ERROR_BAD_STATE );
3201 if( operation->iv_required && ! operation->iv_set )
3202 return( PSA_ERROR_BAD_STATE );
3203
Gilles Peskine8c9def32018-02-08 10:02:12 +01003204 if( mac_size < operation->mac_size )
3205 return( PSA_ERROR_BUFFER_TOO_SMALL );
3206
Gilles Peskine8c9def32018-02-08 10:02:12 +01003207#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003208 if( operation->alg == PSA_ALG_CMAC )
3209 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003210 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003211 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3212 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003213 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003214 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003215 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003216 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003217 else
3218#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003219#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003220 if( PSA_ALG_IS_HMAC( operation->alg ) )
3221 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003222 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003223 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003224 }
3225 else
John Durkopd0321952020-10-29 21:37:36 -07003226#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003227 {
3228 /* This shouldn't happen if `operation` was initialized by
3229 * a setup function. */
3230 return( PSA_ERROR_BAD_STATE );
3231 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003232}
3233
Gilles Peskineacd4be32018-07-08 19:56:25 +02003234psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3235 uint8_t *mac,
3236 size_t mac_size,
3237 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003238{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003239 psa_status_t status;
3240
Jaeden Amero252ef282019-02-15 14:05:35 +00003241 if( operation->alg == 0 )
3242 {
3243 return( PSA_ERROR_BAD_STATE );
3244 }
3245
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003246 /* Fill the output buffer with something that isn't a valid mac
3247 * (barring an attack on the mac and deliberately-crafted input),
3248 * in case the caller doesn't check the return status properly. */
3249 *mac_length = mac_size;
3250 /* If mac_size is 0 then mac may be NULL and then the
3251 * call to memset would have undefined behavior. */
3252 if( mac_size != 0 )
3253 memset( mac, '!', mac_size );
3254
Gilles Peskine89167cb2018-07-08 20:12:23 +02003255 if( ! operation->is_sign )
3256 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003257 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003258 }
mohammad16036df908f2018-04-02 08:34:15 -07003259
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003260 status = psa_mac_finish_internal( operation, mac, mac_size );
3261
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003262 if( status == PSA_SUCCESS )
3263 {
3264 status = psa_mac_abort( operation );
3265 if( status == PSA_SUCCESS )
3266 *mac_length = operation->mac_size;
3267 else
3268 memset( mac, '!', mac_size );
3269 }
3270 else
3271 psa_mac_abort( operation );
3272 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003273}
3274
Gilles Peskineacd4be32018-07-08 19:56:25 +02003275psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3276 const uint8_t *mac,
3277 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003278{
Gilles Peskine828ed142018-06-18 23:25:51 +02003279 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003280 psa_status_t status;
3281
Jaeden Amero252ef282019-02-15 14:05:35 +00003282 if( operation->alg == 0 )
3283 {
3284 return( PSA_ERROR_BAD_STATE );
3285 }
3286
Gilles Peskine89167cb2018-07-08 20:12:23 +02003287 if( operation->is_sign )
3288 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003289 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003290 }
3291 if( operation->mac_size != mac_length )
3292 {
3293 status = PSA_ERROR_INVALID_SIGNATURE;
3294 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003295 }
mohammad16036df908f2018-04-02 08:34:15 -07003296
3297 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003298 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003299 if( status != PSA_SUCCESS )
3300 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003301
3302 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3303 status = PSA_ERROR_INVALID_SIGNATURE;
3304
3305cleanup:
3306 if( status == PSA_SUCCESS )
3307 status = psa_mac_abort( operation );
3308 else
3309 psa_mac_abort( operation );
3310
Gilles Peskine3f108122018-12-07 18:14:53 +01003311 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003312
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003313 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003314}
3315
3316
Gilles Peskine20035e32018-02-03 22:44:14 +01003317
Gilles Peskine20035e32018-02-03 22:44:14 +01003318/****************************************************************/
3319/* Asymmetric cryptography */
3320/****************************************************************/
3321
John Durkop6ba40d12020-11-10 08:50:04 -08003322#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3323 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003324/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003325 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003326static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3327 size_t hash_length,
3328 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003329{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003330 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003331 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003332 *md_alg = mbedtls_md_get_type( md_info );
3333
3334 /* The Mbed TLS RSA module uses an unsigned int for hash length
3335 * parameters. Validate that it fits so that we don't risk an
3336 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003337#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003338 if( hash_length > UINT_MAX )
3339 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003340#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003341
John Durkop0e005192020-10-31 22:06:54 -07003342#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003343 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3344 * must be correct. */
3345 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3346 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003347 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003348 if( md_info == NULL )
3349 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003350 if( mbedtls_md_get_size( md_info ) != hash_length )
3351 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003352 }
John Durkop0e005192020-10-31 22:06:54 -07003353#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003354
John Durkop0e005192020-10-31 22:06:54 -07003355#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003356 /* PSS requires a hash internally. */
3357 if( PSA_ALG_IS_RSA_PSS( alg ) )
3358 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003359 if( md_info == NULL )
3360 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003361 }
John Durkop0e005192020-10-31 22:06:54 -07003362#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003363
Gilles Peskine61b91d42018-06-08 16:09:36 +02003364 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003365}
3366
Gilles Peskine2b450e32018-06-27 15:42:46 +02003367static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3368 psa_algorithm_t alg,
3369 const uint8_t *hash,
3370 size_t hash_length,
3371 uint8_t *signature,
3372 size_t signature_size,
3373 size_t *signature_length )
3374{
3375 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003377 mbedtls_md_type_t md_alg;
3378
3379 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3380 if( status != PSA_SUCCESS )
3381 return( status );
3382
Gilles Peskine630a18a2018-06-29 17:49:35 +02003383 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003384 return( PSA_ERROR_BUFFER_TOO_SMALL );
3385
John Durkop0e005192020-10-31 22:06:54 -07003386#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003387 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3388 {
3389 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3390 MBEDTLS_MD_NONE );
3391 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003392 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003393 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003394 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003395 md_alg,
3396 (unsigned int) hash_length,
3397 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003398 signature );
3399 }
3400 else
John Durkop0e005192020-10-31 22:06:54 -07003401#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3402#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003403 if( PSA_ALG_IS_RSA_PSS( alg ) )
3404 {
3405 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3406 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003407 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003408 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003409 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003410 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003411 (unsigned int) hash_length,
3412 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003413 signature );
3414 }
3415 else
John Durkop0e005192020-10-31 22:06:54 -07003416#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003417 {
3418 return( PSA_ERROR_INVALID_ARGUMENT );
3419 }
3420
3421 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003422 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003423 return( mbedtls_to_psa_error( ret ) );
3424}
3425
3426static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3427 psa_algorithm_t alg,
3428 const uint8_t *hash,
3429 size_t hash_length,
3430 const uint8_t *signature,
3431 size_t signature_length )
3432{
3433 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003435 mbedtls_md_type_t md_alg;
3436
3437 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3438 if( status != PSA_SUCCESS )
3439 return( status );
3440
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003441 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3442 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003443
John Durkop0e005192020-10-31 22:06:54 -07003444#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003445 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3446 {
3447 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3448 MBEDTLS_MD_NONE );
3449 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003450 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003451 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003452 MBEDTLS_RSA_PUBLIC,
3453 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003454 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003455 hash,
3456 signature );
3457 }
3458 else
John Durkop0e005192020-10-31 22:06:54 -07003459#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3460#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003461 if( PSA_ALG_IS_RSA_PSS( alg ) )
3462 {
3463 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3464 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003465 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003466 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003467 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003468 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003469 (unsigned int) hash_length,
3470 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003471 signature );
3472 }
3473 else
John Durkop0e005192020-10-31 22:06:54 -07003474#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003475 {
3476 return( PSA_ERROR_INVALID_ARGUMENT );
3477 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003478
3479 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3480 * the rest of the signature is invalid". This has little use in
3481 * practice and PSA doesn't report this distinction. */
3482 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3483 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003484 return( mbedtls_to_psa_error( ret ) );
3485}
John Durkop6ba40d12020-11-10 08:50:04 -08003486#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3487 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003488
John Durkop6ba40d12020-11-10 08:50:04 -08003489#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3490 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003491/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3492 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3493 * (even though these functions don't modify it). */
3494static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3495 psa_algorithm_t alg,
3496 const uint8_t *hash,
3497 size_t hash_length,
3498 uint8_t *signature,
3499 size_t signature_size,
3500 size_t *signature_length )
3501{
Janos Follath24eed8d2019-11-22 13:21:35 +00003502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003503 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003504 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003505 mbedtls_mpi_init( &r );
3506 mbedtls_mpi_init( &s );
3507
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003508 if( signature_size < 2 * curve_bytes )
3509 {
3510 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3511 goto cleanup;
3512 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003513
John Durkop0ea39e02020-10-13 19:58:20 -07003514#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003515 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3516 {
3517 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3518 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3519 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003520 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3521 &ecp->d, hash,
3522 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003523 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003524 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003525 }
3526 else
John Durkop0ea39e02020-10-13 19:58:20 -07003527#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003528 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003529 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003530 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3531 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003532 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003533 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003534 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003535
3536 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3537 signature,
3538 curve_bytes ) );
3539 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3540 signature + curve_bytes,
3541 curve_bytes ) );
3542
3543cleanup:
3544 mbedtls_mpi_free( &r );
3545 mbedtls_mpi_free( &s );
3546 if( ret == 0 )
3547 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003548 return( mbedtls_to_psa_error( ret ) );
3549}
3550
3551static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3552 const uint8_t *hash,
3553 size_t hash_length,
3554 const uint8_t *signature,
3555 size_t signature_length )
3556{
Janos Follath24eed8d2019-11-22 13:21:35 +00003557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003558 mbedtls_mpi r, s;
3559 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3560 mbedtls_mpi_init( &r );
3561 mbedtls_mpi_init( &s );
3562
3563 if( signature_length != 2 * curve_bytes )
3564 return( PSA_ERROR_INVALID_SIGNATURE );
3565
3566 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3567 signature,
3568 curve_bytes ) );
3569 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3570 signature + curve_bytes,
3571 curve_bytes ) );
3572
Steven Cooremanacda8342020-07-24 23:09:52 +02003573 /* Check whether the public part is loaded. If not, load it. */
3574 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3575 {
3576 MBEDTLS_MPI_CHK(
3577 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003578 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003579 }
3580
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003581 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3582 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003583
3584cleanup:
3585 mbedtls_mpi_free( &r );
3586 mbedtls_mpi_free( &s );
3587 return( mbedtls_to_psa_error( ret ) );
3588}
John Durkop6ba40d12020-11-10 08:50:04 -08003589#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3590 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003591
Ronald Croncf56a0a2020-08-04 09:51:30 +02003592psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003593 psa_algorithm_t alg,
3594 const uint8_t *hash,
3595 size_t hash_length,
3596 uint8_t *signature,
3597 size_t signature_size,
3598 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003599{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003601 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003602 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003603
3604 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003605 /* Immediately reject a zero-length signature buffer. This guarantees
3606 * that signature must be a valid pointer. (On the other hand, the hash
3607 * buffer can in principle be empty since it doesn't actually have
3608 * to be a hash.) */
3609 if( signature_size == 0 )
3610 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003611
Ronald Cron5c522922020-11-14 16:35:34 +01003612 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3613 PSA_KEY_USAGE_SIGN_HASH,
3614 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003615 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003616 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003617 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003618 {
3619 status = PSA_ERROR_INVALID_ARGUMENT;
3620 goto exit;
3621 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003622
Steven Cooremancd84cb42020-07-16 20:28:36 +02003623 /* Try any of the available accelerators first */
3624 status = psa_driver_wrapper_sign_hash( slot,
3625 alg,
3626 hash,
3627 hash_length,
3628 signature,
3629 signature_size,
3630 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003631 if( status != PSA_ERROR_NOT_SUPPORTED ||
3632 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003633 goto exit;
3634
Steven Cooreman7a250572020-07-17 16:43:05 +02003635 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003636#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3637 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003638 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003639 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003640 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003641
Ronald Cron90857082020-11-25 14:58:33 +01003642 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3643 slot->key.data,
3644 slot->key.bytes,
3645 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003646 if( status != PSA_SUCCESS )
3647 goto exit;
3648
Steven Cooremana2371e52020-07-28 14:30:39 +02003649 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003650 alg,
3651 hash, hash_length,
3652 signature, signature_size,
3653 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003654
Steven Cooremana2371e52020-07-28 14:30:39 +02003655 mbedtls_rsa_free( rsa );
3656 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003657 }
3658 else
John Durkop6ba40d12020-11-10 08:50:04 -08003659#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3660 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003661 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003662 {
John Durkop9814fa22020-11-04 12:28:15 -08003663#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3664 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003665 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003666#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003667 PSA_ALG_IS_ECDSA( alg )
3668#else
3669 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3670#endif
3671 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003672 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003673 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003674 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3675 slot->key.data,
3676 slot->key.bytes,
3677 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003678 if( status != PSA_SUCCESS )
3679 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003680 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003681 alg,
3682 hash, hash_length,
3683 signature, signature_size,
3684 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003685 mbedtls_ecp_keypair_free( ecp );
3686 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003687 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003688 else
John Durkop9814fa22020-11-04 12:28:15 -08003689#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3690 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003691 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003692 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003693 }
itayzafrir5c753392018-05-08 11:18:38 +03003694 }
3695 else
itayzafrir5c753392018-05-08 11:18:38 +03003696 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003697 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003698 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003699
3700exit:
3701 /* Fill the unused part of the output buffer (the whole buffer on error,
3702 * the trailing part on success) with something that isn't a valid mac
3703 * (barring an attack on the mac and deliberately-crafted input),
3704 * in case the caller doesn't check the return status properly. */
3705 if( status == PSA_SUCCESS )
3706 memset( signature + *signature_length, '!',
3707 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003708 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003709 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003710 /* If signature_size is 0 then we have nothing to do. We must not call
3711 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003712
Ronald Cron5c522922020-11-14 16:35:34 +01003713 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003714
Ronald Cron5c522922020-11-14 16:35:34 +01003715 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003716}
3717
Ronald Croncf56a0a2020-08-04 09:51:30 +02003718psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003719 psa_algorithm_t alg,
3720 const uint8_t *hash,
3721 size_t hash_length,
3722 const uint8_t *signature,
3723 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003724{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003725 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003726 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003727 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003728
Ronald Cron5c522922020-11-14 16:35:34 +01003729 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3730 PSA_KEY_USAGE_VERIFY_HASH,
3731 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003732 if( status != PSA_SUCCESS )
3733 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003734
Steven Cooreman55ae2172020-07-17 19:46:15 +02003735 /* Try any of the available accelerators first */
3736 status = psa_driver_wrapper_verify_hash( slot,
3737 alg,
3738 hash,
3739 hash_length,
3740 signature,
3741 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003742 if( status != PSA_ERROR_NOT_SUPPORTED ||
3743 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003744 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003745
John Durkop6ba40d12020-11-10 08:50:04 -08003746#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3747 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003748 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003749 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003750 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003751
Ronald Cron90857082020-11-25 14:58:33 +01003752 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3753 slot->key.data,
3754 slot->key.bytes,
3755 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003756 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003757 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003758
Steven Cooremana2371e52020-07-28 14:30:39 +02003759 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003760 alg,
3761 hash, hash_length,
3762 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003763 mbedtls_rsa_free( rsa );
3764 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003765 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003766 }
3767 else
John Durkop6ba40d12020-11-10 08:50:04 -08003768#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3769 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003770 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003771 {
John Durkop9814fa22020-11-04 12:28:15 -08003772#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3773 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003774 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003775 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003776 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003777 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3778 slot->key.data,
3779 slot->key.bytes,
3780 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003781 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003782 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003783 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003784 hash, hash_length,
3785 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003786 mbedtls_ecp_keypair_free( ecp );
3787 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003788 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003789 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003790 else
John Durkop9814fa22020-11-04 12:28:15 -08003791#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3792 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003793 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003794 status = PSA_ERROR_INVALID_ARGUMENT;
3795 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003796 }
itayzafrir5c753392018-05-08 11:18:38 +03003797 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003798 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003799 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003800 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003801 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003802
3803exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003804 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003805
Ronald Cron5c522922020-11-14 16:35:34 +01003806 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003807}
3808
John Durkop0e005192020-10-31 22:06:54 -07003809#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003810static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3811 mbedtls_rsa_context *rsa )
3812{
3813 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3814 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3815 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3816 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3817}
John Durkop0e005192020-10-31 22:06:54 -07003818#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003819
Ronald Croncf56a0a2020-08-04 09:51:30 +02003820psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003821 psa_algorithm_t alg,
3822 const uint8_t *input,
3823 size_t input_length,
3824 const uint8_t *salt,
3825 size_t salt_length,
3826 uint8_t *output,
3827 size_t output_size,
3828 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003829{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003831 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003832 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003833
Darryl Green5cc689a2018-07-24 15:34:10 +01003834 (void) input;
3835 (void) input_length;
3836 (void) salt;
3837 (void) output;
3838 (void) output_size;
3839
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003840 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003841
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003842 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3843 return( PSA_ERROR_INVALID_ARGUMENT );
3844
Ronald Cron5c522922020-11-14 16:35:34 +01003845 status = psa_get_and_lock_transparent_key_slot_with_policy(
3846 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003847 if( status != PSA_SUCCESS )
3848 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003849 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3850 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003851 {
3852 status = PSA_ERROR_INVALID_ARGUMENT;
3853 goto exit;
3854 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003855
John Durkop6ba40d12020-11-10 08:50:04 -08003856#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3857 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003858 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003859 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003860 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003861 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3862 slot->key.data,
3863 slot->key.bytes,
3864 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003865 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003866 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003867
3868 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003869 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003870 status = PSA_ERROR_BUFFER_TOO_SMALL;
3871 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003872 }
John Durkop0e005192020-10-31 22:06:54 -07003873#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003874 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003876 status = mbedtls_to_psa_error(
3877 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003878 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003879 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003880 MBEDTLS_RSA_PUBLIC,
3881 input_length,
3882 input,
3883 output ) );
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_encrypt( 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_PUBLIC,
3896 salt, salt_length,
3897 input_length,
3898 input,
3899 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900 }
3901 else
John Durkop0e005192020-10-31 22:06:54 -07003902#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003904 status = PSA_ERROR_INVALID_ARGUMENT;
3905 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003907rsa_exit:
3908 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003909 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003910
Steven Cooremana2371e52020-07-28 14:30:39 +02003911 mbedtls_rsa_free( rsa );
3912 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003914 else
John Durkop9814fa22020-11-04 12:28:15 -08003915#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003916 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003917 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003918 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003919 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003920
3921exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003922 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003923
Ronald Cron5c522922020-11-14 16:35:34 +01003924 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003925}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003926
Ronald Croncf56a0a2020-08-04 09:51:30 +02003927psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003928 psa_algorithm_t alg,
3929 const uint8_t *input,
3930 size_t input_length,
3931 const uint8_t *salt,
3932 size_t salt_length,
3933 uint8_t *output,
3934 size_t output_size,
3935 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003936{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003937 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003938 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003939 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003940
Darryl Green5cc689a2018-07-24 15:34:10 +01003941 (void) input;
3942 (void) input_length;
3943 (void) salt;
3944 (void) output;
3945 (void) output_size;
3946
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003947 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003948
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003949 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3950 return( PSA_ERROR_INVALID_ARGUMENT );
3951
Ronald Cron5c522922020-11-14 16:35:34 +01003952 status = psa_get_and_lock_transparent_key_slot_with_policy(
3953 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003954 if( status != PSA_SUCCESS )
3955 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003956 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003957 {
3958 status = PSA_ERROR_INVALID_ARGUMENT;
3959 goto exit;
3960 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961
John Durkop6ba40d12020-11-10 08:50:04 -08003962#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3963 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003964 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003965 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003966 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003967 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3968 slot->key.data,
3969 slot->key.bytes,
3970 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003971 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003972 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003973
Steven Cooremana2371e52020-07-28 14:30:39 +02003974 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003975 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003976 status = PSA_ERROR_INVALID_ARGUMENT;
3977 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003978 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979
John Durkop0e005192020-10-31 22:06:54 -07003980#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003981 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003982 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003983 status = mbedtls_to_psa_error(
3984 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003985 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003986 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003987 MBEDTLS_RSA_PRIVATE,
3988 output_length,
3989 input,
3990 output,
3991 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003992 }
3993 else
John Durkop0e005192020-10-31 22:06:54 -07003994#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3995#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003996 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003998 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003999 status = mbedtls_to_psa_error(
4000 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004001 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004002 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004003 MBEDTLS_RSA_PRIVATE,
4004 salt, salt_length,
4005 output_length,
4006 input,
4007 output,
4008 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004009 }
4010 else
John Durkop0e005192020-10-31 22:06:54 -07004011#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004013 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004014 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004015
Steven Cooreman4fed4552020-08-03 14:46:03 +02004016rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004017 mbedtls_rsa_free( rsa );
4018 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004019 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004020 else
John Durkop6ba40d12020-11-10 08:50:04 -08004021#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4022 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004023 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004024 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004025 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004026
4027exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004028 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004029
Ronald Cron5c522922020-11-14 16:35:34 +01004030 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004031}
Gilles Peskine20035e32018-02-03 22:44:14 +01004032
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004033
4034
mohammad1603503973b2018-03-12 15:59:30 +02004035/****************************************************************/
4036/* Symmetric cryptography */
4037/****************************************************************/
4038
Gilles Peskinee553c652018-06-04 16:22:46 +02004039static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004040 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004041 psa_algorithm_t alg,
4042 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004043{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004044 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004045 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004046 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004047 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004048 size_t key_bits;
4049 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004050 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4051 PSA_KEY_USAGE_ENCRYPT :
4052 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004053
Steven Cooremand3feccd2020-09-01 15:56:14 +02004054 /* A context must be freshly initialized before it can be set up. */
4055 if( operation->alg != 0 )
4056 return( PSA_ERROR_BAD_STATE );
4057
Steven Cooremana07b9972020-09-10 14:54:14 +02004058 /* The requested algorithm must be one that can be processed by cipher. */
4059 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004060 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004061
Steven Cooremanef8575e2020-09-11 11:44:50 +02004062 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004063 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004064 if( status != PSA_SUCCESS )
4065 goto exit;
4066
4067 /* Initialize the operation struct members, except for alg. The alg member
4068 * is used to indicate to psa_cipher_abort that there are resources to free,
4069 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004070 operation->key_set = 0;
4071 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004072 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004073 operation->iv_size = 0;
4074 operation->block_size = 0;
4075 if( alg == PSA_ALG_ECB_NO_PADDING )
4076 operation->iv_required = 0;
4077 else
4078 operation->iv_required = 1;
4079
Steven Cooremana07b9972020-09-10 14:54:14 +02004080 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004081 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004082 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004083 slot,
4084 alg );
4085 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004086 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004087 slot,
4088 alg );
4089
Steven Cooremanef8575e2020-09-11 11:44:50 +02004090 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004091 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004092 /* Once the driver context is initialised, it needs to be freed using
4093 * psa_cipher_abort. Indicate this through setting alg. */
4094 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004095 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004096
Steven Cooremand3feccd2020-09-01 15:56:14 +02004097 if( status != PSA_ERROR_NOT_SUPPORTED ||
4098 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4099 goto exit;
4100
Steven Cooremana07b9972020-09-10 14:54:14 +02004101 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004102 * available for the given algorithm & key. */
4103 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004104
Steven Cooremana07b9972020-09-10 14:54:14 +02004105 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004106 * psa_cipher_abort. Indicate there is something to be freed through setting
4107 * alg, and indicate the operation is being done using mbedtls crypto through
4108 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004109 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004110 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004111
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004112 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004113 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004114 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004115 {
4116 status = PSA_ERROR_NOT_SUPPORTED;
4117 goto exit;
4118 }
mohammad1603503973b2018-03-12 15:59:30 +02004119
mohammad1603503973b2018-03-12 15:59:30 +02004120 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004121 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004122 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004123
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004124#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004125 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004126 {
4127 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004128 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004129 memcpy( keys, slot->key.data, 16 );
4130 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004131 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4132 keys,
4133 192, cipher_operation );
4134 }
4135 else
4136#endif
4137 {
4138 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004139 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004140 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004141 }
Moran Peker41deec42018-04-04 15:43:05 +03004142 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004143 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004144
mohammad16038481e742018-03-18 13:57:31 +02004145#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004146 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004147 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004148 case PSA_ALG_CBC_NO_PADDING:
4149 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4150 MBEDTLS_PADDING_NONE );
4151 break;
4152 case PSA_ALG_CBC_PKCS7:
4153 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4154 MBEDTLS_PADDING_PKCS7 );
4155 break;
4156 default:
4157 /* The algorithm doesn't involve padding. */
4158 ret = 0;
4159 break;
4160 }
4161 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004162 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004163#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4164
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004165 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004166 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004167 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4168 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004169 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004170 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004171 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004172#if defined(MBEDTLS_CHACHA20_C)
4173 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004174 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004175 operation->iv_size = 12;
4176#endif
mohammad1603503973b2018-03-12 15:59:30 +02004177
Steven Cooreman7df02922020-09-09 15:28:49 +02004178 status = PSA_SUCCESS;
4179
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004180exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004181 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004182 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004183 if( status == PSA_SUCCESS )
4184 {
4185 /* Update operation flags for both driver and software implementations */
4186 operation->key_set = 1;
4187 }
4188 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004189 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004190
Ronald Cron5c522922020-11-14 16:35:34 +01004191 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004192
Ronald Cron5c522922020-11-14 16:35:34 +01004193 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004194}
4195
Gilles Peskinefe119512018-07-08 21:39:34 +02004196psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004197 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004198 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004199{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004200 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004201}
4202
Gilles Peskinefe119512018-07-08 21:39:34 +02004203psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004204 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004205 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004206{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004207 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004208}
4209
Gilles Peskinefe119512018-07-08 21:39:34 +02004210psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004211 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004212 size_t iv_size,
4213 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004214{
itayzafrir534bd7c2018-08-02 13:56:32 +03004215 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004217 if( operation->iv_set || ! operation->iv_required )
4218 {
4219 return( PSA_ERROR_BAD_STATE );
4220 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004221
Steven Cooremanef8575e2020-09-11 11:44:50 +02004222 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004223 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004224 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004225 iv,
4226 iv_size,
4227 iv_length );
4228 goto exit;
4229 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004230
Moran Peker41deec42018-04-04 15:43:05 +03004231 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004232 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004233 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004234 goto exit;
4235 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004236 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004237 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004238 if( ret != 0 )
4239 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004240 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004241 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004242 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004243
mohammad16038481e742018-03-18 13:57:31 +02004244 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004245 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004246
Moran Peker395db872018-05-31 14:07:14 +03004247exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004248 if( status == PSA_SUCCESS )
4249 operation->iv_set = 1;
4250 else
Moran Peker395db872018-05-31 14:07:14 +03004251 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004252 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004253}
4254
Gilles Peskinefe119512018-07-08 21:39:34 +02004255psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004256 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004257 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004258{
itayzafrir534bd7c2018-08-02 13:56:32 +03004259 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004261 if( operation->iv_set || ! operation->iv_required )
4262 {
4263 return( PSA_ERROR_BAD_STATE );
4264 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004265
Steven Cooremanef8575e2020-09-11 11:44:50 +02004266 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004267 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004268 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004269 iv,
4270 iv_length );
4271 goto exit;
4272 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004273
Moran Pekera28258c2018-05-29 16:25:04 +03004274 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004275 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004276 status = PSA_ERROR_INVALID_ARGUMENT;
4277 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004278 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004279 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4280 status = mbedtls_to_psa_error( ret );
4281exit:
4282 if( status == PSA_SUCCESS )
4283 operation->iv_set = 1;
4284 else
mohammad1603503973b2018-03-12 15:59:30 +02004285 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004286 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004287}
4288
Steven Cooreman177deba2020-09-07 17:14:14 +02004289/* Process input for which the algorithm is set to ECB mode. This requires
4290 * manual processing, since the PSA API is defined as being able to process
4291 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4292 * underlying mbedtls_cipher_update only takes full blocks. */
4293static psa_status_t psa_cipher_update_ecb_internal(
4294 mbedtls_cipher_context_t *ctx,
4295 const uint8_t *input,
4296 size_t input_length,
4297 uint8_t *output,
4298 size_t output_size,
4299 size_t *output_length )
4300{
4301 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4302 size_t block_size = ctx->cipher_info->block_size;
4303 size_t internal_output_length = 0;
4304 *output_length = 0;
4305
4306 if( input_length == 0 )
4307 {
4308 status = PSA_SUCCESS;
4309 goto exit;
4310 }
4311
4312 if( ctx->unprocessed_len > 0 )
4313 {
4314 /* Fill up to block size, and run the block if there's a full one. */
4315 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4316
4317 if( input_length < bytes_to_copy )
4318 bytes_to_copy = input_length;
4319
4320 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4321 input, bytes_to_copy );
4322 input_length -= bytes_to_copy;
4323 input += bytes_to_copy;
4324 ctx->unprocessed_len += bytes_to_copy;
4325
4326 if( ctx->unprocessed_len == block_size )
4327 {
4328 status = mbedtls_to_psa_error(
4329 mbedtls_cipher_update( ctx,
4330 ctx->unprocessed_data,
4331 block_size,
4332 output, &internal_output_length ) );
4333
4334 if( status != PSA_SUCCESS )
4335 goto exit;
4336
4337 output += internal_output_length;
4338 output_size -= internal_output_length;
4339 *output_length += internal_output_length;
4340 ctx->unprocessed_len = 0;
4341 }
4342 }
4343
4344 while( input_length >= block_size )
4345 {
4346 /* Run all full blocks we have, one by one */
4347 status = mbedtls_to_psa_error(
4348 mbedtls_cipher_update( ctx, input,
4349 block_size,
4350 output, &internal_output_length ) );
4351
4352 if( status != PSA_SUCCESS )
4353 goto exit;
4354
4355 input_length -= block_size;
4356 input += block_size;
4357
4358 output += internal_output_length;
4359 output_size -= internal_output_length;
4360 *output_length += internal_output_length;
4361 }
4362
4363 if( input_length > 0 )
4364 {
4365 /* Save unprocessed bytes for later processing */
4366 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4367 input, input_length );
4368 ctx->unprocessed_len += input_length;
4369 }
4370
4371 status = PSA_SUCCESS;
4372
4373exit:
4374 return( status );
4375}
4376
Gilles Peskinee553c652018-06-04 16:22:46 +02004377psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4378 const uint8_t *input,
4379 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004380 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004381 size_t output_size,
4382 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004383{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004384 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004385 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004386 if( operation->alg == 0 )
4387 {
4388 return( PSA_ERROR_BAD_STATE );
4389 }
4390 if( operation->iv_required && ! operation->iv_set )
4391 {
4392 return( PSA_ERROR_BAD_STATE );
4393 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004394
Steven Cooremanef8575e2020-09-11 11:44:50 +02004395 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004396 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004397 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004398 input,
4399 input_length,
4400 output,
4401 output_size,
4402 output_length );
4403 goto exit;
4404 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004405
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004406 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004407 {
4408 /* Take the unprocessed partial block left over from previous
4409 * update calls, if any, plus the input to this call. Remove
4410 * the last partial block, if any. You get the data that will be
4411 * output in this call. */
4412 expected_output_size =
4413 ( operation->ctx.cipher.unprocessed_len + input_length )
4414 / operation->block_size * operation->block_size;
4415 }
4416 else
4417 {
4418 expected_output_size = input_length;
4419 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004420
Gilles Peskine89d789c2018-06-04 17:17:16 +02004421 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004422 {
4423 status = PSA_ERROR_BUFFER_TOO_SMALL;
4424 goto exit;
4425 }
mohammad160382759612018-03-12 18:16:40 +02004426
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004427 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4428 {
4429 /* mbedtls_cipher_update has an API inconsistency: it will only
4430 * process a single block at a time in ECB mode. Abstract away that
4431 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004432 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4433 input,
4434 input_length,
4435 output,
4436 output_size,
4437 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004438 }
4439 else
4440 {
4441 status = mbedtls_to_psa_error(
4442 mbedtls_cipher_update( &operation->ctx.cipher, input,
4443 input_length, output, output_length ) );
4444 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004445exit:
4446 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004447 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004448 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004449}
4450
Gilles Peskinee553c652018-06-04 16:22:46 +02004451psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4452 uint8_t *output,
4453 size_t output_size,
4454 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004455{
David Saadab4ecc272019-02-14 13:48:10 +02004456 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004457 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004458 if( operation->alg == 0 )
4459 {
4460 return( PSA_ERROR_BAD_STATE );
4461 }
4462 if( operation->iv_required && ! operation->iv_set )
4463 {
4464 return( PSA_ERROR_BAD_STATE );
4465 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004466
Steven Cooremanef8575e2020-09-11 11:44:50 +02004467 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004468 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004469 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004470 output,
4471 output_size,
4472 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004473 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004474 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004475
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004476 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004477 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004478 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004479 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004480 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004481 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004482 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004483 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004484 }
4485
Steven Cooremanef8575e2020-09-11 11:44:50 +02004486 status = mbedtls_to_psa_error(
4487 mbedtls_cipher_finish( &operation->ctx.cipher,
4488 temp_output_buffer,
4489 output_length ) );
4490 if( status != PSA_SUCCESS )
4491 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004492
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004493 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004494 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004495 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004496 memcpy( output, temp_output_buffer, *output_length );
4497 else
Janos Follath315b51c2018-07-09 16:04:51 +01004498 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004499
Steven Cooremanef8575e2020-09-11 11:44:50 +02004500exit:
4501 if( operation->mbedtls_in_use == 1 )
4502 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004503
Steven Cooremanef8575e2020-09-11 11:44:50 +02004504 if( status == PSA_SUCCESS )
4505 return( psa_cipher_abort( operation ) );
4506 else
4507 {
4508 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004509 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004510
Steven Cooremanef8575e2020-09-11 11:44:50 +02004511 return( status );
4512 }
mohammad1603503973b2018-03-12 15:59:30 +02004513}
4514
Gilles Peskinee553c652018-06-04 16:22:46 +02004515psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4516{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004517 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004518 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004519 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004520 * in use. It's ok to call abort on such an object, and there's
4521 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004522 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004523 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004524
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004525 /* Sanity check (shouldn't happen: operation->alg should
4526 * always have been initialized to a valid value). */
4527 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4528 return( PSA_ERROR_BAD_STATE );
4529
Steven Cooremanef8575e2020-09-11 11:44:50 +02004530 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004531 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004532 else
4533 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004534
Moran Peker41deec42018-04-04 15:43:05 +03004535 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004536 operation->key_set = 0;
4537 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004538 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004539 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004540 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004541 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004542
Moran Peker395db872018-05-31 14:07:14 +03004543 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004544}
4545
Gilles Peskinea0655c32018-04-30 17:06:50 +02004546
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004547
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004548
mohammad16035955c982018-04-26 00:53:03 +03004549/****************************************************************/
4550/* AEAD */
4551/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004552
Gilles Peskineedf9a652018-08-17 18:11:56 +02004553typedef struct
4554{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004555 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004556 const mbedtls_cipher_info_t *cipher_info;
4557 union
4558 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004559 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004560#if defined(MBEDTLS_CCM_C)
4561 mbedtls_ccm_context ccm;
4562#endif /* MBEDTLS_CCM_C */
4563#if defined(MBEDTLS_GCM_C)
4564 mbedtls_gcm_context gcm;
4565#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004566#if defined(MBEDTLS_CHACHAPOLY_C)
4567 mbedtls_chachapoly_context chachapoly;
4568#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004569 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004570 psa_algorithm_t core_alg;
4571 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004572 uint8_t tag_length;
4573} aead_operation_t;
4574
Ronald Cronf95a2b12020-10-22 15:24:49 +02004575#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4576
Gilles Peskine30a9e412019-01-14 18:36:12 +01004577static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004578{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004579 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004580 {
4581#if defined(MBEDTLS_CCM_C)
4582 case PSA_ALG_CCM:
4583 mbedtls_ccm_free( &operation->ctx.ccm );
4584 break;
4585#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004586#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004587 case PSA_ALG_GCM:
4588 mbedtls_gcm_free( &operation->ctx.gcm );
4589 break;
4590#endif /* MBEDTLS_GCM_C */
4591 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004592
Ronald Cron5c522922020-11-14 16:35:34 +01004593 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004594}
4595
4596static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004597 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004598 psa_key_usage_t usage,
4599 psa_algorithm_t alg )
4600{
4601 psa_status_t status;
4602 size_t key_bits;
4603 mbedtls_cipher_id_t cipher_id;
4604
Ronald Cron5c522922020-11-14 16:35:34 +01004605 status = psa_get_and_lock_transparent_key_slot_with_policy(
4606 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004607 if( status != PSA_SUCCESS )
4608 return( status );
4609
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004610 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004611
4612 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004613 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004614 &cipher_id );
4615 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004616 {
4617 status = PSA_ERROR_NOT_SUPPORTED;
4618 goto cleanup;
4619 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004620
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004621 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004622 {
4623#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004624 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4625 operation->core_alg = PSA_ALG_CCM;
4626 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004627 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4628 * The call to mbedtls_ccm_encrypt_and_tag or
4629 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004630 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004631 {
4632 status = PSA_ERROR_INVALID_ARGUMENT;
4633 goto cleanup;
4634 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004635 mbedtls_ccm_init( &operation->ctx.ccm );
4636 status = mbedtls_to_psa_error(
4637 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004638 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004639 (unsigned int) key_bits ) );
4640 if( status != 0 )
4641 goto cleanup;
4642 break;
4643#endif /* MBEDTLS_CCM_C */
4644
4645#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004646 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4647 operation->core_alg = PSA_ALG_GCM;
4648 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004649 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4650 * The call to mbedtls_gcm_crypt_and_tag or
4651 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004652 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004653 {
4654 status = PSA_ERROR_INVALID_ARGUMENT;
4655 goto cleanup;
4656 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004657 mbedtls_gcm_init( &operation->ctx.gcm );
4658 status = mbedtls_to_psa_error(
4659 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004660 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004661 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004662 if( status != 0 )
4663 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004664 break;
4665#endif /* MBEDTLS_GCM_C */
4666
Gilles Peskine26869f22019-05-06 15:25:00 +02004667#if defined(MBEDTLS_CHACHAPOLY_C)
4668 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4669 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4670 operation->full_tag_length = 16;
4671 /* We only support the default tag length. */
4672 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004673 {
4674 status = PSA_ERROR_NOT_SUPPORTED;
4675 goto cleanup;
4676 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004677 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4678 status = mbedtls_to_psa_error(
4679 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004680 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004681 if( status != 0 )
4682 goto cleanup;
4683 break;
4684#endif /* MBEDTLS_CHACHAPOLY_C */
4685
Gilles Peskineedf9a652018-08-17 18:11:56 +02004686 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004687 status = PSA_ERROR_NOT_SUPPORTED;
4688 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004689 }
4690
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004691 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4692 {
4693 status = PSA_ERROR_INVALID_ARGUMENT;
4694 goto cleanup;
4695 }
4696 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004697
Gilles Peskineedf9a652018-08-17 18:11:56 +02004698 return( PSA_SUCCESS );
4699
4700cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004701 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004702 return( status );
4703}
4704
Ronald Croncf56a0a2020-08-04 09:51:30 +02004705psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004706 psa_algorithm_t alg,
4707 const uint8_t *nonce,
4708 size_t nonce_length,
4709 const uint8_t *additional_data,
4710 size_t additional_data_length,
4711 const uint8_t *plaintext,
4712 size_t plaintext_length,
4713 uint8_t *ciphertext,
4714 size_t ciphertext_size,
4715 size_t *ciphertext_length )
4716{
mohammad16035955c982018-04-26 00:53:03 +03004717 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004718 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004719 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004720
mohammad1603f08a5502018-06-03 15:05:47 +03004721 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004722
Ronald Croncf56a0a2020-08-04 09:51:30 +02004723 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004724 if( status != PSA_SUCCESS )
4725 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004726
Gilles Peskineedf9a652018-08-17 18:11:56 +02004727 /* For all currently supported modes, the tag is at the end of the
4728 * ciphertext. */
4729 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4730 {
4731 status = PSA_ERROR_BUFFER_TOO_SMALL;
4732 goto exit;
4733 }
4734 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004735
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004736#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004737 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004738 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004739 status = mbedtls_to_psa_error(
4740 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4741 MBEDTLS_GCM_ENCRYPT,
4742 plaintext_length,
4743 nonce, nonce_length,
4744 additional_data, additional_data_length,
4745 plaintext, ciphertext,
4746 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004747 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004748 else
4749#endif /* MBEDTLS_GCM_C */
4750#if defined(MBEDTLS_CCM_C)
4751 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004752 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004753 status = mbedtls_to_psa_error(
4754 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4755 plaintext_length,
4756 nonce, nonce_length,
4757 additional_data,
4758 additional_data_length,
4759 plaintext, ciphertext,
4760 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004761 }
mohammad16035c8845f2018-05-09 05:40:09 -07004762 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004763#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004764#if defined(MBEDTLS_CHACHAPOLY_C)
4765 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4766 {
4767 if( nonce_length != 12 || operation.tag_length != 16 )
4768 {
4769 status = PSA_ERROR_NOT_SUPPORTED;
4770 goto exit;
4771 }
4772 status = mbedtls_to_psa_error(
4773 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4774 plaintext_length,
4775 nonce,
4776 additional_data,
4777 additional_data_length,
4778 plaintext,
4779 ciphertext,
4780 tag ) );
4781 }
4782 else
4783#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004784 {
mohammad1603554faad2018-06-03 15:07:38 +03004785 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004786 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004787
Gilles Peskineedf9a652018-08-17 18:11:56 +02004788 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4789 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004790
Gilles Peskineedf9a652018-08-17 18:11:56 +02004791exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004792 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004793 if( status == PSA_SUCCESS )
4794 *ciphertext_length = plaintext_length + operation.tag_length;
4795 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004796}
4797
Gilles Peskineee652a32018-06-01 19:23:52 +02004798/* Locate the tag in a ciphertext buffer containing the encrypted data
4799 * followed by the tag. Return the length of the part preceding the tag in
4800 * *plaintext_length. This is the size of the plaintext in modes where
4801 * the encrypted data has the same size as the plaintext, such as
4802 * CCM and GCM. */
4803static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4804 const uint8_t *ciphertext,
4805 size_t ciphertext_length,
4806 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004807 const uint8_t **p_tag )
4808{
4809 size_t payload_length;
4810 if( tag_length > ciphertext_length )
4811 return( PSA_ERROR_INVALID_ARGUMENT );
4812 payload_length = ciphertext_length - tag_length;
4813 if( payload_length > plaintext_size )
4814 return( PSA_ERROR_BUFFER_TOO_SMALL );
4815 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004816 return( PSA_SUCCESS );
4817}
4818
Ronald Croncf56a0a2020-08-04 09:51:30 +02004819psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004820 psa_algorithm_t alg,
4821 const uint8_t *nonce,
4822 size_t nonce_length,
4823 const uint8_t *additional_data,
4824 size_t additional_data_length,
4825 const uint8_t *ciphertext,
4826 size_t ciphertext_length,
4827 uint8_t *plaintext,
4828 size_t plaintext_size,
4829 size_t *plaintext_length )
4830{
mohammad16035955c982018-04-26 00:53:03 +03004831 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004832 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004833 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004834
Gilles Peskineee652a32018-06-01 19:23:52 +02004835 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004836
Ronald Croncf56a0a2020-08-04 09:51:30 +02004837 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004838 if( status != PSA_SUCCESS )
4839 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004840
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004841 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4842 ciphertext, ciphertext_length,
4843 plaintext_size, &tag );
4844 if( status != PSA_SUCCESS )
4845 goto exit;
4846
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004847#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004848 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004849 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004850 status = mbedtls_to_psa_error(
4851 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4852 ciphertext_length - operation.tag_length,
4853 nonce, nonce_length,
4854 additional_data,
4855 additional_data_length,
4856 tag, operation.tag_length,
4857 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004858 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004859 else
4860#endif /* MBEDTLS_GCM_C */
4861#if defined(MBEDTLS_CCM_C)
4862 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004863 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004864 status = mbedtls_to_psa_error(
4865 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4866 ciphertext_length - operation.tag_length,
4867 nonce, nonce_length,
4868 additional_data,
4869 additional_data_length,
4870 ciphertext, plaintext,
4871 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004872 }
mohammad160339574652018-06-01 04:39:53 -07004873 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004874#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004875#if defined(MBEDTLS_CHACHAPOLY_C)
4876 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4877 {
4878 if( nonce_length != 12 || operation.tag_length != 16 )
4879 {
4880 status = PSA_ERROR_NOT_SUPPORTED;
4881 goto exit;
4882 }
4883 status = mbedtls_to_psa_error(
4884 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4885 ciphertext_length - operation.tag_length,
4886 nonce,
4887 additional_data,
4888 additional_data_length,
4889 tag,
4890 ciphertext,
4891 plaintext ) );
4892 }
4893 else
4894#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004895 {
mohammad1603554faad2018-06-03 15:07:38 +03004896 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004897 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004898
Gilles Peskineedf9a652018-08-17 18:11:56 +02004899 if( status != PSA_SUCCESS && plaintext_size != 0 )
4900 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004901
Gilles Peskineedf9a652018-08-17 18:11:56 +02004902exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004903 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004904 if( status == PSA_SUCCESS )
4905 *plaintext_length = ciphertext_length - operation.tag_length;
4906 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004907}
4908
Gilles Peskinea0655c32018-04-30 17:06:50 +02004909
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004910
Gilles Peskine20035e32018-02-03 22:44:14 +01004911/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004912/* Generators */
4913/****************************************************************/
4914
John Durkop07cc04a2020-11-16 22:08:34 -08004915#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4916 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4917 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4918#define AT_LEAST_ONE_BUILTIN_KDF
4919#endif
4920
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004921#define HKDF_STATE_INIT 0 /* no input yet */
4922#define HKDF_STATE_STARTED 1 /* got salt */
4923#define HKDF_STATE_KEYED 2 /* got key */
4924#define HKDF_STATE_OUTPUT 3 /* output started */
4925
Gilles Peskinecbe66502019-05-16 16:59:18 +02004926static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004928{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004929 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4930 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004931 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004933}
4934
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004936{
4937 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004938 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004939 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004940 {
4941 /* The object has (apparently) been initialized but it is not
4942 * in use. It's ok to call abort on such an object, and there's
4943 * nothing to do. */
4944 }
4945 else
John Durkopd0321952020-10-29 21:37:36 -07004946#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004947 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004948 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004949 mbedtls_free( operation->ctx.hkdf.info );
4950 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004951 }
John Durkop07cc04a2020-11-16 22:08:34 -08004952 else
4953#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4954#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4955 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4956 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004958 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004959 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004960 if( operation->ctx.tls12_prf.seed != NULL )
4961 {
4962 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4963 operation->ctx.tls12_prf.seed_length );
4964 mbedtls_free( operation->ctx.tls12_prf.seed );
4965 }
4966
4967 if( operation->ctx.tls12_prf.label != NULL )
4968 {
4969 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4970 operation->ctx.tls12_prf.label_length );
4971 mbedtls_free( operation->ctx.tls12_prf.label );
4972 }
4973
4974 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4975
4976 /* We leave the fields Ai and output_block to be erased safely by the
4977 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004978 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004979 else
John Durkop07cc04a2020-11-16 22:08:34 -08004980#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4981 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004982 {
4983 status = PSA_ERROR_BAD_STATE;
4984 }
Janos Follath083036a2019-06-11 10:22:26 +01004985 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004986 return( status );
4987}
4988
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004989psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004990 size_t *capacity)
4991{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004992 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004993 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004994 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004995 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004996 }
4997
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004998 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004999 return( PSA_SUCCESS );
5000}
5001
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005003 size_t capacity )
5004{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005006 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005008 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005010 return( PSA_SUCCESS );
5011}
5012
John Durkopd0321952020-10-29 21:37:36 -07005013#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005014/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005015 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005016static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005017 psa_algorithm_t hash_alg,
5018 uint8_t *output,
5019 size_t output_length )
5020{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005021 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005022 psa_status_t status;
5023
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005024 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5025 return( PSA_ERROR_BAD_STATE );
5026 hkdf->state = HKDF_STATE_OUTPUT;
5027
Gilles Peskinebef7f142018-07-12 17:22:21 +02005028 while( output_length != 0 )
5029 {
5030 /* Copy what remains of the current block */
5031 uint8_t n = hash_length - hkdf->offset_in_block;
5032 if( n > output_length )
5033 n = (uint8_t) output_length;
5034 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5035 output += n;
5036 output_length -= n;
5037 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005038 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005039 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005040 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005041 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005043 * object was corrupted or if this function is called directly
5044 * inside the library. */
5045 if( hkdf->block_number == 0xff )
5046 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005047
5048 /* We need a new block */
5049 ++hkdf->block_number;
5050 hkdf->offset_in_block = 0;
5051 status = psa_hmac_setup_internal( &hkdf->hmac,
5052 hkdf->prk, hash_length,
5053 hash_alg );
5054 if( status != PSA_SUCCESS )
5055 return( status );
5056 if( hkdf->block_number != 1 )
5057 {
5058 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5059 hkdf->output_block,
5060 hash_length );
5061 if( status != PSA_SUCCESS )
5062 return( status );
5063 }
5064 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5065 hkdf->info,
5066 hkdf->info_length );
5067 if( status != PSA_SUCCESS )
5068 return( status );
5069 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5070 &hkdf->block_number, 1 );
5071 if( status != PSA_SUCCESS )
5072 return( status );
5073 status = psa_hmac_finish_internal( &hkdf->hmac,
5074 hkdf->output_block,
5075 sizeof( hkdf->output_block ) );
5076 if( status != PSA_SUCCESS )
5077 return( status );
5078 }
5079
5080 return( PSA_SUCCESS );
5081}
John Durkop07cc04a2020-11-16 22:08:34 -08005082#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005083
John Durkop07cc04a2020-11-16 22:08:34 -08005084#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5085 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005086static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5087 psa_tls12_prf_key_derivation_t *tls12_prf,
5088 psa_algorithm_t alg )
5089{
5090 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005091 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005092 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5093 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005094
Janos Follath7742fee2019-06-17 12:58:10 +01005095 /* We can't be wanting more output after block 0xff, otherwise
5096 * the capacity check in psa_key_derivation_output_bytes() would have
5097 * prevented this call. It could happen only if the operation
5098 * object was corrupted or if this function is called directly
5099 * inside the library. */
5100 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005101 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005102
5103 /* We need a new block */
5104 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005105 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005106
5107 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5108 *
5109 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5110 *
5111 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5112 * HMAC_hash(secret, A(2) + seed) +
5113 * HMAC_hash(secret, A(3) + seed) + ...
5114 *
5115 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005116 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005117 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005118 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005119 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005120 * is currently extracted as `output_block` and where i is
5121 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005122 */
5123
Janos Follathea29bfb2019-06-19 12:21:20 +01005124 /* Save the hash context before using it, to preserve the hash state with
5125 * only the inner padding in it. We need this, because inner padding depends
5126 * on the key (secret in the RFC's terminology). */
5127 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5128 if( status != PSA_SUCCESS )
5129 goto cleanup;
5130
5131 /* Calculate A(i) where i = tls12_prf->block_number. */
5132 if( tls12_prf->block_number == 1 )
5133 {
5134 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5135 * the variable seed and in this instance means it in the context of the
5136 * P_hash function, where seed = label + seed.) */
5137 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5138 tls12_prf->label, tls12_prf->label_length );
5139 if( status != PSA_SUCCESS )
5140 goto cleanup;
5141 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5142 tls12_prf->seed, tls12_prf->seed_length );
5143 if( status != PSA_SUCCESS )
5144 goto cleanup;
5145 }
5146 else
5147 {
5148 /* A(i) = HMAC_hash(secret, A(i-1)) */
5149 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5150 tls12_prf->Ai, hash_length );
5151 if( status != PSA_SUCCESS )
5152 goto cleanup;
5153 }
5154
5155 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5156 tls12_prf->Ai, hash_length );
5157 if( status != PSA_SUCCESS )
5158 goto cleanup;
5159 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5160 if( status != PSA_SUCCESS )
5161 goto cleanup;
5162
5163 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5164 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5165 tls12_prf->Ai, hash_length );
5166 if( status != PSA_SUCCESS )
5167 goto cleanup;
5168 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5169 tls12_prf->label, tls12_prf->label_length );
5170 if( status != PSA_SUCCESS )
5171 goto cleanup;
5172 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5173 tls12_prf->seed, tls12_prf->seed_length );
5174 if( status != PSA_SUCCESS )
5175 goto cleanup;
5176 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5177 tls12_prf->output_block, hash_length );
5178 if( status != PSA_SUCCESS )
5179 goto cleanup;
5180 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5181 if( status != PSA_SUCCESS )
5182 goto cleanup;
5183
Janos Follath7742fee2019-06-17 12:58:10 +01005184
5185cleanup:
5186
Janos Follathea29bfb2019-06-19 12:21:20 +01005187 cleanup_status = psa_hash_abort( &backup );
5188 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5189 status = cleanup_status;
5190
5191 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005192}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005193
Janos Follath844eb0e2019-06-19 12:10:49 +01005194static psa_status_t psa_key_derivation_tls12_prf_read(
5195 psa_tls12_prf_key_derivation_t *tls12_prf,
5196 psa_algorithm_t alg,
5197 uint8_t *output,
5198 size_t output_length )
5199{
5200 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005201 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005202 psa_status_t status;
5203 uint8_t offset, length;
5204
5205 while( output_length != 0 )
5206 {
5207 /* Check if we have fully processed the current block. */
5208 if( tls12_prf->left_in_block == 0 )
5209 {
5210 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5211 alg );
5212 if( status != PSA_SUCCESS )
5213 return( status );
5214
5215 continue;
5216 }
5217
5218 if( tls12_prf->left_in_block > output_length )
5219 length = (uint8_t) output_length;
5220 else
5221 length = tls12_prf->left_in_block;
5222
5223 offset = hash_length - tls12_prf->left_in_block;
5224 memcpy( output, tls12_prf->output_block + offset, length );
5225 output += length;
5226 output_length -= length;
5227 tls12_prf->left_in_block -= length;
5228 }
5229
5230 return( PSA_SUCCESS );
5231}
John Durkop07cc04a2020-11-16 22:08:34 -08005232#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5233 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005234
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005235psa_status_t psa_key_derivation_output_bytes(
5236 psa_key_derivation_operation_t *operation,
5237 uint8_t *output,
5238 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005239{
5240 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005242
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005243 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005244 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005246 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005247 }
5248
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005250 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005252 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005254 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005255 goto exit;
5256 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005258 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005259 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005260 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005261 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5262 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005264 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005265 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005266 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005268
John Durkopd0321952020-10-29 21:37:36 -07005269#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005270 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005271 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005272 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005273 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005274 output, output_length );
5275 }
Janos Follathadbec812019-06-14 11:05:39 +01005276 else
John Durkop07cc04a2020-11-16 22:08:34 -08005277#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5278#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5279 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005280 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005281 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005282 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005283 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005284 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005285 output_length );
5286 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005287 else
John Durkop07cc04a2020-11-16 22:08:34 -08005288#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5289 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005290 {
5291 return( PSA_ERROR_BAD_STATE );
5292 }
5293
5294exit:
5295 if( status != PSA_SUCCESS )
5296 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005297 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005298 * This allows us to differentiate between exhausted operations and
5299 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5300 * operations. */
5301 psa_algorithm_t alg = operation->alg;
5302 psa_key_derivation_abort( operation );
5303 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005304 memset( output, '!', output_length );
5305 }
5306 return( status );
5307}
5308
Gilles Peskine08542d82018-07-19 17:05:42 +02005309#if defined(MBEDTLS_DES_C)
5310static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5311{
5312 if( data_size >= 8 )
5313 mbedtls_des_key_set_parity( data );
5314 if( data_size >= 16 )
5315 mbedtls_des_key_set_parity( data + 8 );
5316 if( data_size >= 24 )
5317 mbedtls_des_key_set_parity( data + 16 );
5318}
5319#endif /* MBEDTLS_DES_C */
5320
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005321static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005322 psa_key_slot_t *slot,
5323 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005324 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005325{
5326 uint8_t *data = NULL;
5327 size_t bytes = PSA_BITS_TO_BYTES( bits );
5328 psa_status_t status;
5329
Gilles Peskine8e338702019-07-30 20:06:31 +02005330 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005331 return( PSA_ERROR_INVALID_ARGUMENT );
5332 if( bits % 8 != 0 )
5333 return( PSA_ERROR_INVALID_ARGUMENT );
5334 data = mbedtls_calloc( 1, bytes );
5335 if( data == NULL )
5336 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5337
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005339 if( status != PSA_SUCCESS )
5340 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005341#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005342 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005343 psa_des_set_key_parity( data, bytes );
5344#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005345 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005346
5347exit:
5348 mbedtls_free( data );
5349 return( status );
5350}
5351
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005352psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005353 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005354 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005355{
5356 psa_status_t status;
5357 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005358 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005359
Ronald Cron81709fc2020-11-14 12:10:32 +01005360 *key = MBEDTLS_SVC_KEY_ID_INIT;
5361
Gilles Peskine0f84d622019-09-12 19:03:13 +02005362 /* Reject any attempt to create a zero-length key so that we don't
5363 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5364 if( psa_get_key_bits( attributes ) == 0 )
5365 return( PSA_ERROR_INVALID_ARGUMENT );
5366
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005367 if( ! operation->can_output_key )
5368 return( PSA_ERROR_NOT_PERMITTED );
5369
Ronald Cron81709fc2020-11-14 12:10:32 +01005370 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5371 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005372#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5373 if( driver != NULL )
5374 {
5375 /* Deriving a key in a secure element is not implemented yet. */
5376 status = PSA_ERROR_NOT_SUPPORTED;
5377 }
5378#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005379 if( status == PSA_SUCCESS )
5380 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005381 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005382 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005383 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005384 }
5385 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005386 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005387 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005388 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005389
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005390 return( status );
5391}
5392
Gilles Peskineeab56e42018-07-12 17:12:33 +02005393
5394
5395/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005396/* Key derivation */
5397/****************************************************************/
5398
John Durkop07cc04a2020-11-16 22:08:34 -08005399#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005400static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005401 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005402 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005403{
John Durkop07cc04a2020-11-16 22:08:34 -08005404 int is_kdf_alg_supported;
5405
Janos Follath5fe19732019-06-20 15:09:30 +01005406 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5407 * initialisers for this union leave some bytes unspecified.) */
5408 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5409
Gilles Peskine969c5d62019-01-16 15:53:06 +01005410 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005411#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005412 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5413 is_kdf_alg_supported = 1;
5414 else
5415#endif
5416#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5417 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5418 is_kdf_alg_supported = 1;
5419 else
5420#endif
5421#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5422 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5423 is_kdf_alg_supported = 1;
5424 else
5425#endif
5426 is_kdf_alg_supported = 0;
5427
5428 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005429 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005430 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005431 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005432 if( hash_size == 0 )
5433 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005434 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5435 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005436 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005437 {
5438 return( PSA_ERROR_NOT_SUPPORTED );
5439 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005440 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005441 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005442 }
John Durkop07cc04a2020-11-16 22:08:34 -08005443
5444 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005445}
John Durkop07cc04a2020-11-16 22:08:34 -08005446#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005447
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005449 psa_algorithm_t alg )
5450{
5451 psa_status_t status;
5452
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005453 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005454 return( PSA_ERROR_BAD_STATE );
5455
Gilles Peskine6843c292019-01-18 16:44:49 +01005456 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5457 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005458#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005459 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005460 {
5461 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005462 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005463 }
5464 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5465 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005467 }
John Durkop07cc04a2020-11-16 22:08:34 -08005468#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005469 else
5470 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005471
5472 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005473 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005474 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005475}
5476
John Durkopd0321952020-10-29 21:37:36 -07005477#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005478static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005479 psa_algorithm_t hash_alg,
5480 psa_key_derivation_step_t step,
5481 const uint8_t *data,
5482 size_t data_length )
5483{
5484 psa_status_t status;
5485 switch( step )
5486 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005487 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005488 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005489 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005490 status = psa_hmac_setup_internal( &hkdf->hmac,
5491 data, data_length,
5492 hash_alg );
5493 if( status != PSA_SUCCESS )
5494 return( status );
5495 hkdf->state = HKDF_STATE_STARTED;
5496 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005497 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005498 /* If no salt was provided, use an empty salt. */
5499 if( hkdf->state == HKDF_STATE_INIT )
5500 {
5501 status = psa_hmac_setup_internal( &hkdf->hmac,
5502 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005503 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005504 if( status != PSA_SUCCESS )
5505 return( status );
5506 hkdf->state = HKDF_STATE_STARTED;
5507 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005508 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005509 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005510 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5511 data, data_length );
5512 if( status != PSA_SUCCESS )
5513 return( status );
5514 status = psa_hmac_finish_internal( &hkdf->hmac,
5515 hkdf->prk,
5516 sizeof( hkdf->prk ) );
5517 if( status != PSA_SUCCESS )
5518 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005519 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005520 hkdf->block_number = 0;
5521 hkdf->state = HKDF_STATE_KEYED;
5522 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005523 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005524 if( hkdf->state == HKDF_STATE_OUTPUT )
5525 return( PSA_ERROR_BAD_STATE );
5526 if( hkdf->info_set )
5527 return( PSA_ERROR_BAD_STATE );
5528 hkdf->info_length = data_length;
5529 if( data_length != 0 )
5530 {
5531 hkdf->info = mbedtls_calloc( 1, data_length );
5532 if( hkdf->info == NULL )
5533 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5534 memcpy( hkdf->info, data, data_length );
5535 }
5536 hkdf->info_set = 1;
5537 return( PSA_SUCCESS );
5538 default:
5539 return( PSA_ERROR_INVALID_ARGUMENT );
5540 }
5541}
John Durkop07cc04a2020-11-16 22:08:34 -08005542#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005543
John Durkop07cc04a2020-11-16 22:08:34 -08005544#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5545 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005546static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5547 const uint8_t *data,
5548 size_t data_length )
5549{
5550 if( prf->state != TLS12_PRF_STATE_INIT )
5551 return( PSA_ERROR_BAD_STATE );
5552
Janos Follathd6dce9f2019-07-04 09:11:38 +01005553 if( data_length != 0 )
5554 {
5555 prf->seed = mbedtls_calloc( 1, data_length );
5556 if( prf->seed == NULL )
5557 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005558
Janos Follathd6dce9f2019-07-04 09:11:38 +01005559 memcpy( prf->seed, data, data_length );
5560 prf->seed_length = data_length;
5561 }
Janos Follathf08e2652019-06-13 09:05:41 +01005562
5563 prf->state = TLS12_PRF_STATE_SEED_SET;
5564
5565 return( PSA_SUCCESS );
5566}
5567
Janos Follath81550542019-06-13 14:26:34 +01005568static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5569 psa_algorithm_t hash_alg,
5570 const uint8_t *data,
5571 size_t data_length )
5572{
5573 psa_status_t status;
5574 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5575 return( PSA_ERROR_BAD_STATE );
5576
5577 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5578 if( status != PSA_SUCCESS )
5579 return( status );
5580
5581 prf->state = TLS12_PRF_STATE_KEY_SET;
5582
5583 return( PSA_SUCCESS );
5584}
5585
Janos Follath63028dd2019-06-13 09:15:47 +01005586static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5587 const uint8_t *data,
5588 size_t data_length )
5589{
5590 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5591 return( PSA_ERROR_BAD_STATE );
5592
Janos Follathd6dce9f2019-07-04 09:11:38 +01005593 if( data_length != 0 )
5594 {
5595 prf->label = mbedtls_calloc( 1, data_length );
5596 if( prf->label == NULL )
5597 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005598
Janos Follathd6dce9f2019-07-04 09:11:38 +01005599 memcpy( prf->label, data, data_length );
5600 prf->label_length = data_length;
5601 }
Janos Follath63028dd2019-06-13 09:15:47 +01005602
5603 prf->state = TLS12_PRF_STATE_LABEL_SET;
5604
5605 return( PSA_SUCCESS );
5606}
5607
Janos Follathb03233e2019-06-11 15:30:30 +01005608static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5609 psa_algorithm_t hash_alg,
5610 psa_key_derivation_step_t step,
5611 const uint8_t *data,
5612 size_t data_length )
5613{
Janos Follathb03233e2019-06-11 15:30:30 +01005614 switch( step )
5615 {
Janos Follathf08e2652019-06-13 09:05:41 +01005616 case PSA_KEY_DERIVATION_INPUT_SEED:
5617 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005618 case PSA_KEY_DERIVATION_INPUT_SECRET:
5619 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005620 case PSA_KEY_DERIVATION_INPUT_LABEL:
5621 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005622 default:
5623 return( PSA_ERROR_INVALID_ARGUMENT );
5624 }
5625}
John Durkop07cc04a2020-11-16 22:08:34 -08005626#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5627 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5628
5629#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5630static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5631 psa_tls12_prf_key_derivation_t *prf,
5632 psa_algorithm_t hash_alg,
5633 const uint8_t *data,
5634 size_t data_length )
5635{
5636 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005637 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005638 uint8_t *cur = pms;
5639
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005640 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005641 return( PSA_ERROR_INVALID_ARGUMENT );
5642
5643 /* Quoting RFC 4279, Section 2:
5644 *
5645 * The premaster secret is formed as follows: if the PSK is N octets
5646 * long, concatenate a uint16 with the value N, N zero octets, a second
5647 * uint16 with the value N, and the PSK itself.
5648 */
5649
5650 *cur++ = ( data_length >> 8 ) & 0xff;
5651 *cur++ = ( data_length >> 0 ) & 0xff;
5652 memset( cur, 0, data_length );
5653 cur += data_length;
5654 *cur++ = pms[0];
5655 *cur++ = pms[1];
5656 memcpy( cur, data, data_length );
5657 cur += data_length;
5658
5659 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5660
5661 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5662 return( status );
5663}
Janos Follath6660f0e2019-06-17 08:44:03 +01005664
5665static psa_status_t psa_tls12_prf_psk_to_ms_input(
5666 psa_tls12_prf_key_derivation_t *prf,
5667 psa_algorithm_t hash_alg,
5668 psa_key_derivation_step_t step,
5669 const uint8_t *data,
5670 size_t data_length )
5671{
5672 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005673 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005674 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5675 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005676 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005677
5678 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5679}
John Durkop07cc04a2020-11-16 22:08:34 -08005680#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005681
Gilles Peskineb8965192019-09-24 16:21:10 +02005682/** Check whether the given key type is acceptable for the given
5683 * input step of a key derivation.
5684 *
5685 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5686 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5687 * Both secret and non-secret inputs can alternatively have the type
5688 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5689 * that the input was passed as a buffer rather than via a key object.
5690 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005691static int psa_key_derivation_check_input_type(
5692 psa_key_derivation_step_t step,
5693 psa_key_type_t key_type )
5694{
5695 switch( step )
5696 {
5697 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005698 if( key_type == PSA_KEY_TYPE_DERIVE )
5699 return( PSA_SUCCESS );
5700 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005701 return( PSA_SUCCESS );
5702 break;
5703 case PSA_KEY_DERIVATION_INPUT_LABEL:
5704 case PSA_KEY_DERIVATION_INPUT_SALT:
5705 case PSA_KEY_DERIVATION_INPUT_INFO:
5706 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005707 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5708 return( PSA_SUCCESS );
5709 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005710 return( PSA_SUCCESS );
5711 break;
5712 }
5713 return( PSA_ERROR_INVALID_ARGUMENT );
5714}
5715
Janos Follathb80a94e2019-06-12 15:54:46 +01005716static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005717 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005718 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005719 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005720 const uint8_t *data,
5721 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005722{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005723 psa_status_t status;
5724 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5725
5726 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005727 if( status != PSA_SUCCESS )
5728 goto exit;
5729
John Durkopd0321952020-10-29 21:37:36 -07005730#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005731 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005732 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005733 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005734 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005735 step, data, data_length );
5736 }
John Durkop07cc04a2020-11-16 22:08:34 -08005737 else
5738#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5739#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5740 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005741 {
Janos Follathb03233e2019-06-11 15:30:30 +01005742 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5743 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5744 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005745 }
John Durkop07cc04a2020-11-16 22:08:34 -08005746 else
5747#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5748#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5749 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005750 {
5751 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5752 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5753 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005754 }
5755 else
John Durkop07cc04a2020-11-16 22:08:34 -08005756#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005757 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005758 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005759 return( PSA_ERROR_BAD_STATE );
5760 }
5761
Gilles Peskine224b0d62019-09-23 18:13:17 +02005762exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005763 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005764 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005765 return( status );
5766}
5767
Janos Follath51f4a0f2019-06-14 11:35:55 +01005768psa_status_t psa_key_derivation_input_bytes(
5769 psa_key_derivation_operation_t *operation,
5770 psa_key_derivation_step_t step,
5771 const uint8_t *data,
5772 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005773{
Gilles Peskineb8965192019-09-24 16:21:10 +02005774 return( psa_key_derivation_input_internal( operation, step,
5775 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005776 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005777}
5778
Janos Follath51f4a0f2019-06-14 11:35:55 +01005779psa_status_t psa_key_derivation_input_key(
5780 psa_key_derivation_operation_t *operation,
5781 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005782 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005783{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005784 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005785 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005786 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005787
Ronald Cron5c522922020-11-14 16:35:34 +01005788 status = psa_get_and_lock_transparent_key_slot_with_policy(
5789 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005790 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005791 {
5792 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005793 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005794 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005795
5796 /* Passing a key object as a SECRET input unlocks the permission
5797 * to output to a key object. */
5798 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5799 operation->can_output_key = 1;
5800
Ronald Cronf95a2b12020-10-22 15:24:49 +02005801 status = psa_key_derivation_input_internal( operation,
5802 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005803 slot->key.data,
5804 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005805
Ronald Cron5c522922020-11-14 16:35:34 +01005806 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005807
Ronald Cron5c522922020-11-14 16:35:34 +01005808 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005809}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005810
5811
5812
5813/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005814/* Key agreement */
5815/****************************************************************/
5816
John Durkop6ba40d12020-11-10 08:50:04 -08005817#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005818static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5819 size_t peer_key_length,
5820 const mbedtls_ecp_keypair *our_key,
5821 uint8_t *shared_secret,
5822 size_t shared_secret_size,
5823 size_t *shared_secret_length )
5824{
Steven Cooremand4867872020-08-05 16:31:39 +02005825 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005826 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005827 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005828 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005829 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005830 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005831
Ronald Cron90857082020-11-25 14:58:33 +01005832 status = mbedtls_psa_ecp_load_representation(
5833 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5834 peer_key,
5835 peer_key_length,
5836 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005837 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005838 goto exit;
5839
Jaeden Amero97271b32019-01-10 19:38:51 +00005840 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005841 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005842 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005843 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005844 status = mbedtls_to_psa_error(
5845 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5846 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005847 goto exit;
5848
Jaeden Amero97271b32019-01-10 19:38:51 +00005849 status = mbedtls_to_psa_error(
5850 mbedtls_ecdh_calc_secret( &ecdh,
5851 shared_secret_length,
5852 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005853 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005854 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005855 if( status != PSA_SUCCESS )
5856 goto exit;
5857 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5858 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005859
5860exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005861 if( status != PSA_SUCCESS )
5862 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005863 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005864 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005865 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005866
Jaeden Amero97271b32019-01-10 19:38:51 +00005867 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005868}
John Durkop6ba40d12020-11-10 08:50:04 -08005869#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005870
Gilles Peskine01d718c2018-09-18 12:01:02 +02005871#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5872
Gilles Peskine0216fe12019-04-11 21:23:21 +02005873static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5874 psa_key_slot_t *private_key,
5875 const uint8_t *peer_key,
5876 size_t peer_key_length,
5877 uint8_t *shared_secret,
5878 size_t shared_secret_size,
5879 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005880{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005881 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005882 {
John Durkop6ba40d12020-11-10 08:50:04 -08005883#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005884 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005885 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005886 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005887 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005888 psa_status_t status = mbedtls_psa_ecp_load_representation(
5889 private_key->attr.type,
5890 private_key->key.data,
5891 private_key->key.bytes,
5892 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005893 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005894 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005895 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005896 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005897 shared_secret, shared_secret_size,
5898 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005899 mbedtls_ecp_keypair_free( ecp );
5900 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005901 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005902#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005903 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005904 (void) private_key;
5905 (void) peer_key;
5906 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005907 (void) shared_secret;
5908 (void) shared_secret_size;
5909 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005910 return( PSA_ERROR_NOT_SUPPORTED );
5911 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005912}
5913
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005914/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005915 * to potentially free embedded data structures and wipe confidential data.
5916 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005917static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005918 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005919 psa_key_slot_t *private_key,
5920 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005921 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005922{
5923 psa_status_t status;
5924 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5925 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005926 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005927
5928 /* Step 1: run the secret agreement algorithm to generate the shared
5929 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005930 status = psa_key_agreement_raw_internal( ka_alg,
5931 private_key,
5932 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005933 shared_secret,
5934 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005935 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005936 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005937 goto exit;
5938
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005939 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005940 * the shared secret. A shared secret is permitted wherever a key
5941 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005942 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005943 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005944 shared_secret,
5945 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005946exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005947 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005948 return( status );
5949}
5950
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005951psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005952 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005953 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005954 const uint8_t *peer_key,
5955 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005956{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005957 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005958 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005959 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005960
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005961 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005962 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005963 status = psa_get_and_lock_transparent_key_slot_with_policy(
5964 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005965 if( status != PSA_SUCCESS )
5966 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005967 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005968 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005969 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005970 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005971 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005972 else
5973 {
5974 /* If a private key has been added as SECRET, we allow the derived
5975 * key material to be used as a key in PSA Crypto. */
5976 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5977 operation->can_output_key = 1;
5978 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005979
Ronald Cron5c522922020-11-14 16:35:34 +01005980 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005981
Ronald Cron5c522922020-11-14 16:35:34 +01005982 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005983}
5984
Gilles Peskinebe697d82019-05-16 18:00:41 +02005985psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005986 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005987 const uint8_t *peer_key,
5988 size_t peer_key_length,
5989 uint8_t *output,
5990 size_t output_size,
5991 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005992{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005993 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005994 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005995 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005996
5997 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5998 {
5999 status = PSA_ERROR_INVALID_ARGUMENT;
6000 goto exit;
6001 }
Ronald Cron5c522922020-11-14 16:35:34 +01006002 status = psa_get_and_lock_transparent_key_slot_with_policy(
6003 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006004 if( status != PSA_SUCCESS )
6005 goto exit;
6006
6007 status = psa_key_agreement_raw_internal( alg, slot,
6008 peer_key, peer_key_length,
6009 output, output_size,
6010 output_length );
6011
6012exit:
6013 if( status != PSA_SUCCESS )
6014 {
6015 /* If an error happens and is not handled properly, the output
6016 * may be used as a key to protect sensitive data. Arrange for such
6017 * a key to be random, which is likely to result in decryption or
6018 * verification errors. This is better than filling the buffer with
6019 * some constant data such as zeros, which would result in the data
6020 * being protected with a reproducible, easily knowable key.
6021 */
6022 psa_generate_random( output, output_size );
6023 *output_length = output_size;
6024 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006025
Ronald Cron5c522922020-11-14 16:35:34 +01006026 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006027
Ronald Cron5c522922020-11-14 16:35:34 +01006028 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006029}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006030
6031
Gilles Peskine30524eb2020-11-13 17:02:26 +01006032
Gilles Peskine86a440b2018-11-12 18:39:40 +01006033/****************************************************************/
6034/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006035/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006036
Gilles Peskine30524eb2020-11-13 17:02:26 +01006037/** Initialize the PSA random generator.
6038 */
6039static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6040{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006041#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6042 memset( rng, 0, sizeof( *rng ) );
6043#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6044
Gilles Peskine30524eb2020-11-13 17:02:26 +01006045 /* Set default configuration if
6046 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6047 if( rng->entropy_init == NULL )
6048 rng->entropy_init = mbedtls_entropy_init;
6049 if( rng->entropy_free == NULL )
6050 rng->entropy_free = mbedtls_entropy_free;
6051
6052 rng->entropy_init( &rng->entropy );
6053#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6054 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6055 /* The PSA entropy injection feature depends on using NV seed as an entropy
6056 * source. Add NV seed as an entropy source for PSA entropy injection. */
6057 mbedtls_entropy_add_source( &rng->entropy,
6058 mbedtls_nv_seed_poll, NULL,
6059 MBEDTLS_ENTROPY_BLOCK_SIZE,
6060 MBEDTLS_ENTROPY_SOURCE_STRONG );
6061#endif
6062
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006063 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006064#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006065}
6066
6067/** Deinitialize the PSA random generator.
6068 */
6069static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6070{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006071#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6072 memset( rng, 0, sizeof( *rng ) );
6073#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006074 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006075 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006076#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006077}
6078
6079/** Seed the PSA random generator.
6080 */
6081static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6082{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006083#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6084 /* Do nothing: the external RNG seeds itself. */
6085 (void) rng;
6086 return( PSA_SUCCESS );
6087#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006088 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006089 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6090 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006091 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006092#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006093}
6094
Gilles Peskinee59236f2018-01-27 23:32:46 +01006095psa_status_t psa_generate_random( uint8_t *output,
6096 size_t output_size )
6097{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006098 GUARD_MODULE_INITIALIZED;
6099
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006100#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6101
6102 size_t output_length = 0;
6103 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6104 output, output_size,
6105 &output_length );
6106 if( status != PSA_SUCCESS )
6107 return( status );
6108 /* Breaking up a request into smaller chunks is currently not supported
6109 * for the extrernal RNG interface. */
6110 if( output_length != output_size )
6111 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6112 return( PSA_SUCCESS );
6113
6114#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6115
Gilles Peskine71ddab92021-01-04 21:01:07 +01006116 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006117 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006118 size_t request_size =
6119 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6120 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6121 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006122 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6123 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006124 if( ret != 0 )
6125 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006126 output_size -= request_size;
6127 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006128 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006129 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006130#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006131}
6132
Gilles Peskine8814fc42020-12-14 15:33:44 +01006133/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006134 *
6135 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6136 * `psa_generate_random(...)`. The state parameter is ignored since the
6137 * PSA API doesn't support passing an explicit state.
6138 *
6139 * In the non-external case, psa_generate_random() calls an
6140 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6141 * and semantics as mbedtls_psa_get_random(). As an optimization,
6142 * instead of doing this back-and-forth between the PSA API and the
6143 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6144 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006145 */
6146#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6147int mbedtls_psa_get_random( void *p_rng,
6148 unsigned char *output,
6149 size_t output_size )
6150{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006151 /* This function takes a pointer to the RNG state because that's what
6152 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6153 * its own state internally and doesn't let the caller access that state.
6154 * So we just ignore the state parameter, and in practice we'll pass
6155 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006156 (void) p_rng;
6157 psa_status_t status = psa_generate_random( output, output_size );
6158 if( status == PSA_SUCCESS )
6159 return( 0 );
6160 else
6161 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6162}
6163#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6164
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006165#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6166#include "mbedtls/entropy_poll.h"
6167
Gilles Peskine7228da22019-07-15 11:06:15 +02006168psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006169 size_t seed_size )
6170{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006171 if( global_data.initialized )
6172 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006173
6174 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6175 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6176 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6177 return( PSA_ERROR_INVALID_ARGUMENT );
6178
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006179 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006180}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006181#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006182
John Durkop07cc04a2020-11-16 22:08:34 -08006183#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006184static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6185 size_t domain_parameters_size,
6186 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006187{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006188 size_t i;
6189 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006190
Gilles Peskinee56e8782019-04-26 17:34:02 +02006191 if( domain_parameters_size == 0 )
6192 {
6193 *exponent = 65537;
6194 return( PSA_SUCCESS );
6195 }
6196
6197 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6198 * support values that fit in a 32-bit integer, which is larger than
6199 * int on just about every platform anyway. */
6200 if( domain_parameters_size > sizeof( acc ) )
6201 return( PSA_ERROR_NOT_SUPPORTED );
6202 for( i = 0; i < domain_parameters_size; i++ )
6203 acc = ( acc << 8 ) | domain_parameters[i];
6204 if( acc > INT_MAX )
6205 return( PSA_ERROR_NOT_SUPPORTED );
6206 *exponent = acc;
6207 return( PSA_SUCCESS );
6208}
John Durkop07cc04a2020-11-16 22:08:34 -08006209#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006210
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006211static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006212 psa_key_slot_t *slot, size_t bits,
6213 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006214{
Gilles Peskine8e338702019-07-30 20:06:31 +02006215 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006216
Gilles Peskinee56e8782019-04-26 17:34:02 +02006217 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006218 return( PSA_ERROR_INVALID_ARGUMENT );
6219
Gilles Peskinee59236f2018-01-27 23:32:46 +01006220 if( key_type_is_raw_bytes( type ) )
6221 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006222 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006223
6224 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006225 if( status != PSA_SUCCESS )
6226 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006227
6228 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006229 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6230 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006231 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006232
Ronald Cronea0f8a62020-11-25 17:52:23 +01006233 status = psa_generate_random( slot->key.data,
6234 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006235 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006236 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006237
6238 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006239#if defined(MBEDTLS_DES_C)
6240 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006241 psa_des_set_key_parity( slot->key.data,
6242 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006243#endif /* MBEDTLS_DES_C */
6244 }
6245 else
6246
John Durkop07cc04a2020-11-16 22:08:34 -08006247#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006248 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006249 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006250 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006252 int exponent;
6253 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006254 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6255 return( PSA_ERROR_NOT_SUPPORTED );
6256 /* Accept only byte-aligned keys, for the same reasons as
6257 * in psa_import_rsa_key(). */
6258 if( bits % 8 != 0 )
6259 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006260 status = psa_read_rsa_exponent( domain_parameters,
6261 domain_parameters_size,
6262 &exponent );
6263 if( status != PSA_SUCCESS )
6264 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006265 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6266 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006267 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006268 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006269 (unsigned int) bits,
6270 exponent );
6271 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006272 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006273
6274 /* Make sure to always have an export representation available */
6275 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6276
Steven Cooreman75b74362020-07-28 14:30:13 +02006277 status = psa_allocate_buffer_to_slot( slot, bytes );
6278 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006279 {
6280 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006281 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006282 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006283
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01006284 status = mbedtls_psa_rsa_export_key( type,
6285 &rsa,
6286 slot->key.data,
6287 bytes,
6288 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006289 mbedtls_rsa_free( &rsa );
6290 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006291 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006292 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006293 }
6294 else
John Durkop07cc04a2020-11-16 22:08:34 -08006295#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006296
John Durkop9814fa22020-11-04 12:28:15 -08006297#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006298 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006299 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006300 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006301 mbedtls_ecp_group_id grp_id =
6302 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006303 const mbedtls_ecp_curve_info *curve_info =
6304 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006305 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006307 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006308 return( PSA_ERROR_NOT_SUPPORTED );
6309 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6310 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006311 mbedtls_ecp_keypair_init( &ecp );
6312 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006313 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006314 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006315 if( ret != 0 )
6316 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006317 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006318 return( mbedtls_to_psa_error( ret ) );
6319 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006320
6321
6322 /* Make sure to always have an export representation available */
6323 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006324 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6325 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006326 {
6327 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006328 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006329 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006330
6331 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006332 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006333
6334 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006335 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006336 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006337 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006338 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006339 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006340 }
6341 else
John Durkop9814fa22020-11-04 12:28:15 -08006342#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006343 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006344 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006345 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006346
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006347 return( PSA_SUCCESS );
6348}
Darryl Green0c6575a2018-11-07 16:05:30 +00006349
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006350psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006351 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006352{
6353 psa_status_t status;
6354 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006355 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006356
Ronald Cron81709fc2020-11-14 12:10:32 +01006357 *key = MBEDTLS_SVC_KEY_ID_INIT;
6358
Gilles Peskine0f84d622019-09-12 19:03:13 +02006359 /* Reject any attempt to create a zero-length key so that we don't
6360 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6361 if( psa_get_key_bits( attributes ) == 0 )
6362 return( PSA_ERROR_INVALID_ARGUMENT );
6363
Ronald Cron81709fc2020-11-14 12:10:32 +01006364 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6365 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006366 if( status != PSA_SUCCESS )
6367 goto exit;
6368
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006369 status = psa_driver_wrapper_generate_key( attributes,
6370 slot );
6371 if( status != PSA_ERROR_NOT_SUPPORTED ||
6372 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6373 goto exit;
6374
6375 status = psa_generate_key_internal(
6376 slot, attributes->core.bits,
6377 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006378
6379exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006380 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006381 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006382 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006383 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006384
Darryl Green0c6575a2018-11-07 16:05:30 +00006385 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006386}
6387
6388
Gilles Peskinee59236f2018-01-27 23:32:46 +01006389
6390/****************************************************************/
6391/* Module setup */
6392/****************************************************************/
6393
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006394#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006395psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6396 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6397 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6398{
6399 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6400 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006401 global_data.rng.entropy_init = entropy_init;
6402 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006403 return( PSA_SUCCESS );
6404}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006405#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006406
Gilles Peskinee59236f2018-01-27 23:32:46 +01006407void mbedtls_psa_crypto_free( void )
6408{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006409 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006410 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6411 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006412 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006413 }
6414 /* Wipe all remaining data, including configuration.
6415 * In particular, this sets all state indicator to the value
6416 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006417 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006418#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006419 /* Unregister all secure element drivers, so that we restart from
6420 * a pristine state. */
6421 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006422#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006423}
6424
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006425#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6426/** Recover a transaction that was interrupted by a power failure.
6427 *
6428 * This function is called during initialization, before psa_crypto_init()
6429 * returns. If this function returns a failure status, the initialization
6430 * fails.
6431 */
6432static psa_status_t psa_crypto_recover_transaction(
6433 const psa_crypto_transaction_t *transaction )
6434{
6435 switch( transaction->unknown.type )
6436 {
6437 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6438 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006439 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006440 * is implemented.
6441 * https://github.com/ARMmbed/mbed-crypto/issues/218
6442 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006443 default:
6444 /* We found an unsupported transaction in the storage.
6445 * We don't know what state the storage is in. Give up. */
6446 return( PSA_ERROR_STORAGE_FAILURE );
6447 }
6448}
6449#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6450
Gilles Peskinee59236f2018-01-27 23:32:46 +01006451psa_status_t psa_crypto_init( void )
6452{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006453 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006454
Gilles Peskinec6b69072018-11-20 21:42:52 +01006455 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006456 if( global_data.initialized != 0 )
6457 return( PSA_SUCCESS );
6458
Gilles Peskine30524eb2020-11-13 17:02:26 +01006459 /* Initialize and seed the random generator. */
6460 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006461 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006462 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006463 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006464 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006465 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466
Gilles Peskine66fb1262018-12-10 16:29:04 +01006467 status = psa_initialize_key_slots( );
6468 if( status != PSA_SUCCESS )
6469 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006470
Gilles Peskined9348f22019-10-01 15:22:29 +02006471#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6472 status = psa_init_all_se_drivers( );
6473 if( status != PSA_SUCCESS )
6474 goto exit;
6475#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6476
Gilles Peskine4b734222019-07-24 15:56:31 +02006477#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006478 status = psa_crypto_load_transaction( );
6479 if( status == PSA_SUCCESS )
6480 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006481 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6482 if( status != PSA_SUCCESS )
6483 goto exit;
6484 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006485 }
6486 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6487 {
6488 /* There's no transaction to complete. It's all good. */
6489 status = PSA_SUCCESS;
6490 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006491#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006492
Gilles Peskinec6b69072018-11-20 21:42:52 +01006493 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006494 global_data.initialized = 1;
6495
6496exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006497 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006498 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006499 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006500}
6501
6502#endif /* MBEDTLS_PSA_CRYPTO_C */