blob: 5323bb3d4d08502d4ec0ea82579a31616008b094 [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
544 */
545static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
546 const uint8_t *data,
547 size_t data_length )
548{
549 psa_status_t status;
550 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200551 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200552
Steven Cooremana01795d2020-07-24 22:48:15 +0200553 /* Parse input */
Ronald Cron90857082020-11-25 14:58:33 +0100554 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
555 data,
556 data_length,
557 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200558 if( status != PSA_SUCCESS )
559 goto exit;
560
561 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200562 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200563
Steven Cooreman75b74362020-07-28 14:30:13 +0200564 /* Re-export the data to PSA export format, such that we can store export
565 * representation in the key slot. Export representation in case of RSA is
566 * the smallest representation that's allowed as input, so a straight-up
567 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200568 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 if( output == NULL )
570 {
571 status = PSA_ERROR_INSUFFICIENT_MEMORY;
572 goto exit;
573 }
574
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100575 status = mbedtls_psa_rsa_export_key( slot->attr.type,
576 rsa,
577 output,
578 data_length,
579 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200580exit:
581 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200582 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200583 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200584
585 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000586 if( status != PSA_SUCCESS )
587 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200588 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000589 return( status );
590 }
591
Steven Cooremana01795d2020-07-24 22:48:15 +0200592 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100593 slot->key.data = output;
594 slot->key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000595
596 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200597}
John Durkop6ba40d12020-11-10 08:50:04 -0800598
599#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800600 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200601
John Durkop9814fa22020-11-04 12:28:15 -0800602#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800603 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200604
Steven Cooreman4fed4552020-08-03 14:46:03 +0200605/** Import an ECP key from import representation to a slot
606 *
607 * \param[in,out] slot The slot where to store the export representation to
608 * \param[in] data The buffer containing the import representation
609 * \param[in] data_length The amount of bytes in \p data
610 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200611static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
612 const uint8_t *data,
613 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100614{
Steven Cooremanacda8342020-07-24 23:09:52 +0200615 psa_status_t status;
616 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200617 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100618
Steven Cooremanacda8342020-07-24 23:09:52 +0200619 /* Parse input */
Ronald Cron90857082020-11-25 14:58:33 +0100620 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
621 data,
622 data_length,
623 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100624 if( status != PSA_SUCCESS )
625 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100626
Steven Cooremanacda8342020-07-24 23:09:52 +0200627 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200628 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200629 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200630 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200631
Steven Cooremanacda8342020-07-24 23:09:52 +0200632 /* Re-export the data to PSA export format. There is currently no support
633 * for other input formats then the export format, so this is a 1-1
634 * copy operation. */
635 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200636 if( output == NULL )
637 {
638 status = PSA_ERROR_INSUFFICIENT_MEMORY;
639 goto exit;
640 }
641
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100642 status = mbedtls_psa_ecp_export_key( slot->attr.type,
643 ecp,
644 output,
645 data_length,
646 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100647exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200648 /* Always free the PK object (will also free contained ECP context) */
649 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200650 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200651
652 /* Free the allocated buffer only on error. */
653 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100654 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200655 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200656 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100657 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200658
659 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100660 slot->key.data = output;
661 slot->key.bytes = data_length;
Steven Cooremanacda8342020-07-24 23:09:52 +0200662
663 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100664}
John Durkop9814fa22020-11-04 12:28:15 -0800665#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
666 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667
Gilles Peskineb46bef22019-07-30 21:32:04 +0200668/** Return the size of the key in the given slot, in bits.
669 *
670 * \param[in] slot A key slot.
671 *
672 * \return The key size in bits, read from the metadata in the slot.
673 */
674static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
675{
676 return( slot->attr.bits );
677}
678
Steven Cooreman75b74362020-07-28 14:30:13 +0200679/** Try to allocate a buffer to an empty key slot.
680 *
681 * \param[in,out] slot Key slot to attach buffer to.
682 * \param[in] buffer_length Requested size of the buffer.
683 *
684 * \retval #PSA_SUCCESS
685 * The buffer has been successfully allocated.
686 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
687 * Not enough memory was available for allocation.
688 * \retval #PSA_ERROR_ALREADY_EXISTS
689 * Trying to allocate a buffer to a non-empty key slot.
690 */
691static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
692 size_t buffer_length )
693{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100694 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200695 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200696
Ronald Cronea0f8a62020-11-25 17:52:23 +0100697 slot->key.data = mbedtls_calloc( 1, buffer_length );
698 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200699 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200700
Ronald Cronea0f8a62020-11-25 17:52:23 +0100701 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200702 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200703}
704
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200705psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
706 const uint8_t* data,
707 size_t data_length )
708{
709 psa_status_t status = psa_allocate_buffer_to_slot( slot,
710 data_length );
711 if( status != PSA_SUCCESS )
712 return( status );
713
Ronald Cronea0f8a62020-11-25 17:52:23 +0100714 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200715 return( PSA_SUCCESS );
716}
717
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200718/** Import key data into a slot.
719 *
720 * `slot->type` must have been set previously.
721 * This function assumes that the slot does not contain any key material yet.
722 * On failure, the slot content is unchanged.
723 *
724 * Persistent storage is not affected.
725 *
726 * \param[in,out] slot The key slot to import data into.
727 * Its `type` field must have previously been set to
728 * the desired key type.
729 * It must not contain any key material yet.
730 * \param[in] data Buffer containing the key material to parse and import.
731 * \param data_length Size of \p data in bytes.
732 *
733 * \retval #PSA_SUCCESS
734 * \retval #PSA_ERROR_INVALID_ARGUMENT
735 * \retval #PSA_ERROR_NOT_SUPPORTED
736 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
737 */
738static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
739 const uint8_t *data,
740 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100741{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200742 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +0200743 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100744
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200745 /* zero-length keys are never supported. */
746 if( data_length == 0 )
747 return( PSA_ERROR_NOT_SUPPORTED );
748
Gilles Peskine8e338702019-07-30 20:06:31 +0200749 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 {
Steven Cooreman04524762020-10-13 17:43:44 +0200751 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200752
Steven Cooreman75b74362020-07-28 14:30:13 +0200753 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
754 if( data_length > SIZE_MAX / 8 )
755 return( PSA_ERROR_NOT_SUPPORTED );
756
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200757 /* Enforce a size limit, and in particular ensure that the bit
758 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200759 if( bit_size > PSA_MAX_KEY_BITS )
760 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200761
762 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200763 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200764 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200765
766 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200767 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +0200768 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200769 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200770
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200771 /* Write the actual key size to the slot.
772 * psa_start_key_creation() wrote the size declared by the
773 * caller, which may be 0 (meaning unspecified) or wrong. */
774 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +0100775
776 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200778 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200779 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200780 /* Try validation through accelerators first. */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200781 psa_key_attributes_t attributes = {
782 .core = slot->attr
783 };
Ronald Cron83282872020-11-22 14:02:39 +0100784
785 status = psa_allocate_buffer_to_slot( slot, data_length );
786 if( status != PSA_SUCCESS )
787 return( status );
788
789 bit_size = slot->attr.bits;
790 status = psa_driver_wrapper_import_key( &attributes,
791 data, data_length,
792 slot->key.data,
793 slot->key.bytes,
794 &slot->key.bytes,
795 &bit_size );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200796 if( status == PSA_SUCCESS )
797 {
Ronald Cron83282872020-11-22 14:02:39 +0100798 if( slot->attr.bits == 0 )
799 slot->attr.bits = (psa_key_bits_t) bit_size;
800 else if( bit_size != slot->attr.bits )
801 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200802
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200803 return( PSA_SUCCESS );
804 }
Ronald Cron83282872020-11-22 14:02:39 +0100805 else
806 {
807 if( status != PSA_ERROR_NOT_SUPPORTED )
808 return( status );
809 }
810
811 mbedtls_platform_zeroize( slot->key.data, data_length );
812 mbedtls_free( slot->key.data );
813 slot->key.data = NULL;
814 slot->key.bytes = 0;
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200815
816 /* Key format is not supported by any accelerator, try software fallback
817 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -0800818#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
819 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200820 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
821 {
Steven Cooreman40120f62020-10-29 11:42:22 +0100822 return( psa_import_ecp_key( slot, data, data_length ) );
823 }
John Durkop9814fa22020-11-04 12:28:15 -0800824#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
825 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700826#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
827 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +0100828 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200829 {
Steven Cooreman40120f62020-10-29 11:42:22 +0100830 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200831 }
John Durkop9814fa22020-11-04 12:28:15 -0800832#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
833 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +0100834
835 /* Fell through the fallback as well, so have nothing else to try. */
836 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837 }
838 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100839 {
Steven Cooreman19fd5742020-07-24 23:31:01 +0200840 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100841 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +0100842 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100843}
844
Gilles Peskinef603c712019-01-19 13:40:11 +0100845/** Calculate the intersection of two algorithm usage policies.
846 *
847 * Return 0 (which allows no operation) on incompatibility.
848 */
849static psa_algorithm_t psa_key_policy_algorithm_intersection(
850 psa_algorithm_t alg1,
851 psa_algorithm_t alg2 )
852{
Gilles Peskine549ea862019-05-22 11:45:59 +0200853 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100854 if( alg1 == alg2 )
855 return( alg1 );
856 /* If the policies are from the same hash-and-sign family, check
857 * if one is a wildcard. If so the other has the specific algorithm. */
858 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
859 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
860 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
861 {
862 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
863 return( alg2 );
864 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
865 return( alg1 );
866 }
867 /* If the policies are incompatible, allow nothing. */
868 return( 0 );
869}
870
Gilles Peskined6f371b2019-05-10 19:33:38 +0200871static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
872 psa_algorithm_t requested_alg )
873{
Gilles Peskine549ea862019-05-22 11:45:59 +0200874 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200875 if( requested_alg == policy_alg )
876 return( 1 );
877 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200878 * and requested_alg is the same hash-and-sign family with any hash,
879 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200880 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
881 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
882 {
883 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
884 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
885 }
Steven Cooremance48e852020-10-05 16:02:45 +0200886 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200887 * a key derivation with that key agreement should also be allowed. This
888 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200889 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
890 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
891 {
892 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
893 policy_alg );
894 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200895 /* If it isn't permitted, it's forbidden. */
896 return( 0 );
897}
898
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100899/** Test whether a policy permits an algorithm.
900 *
901 * The caller must test usage flags separately.
902 */
903static int psa_key_policy_permits( const psa_key_policy_t *policy,
904 psa_algorithm_t alg )
905{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200906 return( psa_key_algorithm_permits( policy->alg, alg ) ||
907 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100908}
909
Gilles Peskinef603c712019-01-19 13:40:11 +0100910/** Restrict a key policy based on a constraint.
911 *
912 * \param[in,out] policy The policy to restrict.
913 * \param[in] constraint The policy constraint to apply.
914 *
915 * \retval #PSA_SUCCESS
916 * \c *policy contains the intersection of the original value of
917 * \c *policy and \c *constraint.
918 * \retval #PSA_ERROR_INVALID_ARGUMENT
919 * \c *policy and \c *constraint are incompatible.
920 * \c *policy is unchanged.
921 */
922static psa_status_t psa_restrict_key_policy(
923 psa_key_policy_t *policy,
924 const psa_key_policy_t *constraint )
925{
926 psa_algorithm_t intersection_alg =
927 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200928 psa_algorithm_t intersection_alg2 =
929 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100930 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
931 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200932 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
933 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100934 policy->usage &= constraint->usage;
935 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200936 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100937 return( PSA_SUCCESS );
938}
939
Ronald Cron5c522922020-11-14 16:35:34 +0100940/** Get the description of a key given its identifier and policy constraints
941 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200942 *
Ronald Cron5c522922020-11-14 16:35:34 +0100943 * The key must have allow all the usage flags set in \p usage. If \p alg is
944 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +0100945 *
Ronald Cron5c522922020-11-14 16:35:34 +0100946 * In case of a persistent key, the function loads the description of the key
947 * into a key slot if not already done.
948 *
949 * On success, the returned key slot is locked. It is the responsibility of
950 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200951 */
Ronald Cron5c522922020-11-14 16:35:34 +0100952static psa_status_t psa_get_and_lock_key_slot_with_policy(
953 mbedtls_svc_key_id_t key,
954 psa_key_slot_t **p_slot,
955 psa_key_usage_t usage,
956 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100957{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200958 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
959 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100960
Ronald Cron5c522922020-11-14 16:35:34 +0100961 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100962 if( status != PSA_SUCCESS )
963 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200964 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100965
966 /* Enforce that usage policy for the key slot contains all the flags
967 * required by the usage parameter. There is one exception: public
968 * keys can always be exported, so we treat public key objects as
969 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200970 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100971 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200972
973 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200975 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100976
977 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200978 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200979 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +0100980
Darryl Green06fd18d2018-07-16 11:21:11 +0100981 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200982
983error:
984 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100985 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200986
987 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100988}
Darryl Green940d72c2018-07-13 13:18:51 +0100989
Ronald Cron5c522922020-11-14 16:35:34 +0100990/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200991 *
992 * A transparent key is a key for which the key material is directly
993 * available, as opposed to a key in a secure element.
994 *
Ronald Cron5c522922020-11-14 16:35:34 +0100995 * This is a temporary function to use instead of
996 * psa_get_and_lock_key_slot_with_policy() until secure element support is
997 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200998 *
Ronald Cron5c522922020-11-14 16:35:34 +0100999 * On success, the returned key slot is locked. It is the responsibility of the
1000 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001001 */
1002#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001003static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1004 mbedtls_svc_key_id_t key,
1005 psa_key_slot_t **p_slot,
1006 psa_key_usage_t usage,
1007 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001008{
Ronald Cron5c522922020-11-14 16:35:34 +01001009 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1010 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001011 if( status != PSA_SUCCESS )
1012 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001013
Gilles Peskineadad8132019-07-25 11:31:23 +02001014 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001015 {
Ronald Cron5c522922020-11-14 16:35:34 +01001016 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001017 *p_slot = NULL;
1018 return( PSA_ERROR_NOT_SUPPORTED );
1019 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001020
Gilles Peskine28f8f302019-07-24 13:30:31 +02001021 return( PSA_SUCCESS );
1022}
1023#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1024/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001025#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1026 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001027#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1028
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001029/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001030static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001031{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001032 /* Data pointer will always be either a valid pointer or NULL in an
1033 * initialized slot, so we can just free it. */
1034 if( slot->key.data != NULL )
1035 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1036
1037 mbedtls_free( slot->key.data );
1038 slot->key.data = NULL;
1039 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001040
1041 return( PSA_SUCCESS );
1042}
1043
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001044/** Completely wipe a slot in memory, including its policy.
1045 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001046psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001047{
1048 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001049
1050 /*
1051 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001052 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001053 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1054 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001055 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001056 */
Ronald Cron5c522922020-11-14 16:35:34 +01001057 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001058 {
1059#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001060 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001061#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001062 status = PSA_ERROR_CORRUPTION_DETECTED;
1063 }
1064
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001065 /* Multipart operations may still be using the key. This is safe
1066 * because all multipart operation objects are independent from
1067 * the key slot: if they need to access the key after the setup
1068 * phase, they have a copy of the key. Note that this means that
1069 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001070 /* At this point, key material and other type-specific content has
1071 * been wiped. Clear remaining metadata. We can call memset and not
1072 * zeroize because the metadata is not particularly sensitive. */
1073 memset( slot, 0, sizeof( *slot ) );
1074 return( status );
1075}
1076
Ronald Croncf56a0a2020-08-04 09:51:30 +02001077psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001078{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001079 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001080 psa_status_t status; /* status of the last operation */
1081 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001082#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1083 psa_se_drv_table_entry_t *driver;
1084#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001085
Ronald Croncf56a0a2020-08-04 09:51:30 +02001086 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001087 return( PSA_SUCCESS );
1088
Ronald Cronf2911112020-10-29 17:51:10 +01001089 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001090 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001091 * key, this will load the key description from persistent memory if not
1092 * done yet. We cannot avoid this loading as without it we don't know if
1093 * the key is operated by an SE or not and this information is needed by
1094 * the current implementation.
1095 */
Ronald Cron5c522922020-11-14 16:35:34 +01001096 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001097 if( status != PSA_SUCCESS )
1098 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001099
Ronald Cronf2911112020-10-29 17:51:10 +01001100 /*
1101 * If the key slot containing the key description is under access by the
1102 * library (apart from the present access), the key cannot be destroyed
1103 * yet. For the time being, just return in error. Eventually (to be
1104 * implemented), the key should be destroyed when all accesses have
1105 * stopped.
1106 */
Ronald Cron5c522922020-11-14 16:35:34 +01001107 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001108 {
Ronald Cron5c522922020-11-14 16:35:34 +01001109 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001110 return( PSA_ERROR_GENERIC_ERROR );
1111 }
1112
Gilles Peskine354f7672019-07-12 23:46:38 +02001113#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001114 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001115 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001116 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001117 /* For a key in a secure element, we need to do three things:
1118 * remove the key file in internal storage, destroy the
1119 * key inside the secure element, and update the driver's
1120 * persistent data. Start a transaction that will encompass these
1121 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001122 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001123 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001124 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001125 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001126 status = psa_crypto_save_transaction( );
1127 if( status != PSA_SUCCESS )
1128 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001129 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001130 /* We should still try to destroy the key in the secure
1131 * element and the key metadata in storage. This is especially
1132 * important if the error is that the storage is full.
1133 * But how to do it exactly without risking an inconsistent
1134 * state after a reset?
1135 * https://github.com/ARMmbed/mbed-crypto/issues/215
1136 */
1137 overall_status = status;
1138 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001139 }
1140
Ronald Cronea0f8a62020-11-25 17:52:23 +01001141 status = psa_destroy_se_key( driver,
1142 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001143 if( overall_status == PSA_SUCCESS )
1144 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001145 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001146#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1147
Darryl Greend49a4992018-06-18 17:27:26 +01001148#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001149 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001150 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001151 status = psa_destroy_persistent_key( slot->attr.id );
1152 if( overall_status == PSA_SUCCESS )
1153 overall_status = status;
1154
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001155 /* TODO: other slots may have a copy of the same key. We should
1156 * invalidate them.
1157 * https://github.com/ARMmbed/mbed-crypto/issues/214
1158 */
Darryl Greend49a4992018-06-18 17:27:26 +01001159 }
1160#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001161
Gilles Peskinefc762652019-07-22 19:30:34 +02001162#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1163 if( driver != NULL )
1164 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001165 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001166 if( overall_status == PSA_SUCCESS )
1167 overall_status = status;
1168 status = psa_crypto_stop_transaction( );
1169 if( overall_status == PSA_SUCCESS )
1170 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001171 }
1172#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1173
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001174#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1175exit:
1176#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001177 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001178 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1179 if( overall_status == PSA_SUCCESS )
1180 overall_status = status;
1181 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001182}
1183
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001184void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001185{
Gilles Peskineb699f072019-04-26 16:06:02 +02001186 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001187 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001188}
1189
Gilles Peskineb699f072019-04-26 16:06:02 +02001190psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1191 psa_key_type_t type,
1192 const uint8_t *data,
1193 size_t data_length )
1194{
1195 uint8_t *copy = NULL;
1196
1197 if( data_length != 0 )
1198 {
1199 copy = mbedtls_calloc( 1, data_length );
1200 if( copy == NULL )
1201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1202 memcpy( copy, data, data_length );
1203 }
1204 /* After this point, this function is guaranteed to succeed, so it
1205 * can start modifying `*attributes`. */
1206
1207 if( attributes->domain_parameters != NULL )
1208 {
1209 mbedtls_free( attributes->domain_parameters );
1210 attributes->domain_parameters = NULL;
1211 attributes->domain_parameters_size = 0;
1212 }
1213
1214 attributes->domain_parameters = copy;
1215 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001216 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001217 return( PSA_SUCCESS );
1218}
1219
1220psa_status_t psa_get_key_domain_parameters(
1221 const psa_key_attributes_t *attributes,
1222 uint8_t *data, size_t data_size, size_t *data_length )
1223{
1224 if( attributes->domain_parameters_size > data_size )
1225 return( PSA_ERROR_BUFFER_TOO_SMALL );
1226 *data_length = attributes->domain_parameters_size;
1227 if( attributes->domain_parameters_size != 0 )
1228 memcpy( data, attributes->domain_parameters,
1229 attributes->domain_parameters_size );
1230 return( PSA_SUCCESS );
1231}
1232
John Durkop07cc04a2020-11-16 22:08:34 -08001233#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001234 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001235static psa_status_t psa_get_rsa_public_exponent(
1236 const mbedtls_rsa_context *rsa,
1237 psa_key_attributes_t *attributes )
1238{
1239 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001241 uint8_t *buffer = NULL;
1242 size_t buflen;
1243 mbedtls_mpi_init( &mpi );
1244
1245 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1246 if( ret != 0 )
1247 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001248 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1249 {
1250 /* It's the default value, which is reported as an empty string,
1251 * so there's nothing to do. */
1252 goto exit;
1253 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001254
1255 buflen = mbedtls_mpi_size( &mpi );
1256 buffer = mbedtls_calloc( 1, buflen );
1257 if( buffer == NULL )
1258 {
1259 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1260 goto exit;
1261 }
1262 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1263 if( ret != 0 )
1264 goto exit;
1265 attributes->domain_parameters = buffer;
1266 attributes->domain_parameters_size = buflen;
1267
1268exit:
1269 mbedtls_mpi_free( &mpi );
1270 if( ret != 0 )
1271 mbedtls_free( buffer );
1272 return( mbedtls_to_psa_error( ret ) );
1273}
John Durkop07cc04a2020-11-16 22:08:34 -08001274#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001275 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001276
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001277/** Retrieve all the publicly-accessible attributes of a key.
1278 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001279psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001280 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001281{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001283 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001284 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001286 psa_reset_key_attributes( attributes );
1287
Ronald Cron5c522922020-11-14 16:35:34 +01001288 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001289 if( status != PSA_SUCCESS )
1290 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001291
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001292 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001293 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1294 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1295
1296#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1297 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001298 psa_set_key_slot_number( attributes,
1299 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001300#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001301
Gilles Peskine8e338702019-07-30 20:06:31 +02001302 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001303 {
John Durkop07cc04a2020-11-16 22:08:34 -08001304#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001305 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001306 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001307 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001308#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001309 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001310 * is not yet implemented.
1311 * https://github.com/ARMmbed/mbed-crypto/issues/216
1312 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001313 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001314 break;
1315#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001316 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001317 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001318
Ronald Cron90857082020-11-25 14:58:33 +01001319 status = mbedtls_psa_rsa_load_representation(
1320 slot->attr.type,
1321 slot->key.data,
1322 slot->key.bytes,
1323 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001324 if( status != PSA_SUCCESS )
1325 break;
1326
Steven Cooremana2371e52020-07-28 14:30:39 +02001327 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001328 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001329 mbedtls_rsa_free( rsa );
1330 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001331 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001332 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001333#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001334 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001335 default:
1336 /* Nothing else to do. */
1337 break;
1338 }
1339
1340 if( status != PSA_SUCCESS )
1341 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001342
Ronald Cron5c522922020-11-14 16:35:34 +01001343 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001344
Ronald Cron5c522922020-11-14 16:35:34 +01001345 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001346}
1347
Gilles Peskinec8000c02019-08-02 20:15:51 +02001348#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1349psa_status_t psa_get_key_slot_number(
1350 const psa_key_attributes_t *attributes,
1351 psa_key_slot_number_t *slot_number )
1352{
1353 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1354 {
1355 *slot_number = attributes->slot_number;
1356 return( PSA_SUCCESS );
1357 }
1358 else
1359 return( PSA_ERROR_INVALID_ARGUMENT );
1360}
1361#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1362
Ronald Crond18b5f82020-11-25 16:46:05 +01001363static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1364 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001365 uint8_t *data,
1366 size_t data_size,
1367 size_t *data_length )
1368{
Ronald Crond18b5f82020-11-25 16:46:05 +01001369 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001370 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001371 memcpy( data, key_buffer, key_buffer_size );
1372 memset( data + key_buffer_size, 0,
1373 data_size - key_buffer_size );
1374 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001375 return( PSA_SUCCESS );
1376}
1377
Ronald Cron7285cda2020-11-26 14:46:37 +01001378psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001379 const psa_key_attributes_t *attributes,
1380 const uint8_t *key_buffer, size_t key_buffer_size,
1381 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001382{
Ronald Crond18b5f82020-11-25 16:46:05 +01001383 psa_key_type_t type = attributes->core.type;
1384
Ronald Crond18b5f82020-11-25 16:46:05 +01001385 if( key_type_is_raw_bytes( type ) ||
1386 PSA_KEY_TYPE_IS_RSA( type ) ||
1387 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 {
1389 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001390 key_buffer, key_buffer_size,
1391 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001392 }
1393 else
1394 {
1395 /* This shouldn't happen in the reference implementation, but
1396 it is valid for a special-purpose implementation to omit
1397 support for exporting certain key types. */
1398 return( PSA_ERROR_NOT_SUPPORTED );
1399 }
1400}
1401
1402psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1403 uint8_t *data,
1404 size_t data_size,
1405 size_t *data_length )
1406{
1407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1408 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1409 psa_key_slot_t *slot;
1410
1411 /* Set the key to empty now, so that even when there are errors, we always
1412 * set data_length to a value between 0 and data_size. On error, setting
1413 * the key to empty is a good choice because an empty key representation is
1414 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001415 *data_length = 0;
1416
Ronald Cron9486f9d2020-11-26 13:36:23 +01001417 /* Export requires the EXPORT flag. There is an exception for public keys,
1418 * which don't require any flag, but
1419 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1420 */
1421 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1422 PSA_KEY_USAGE_EXPORT, 0 );
1423 if( status != PSA_SUCCESS )
1424 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001425
Gilles Peskinef9168942019-09-12 19:20:29 +02001426 /* Reject a zero-length output buffer now, since this can never be a
1427 * valid key representation. This way we know that data must be a valid
1428 * pointer and we can do things like memset(data, ..., data_size). */
1429 if( data_size == 0 )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001430 {
1431 status = PSA_ERROR_BUFFER_TOO_SMALL;
1432 goto exit;
1433 }
1434
Ronald Crond18b5f82020-11-25 16:46:05 +01001435 psa_key_attributes_t attributes = {
1436 .core = slot->attr
1437 };
Ronald Cron67227982020-11-26 15:16:05 +01001438 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001439 slot->key.data, slot->key.bytes,
1440 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001441
1442exit:
1443 unlock_status = psa_unlock_key_slot( slot );
1444
1445 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1446}
1447
Ronald Cron7285cda2020-11-26 14:46:37 +01001448psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001449 const psa_key_attributes_t *attributes,
1450 const uint8_t *key_buffer,
1451 size_t key_buffer_size,
1452 uint8_t *data,
1453 size_t data_size,
1454 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001455{
Ronald Crond18b5f82020-11-25 16:46:05 +01001456 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001457
Ronald Crond18b5f82020-11-25 16:46:05 +01001458 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001459 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001460 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001461 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001462 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001463 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001464 key_buffer, key_buffer_size,
1465 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001466 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001467
Ronald Crond18b5f82020-11-25 16:46:05 +01001468 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001469 {
John Durkop0e005192020-10-31 22:06:54 -07001470#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1471 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001472 return( mbedtls_psa_rsa_export_public_key( attributes,
1473 key_buffer,
1474 key_buffer_size,
1475 data,
1476 data_size,
1477 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001478#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001479 /* We don't know how to convert a private RSA key to public. */
1480 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001481#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1482 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001483 }
1484 else
Moran Pekera998bc62018-04-16 18:16:20 +03001485 {
John Durkop6ba40d12020-11-10 08:50:04 -08001486#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1487 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001488 return( mbedtls_psa_ecp_export_public_key( attributes,
1489 key_buffer,
1490 key_buffer_size,
1491 data,
1492 data_size,
1493 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001494#else
1495 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001496 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001497#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1498 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001499 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001501 else
1502 {
1503 /* This shouldn't happen in the reference implementation, but
1504 it is valid for a special-purpose implementation to omit
1505 support for exporting certain key types. */
1506 return( PSA_ERROR_NOT_SUPPORTED );
1507 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001508}
Ronald Crond18b5f82020-11-25 16:46:05 +01001509
Ronald Croncf56a0a2020-08-04 09:51:30 +02001510psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001511 uint8_t *data,
1512 size_t data_size,
1513 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001514{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001515 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001516 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001517 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001518
1519 /* Set the key to empty now, so that even when there are errors, we always
1520 * set data_length to a value between 0 and data_size. On error, setting
1521 * the key to empty is a good choice because an empty key representation is
1522 * unlikely to be accepted anywhere. */
1523 *data_length = 0;
1524
1525 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001526 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001527 if( status != PSA_SUCCESS )
1528 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001529
Ronald Cron9486f9d2020-11-26 13:36:23 +01001530 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1531 {
1532 status = PSA_ERROR_INVALID_ARGUMENT;
1533 goto exit;
1534 }
1535
1536 /* Reject a zero-length output buffer now, since this can never be a
1537 * valid key representation. This way we know that data must be a valid
1538 * pointer and we can do things like memset(data, ..., data_size). */
1539 if( data_size == 0 )
1540 {
1541 status = PSA_ERROR_BUFFER_TOO_SMALL;
1542 goto exit;
1543 }
1544
Ronald Crond18b5f82020-11-25 16:46:05 +01001545 psa_key_attributes_t attributes = {
1546 .core = slot->attr
1547 };
Ronald Cron67227982020-11-26 15:16:05 +01001548 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001549 &attributes, slot->key.data, slot->key.bytes,
1550 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001551
1552exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001553 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001554
Ronald Cron5c522922020-11-14 16:35:34 +01001555 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001556}
1557
Gilles Peskine91e8c332019-08-02 19:19:39 +02001558#if defined(static_assert)
1559static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1560 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001561static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001562 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001563static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001564 "One or more key attribute flag is listed as both internal-only and external-only" );
1565#endif
1566
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001567/** Validate that a key policy is internally well-formed.
1568 *
1569 * This function only rejects invalid policies. It does not validate the
1570 * consistency of the policy with respect to other attributes of the key
1571 * such as the key type.
1572 */
1573static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001574{
1575 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001576 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001577 PSA_KEY_USAGE_ENCRYPT |
1578 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001579 PSA_KEY_USAGE_SIGN_HASH |
1580 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001581 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1582 return( PSA_ERROR_INVALID_ARGUMENT );
1583
Gilles Peskine4747d192019-04-17 15:05:45 +02001584 return( PSA_SUCCESS );
1585}
1586
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001587/** Validate the internal consistency of key attributes.
1588 *
1589 * This function only rejects invalid attribute values. If does not
1590 * validate the consistency of the attributes with any key data that may
1591 * be involved in the creation of the key.
1592 *
1593 * Call this function early in the key creation process.
1594 *
1595 * \param[in] attributes Key attributes for the new key.
1596 * \param[out] p_drv On any return, the driver for the key, if any.
1597 * NULL for a transparent key.
1598 *
1599 */
1600static psa_status_t psa_validate_key_attributes(
1601 const psa_key_attributes_t *attributes,
1602 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001603{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001604 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001605 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001606 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001607
Ronald Cron54b90082020-10-29 15:26:43 +01001608 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001609 if( status != PSA_SUCCESS )
1610 return( status );
1611
Ronald Crond2ed4812020-07-17 16:11:30 +02001612 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001613 if( status != PSA_SUCCESS )
1614 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001615
Ronald Cron65f38a32020-10-23 17:11:13 +02001616 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1617 {
1618 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1619 return( PSA_ERROR_INVALID_ARGUMENT );
1620 }
1621 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001622 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001623 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001624 if( status != PSA_SUCCESS )
1625 return( status );
1626 }
1627
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001628 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001629 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001630 return( status );
1631
1632 /* Refuse to create overly large keys.
1633 * Note that this doesn't trigger on import if the attributes don't
1634 * explicitly specify a size (so psa_get_key_bits returns 0), so
1635 * psa_import_key() needs its own checks. */
1636 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1637 return( PSA_ERROR_NOT_SUPPORTED );
1638
Gilles Peskine91e8c332019-08-02 19:19:39 +02001639 /* Reject invalid flags. These should not be reachable through the API. */
1640 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1641 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1642 return( PSA_ERROR_INVALID_ARGUMENT );
1643
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001644 return( PSA_SUCCESS );
1645}
1646
Gilles Peskine4747d192019-04-17 15:05:45 +02001647/** Prepare a key slot to receive key material.
1648 *
1649 * This function allocates a key slot and sets its metadata.
1650 *
1651 * If this function fails, call psa_fail_key_creation().
1652 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001653 * This function is intended to be used as follows:
1654 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001655 * it with the specified attributes, and in case of a volatile key assign it
1656 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001657 * -# Populate the slot with the key material.
1658 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1659 * In case of failure at any step, stop the sequence and call
1660 * psa_fail_key_creation().
1661 *
Ronald Cron5c522922020-11-14 16:35:34 +01001662 * On success, the key slot is locked. It is the responsibility of the caller
1663 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001664 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001665 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001666 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001667 * \param[out] p_slot On success, a pointer to the prepared slot.
1668 * \param[out] p_drv On any return, the driver for the key, if any.
1669 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001670 *
1671 * \retval #PSA_SUCCESS
1672 * The key slot is ready to receive key material.
1673 * \return If this function fails, the key slot is an invalid state.
1674 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001675 */
1676static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001677 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001678 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001679 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001680 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001681{
1682 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001683 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001684 psa_key_slot_t *slot;
1685
Gilles Peskinedf179142019-07-15 22:02:14 +02001686 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001687 *p_drv = NULL;
1688
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001689 status = psa_validate_key_attributes( attributes, p_drv );
1690 if( status != PSA_SUCCESS )
1691 return( status );
1692
Ronald Cronc4d1b512020-07-31 11:26:37 +02001693 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001694 if( status != PSA_SUCCESS )
1695 return( status );
1696 slot = *p_slot;
1697
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001698 /* We're storing the declared bit-size of the key. It's up to each
1699 * creation mechanism to verify that this information is correct.
1700 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001701 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001702 * is optional (import, copy). In case of a volatile key, assign it the
1703 * volatile key identifier associated to the slot returned to contain its
1704 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001705
1706 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001707 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1708 {
1709#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1710 slot->attr.id = volatile_key_id;
1711#else
1712 slot->attr.id.key_id = volatile_key_id;
1713#endif
1714 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001715
Gilles Peskine91e8c332019-08-02 19:19:39 +02001716 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001717 * external-only flags, query `attributes`. Thanks to the check
1718 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001719 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001720 * may have set. */
1721 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001722
Gilles Peskinecbaff462019-07-12 23:46:04 +02001723#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001724 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001725 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001726 * create the key file in internal storage, create the
1727 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001728 * persistent data. This is done by starting a transaction that will
1729 * encompass these three actions.
1730 * For registering a volatile key, we just need to find an appropriate
1731 * slot number inside the SE. Since the key is designated volatile, creating
1732 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001733 /* The first thing to do is to find a slot number for the new key.
1734 * We save the slot number in persistent storage as part of the
1735 * transaction data. It will be needed to recover if the power
1736 * fails during the key creation process, to clean up on the secure
1737 * element side after restarting. Obtaining a slot number from the
1738 * secure element driver updates its persistent state, but we do not yet
1739 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001740 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001741 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001742 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001743 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001744 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001745 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001746 if( status != PSA_SUCCESS )
1747 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001748
1749 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001750 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001751 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1752 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001753 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001754 psa_crypto_transaction.key.id = slot->attr.id;
1755 status = psa_crypto_save_transaction( );
1756 if( status != PSA_SUCCESS )
1757 {
1758 (void) psa_crypto_stop_transaction( );
1759 return( status );
1760 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001761 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001762
1763 status = psa_copy_key_material_into_slot(
1764 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001765 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001766
1767 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1768 {
1769 /* Key registration only makes sense with a secure element. */
1770 return( PSA_ERROR_INVALID_ARGUMENT );
1771 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001772#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1773
Ronald Cronc4d1b512020-07-31 11:26:37 +02001774 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001775}
Gilles Peskine4747d192019-04-17 15:05:45 +02001776
1777/** Finalize the creation of a key once its key material has been set.
1778 *
1779 * This entails writing the key to persistent storage.
1780 *
1781 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001782 * See the documentation of psa_start_key_creation() for the intended use
1783 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001784 *
Ronald Cron5c522922020-11-14 16:35:34 +01001785 * If the finalization succeeds, the function unlocks the key slot (it was
1786 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1787 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001788 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001789 * \param[in,out] slot Pointer to the slot with key material.
1790 * \param[in] driver The secure element driver for the key,
1791 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001792 * \param[out] key On success, identifier of the key. Note that the
1793 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001794 *
1795 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001796 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001797 * \return If this function fails, the key slot is an invalid state.
1798 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001799 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001800static psa_status_t psa_finish_key_creation(
1801 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001802 psa_se_drv_table_entry_t *driver,
1803 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001804{
1805 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001806 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001807 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001808
1809#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001810 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001811 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001812#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1813 if( driver != NULL )
1814 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001815 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001816 psa_key_slot_number_t slot_number =
1817 psa_key_slot_get_slot_number( slot ) ;
1818
Gilles Peskineb46bef22019-07-30 21:32:04 +02001819#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001820 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001821 sizeof( data.slot_number ),
1822 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001823#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001824 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001825 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001826 (uint8_t*) &data,
1827 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001828 }
1829 else
1830#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1831 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001832 /* Key material is saved in export representation in the slot, so
1833 * just pass the slot buffer for storage. */
1834 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001835 slot->key.data,
1836 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001837 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001838 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001839#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1840
Gilles Peskinecbaff462019-07-12 23:46:04 +02001841#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001842 /* Finish the transaction for a key creation. This does not
1843 * happen when registering an existing key. Detect this case
1844 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001845 * creation of a persistent key in a secure element requires a transaction,
1846 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001847 if( driver != NULL &&
1848 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001849 {
1850 status = psa_save_se_persistent_data( driver );
1851 if( status != PSA_SUCCESS )
1852 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001853 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001854 return( status );
1855 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001856 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001857 }
1858#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1859
Ronald Cron50972942020-11-14 11:28:25 +01001860 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001861 {
1862 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001863 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001864 if( status != PSA_SUCCESS )
1865 *key = MBEDTLS_SVC_KEY_ID_INIT;
1866 }
Ronald Cron50972942020-11-14 11:28:25 +01001867
Gilles Peskine4747d192019-04-17 15:05:45 +02001868 return( status );
1869}
1870
1871/** Abort the creation of a key.
1872 *
1873 * You may call this function after calling psa_start_key_creation(),
1874 * or after psa_finish_key_creation() fails. In other circumstances, this
1875 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001876 * See the documentation of psa_start_key_creation() for the intended use
1877 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001878 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001879 * \param[in,out] slot Pointer to the slot with key material.
1880 * \param[in] driver The secure element driver for the key,
1881 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001882 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001883static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001884 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001885{
Gilles Peskine011e4282019-06-26 18:34:38 +02001886 (void) driver;
1887
Gilles Peskine4747d192019-04-17 15:05:45 +02001888 if( slot == NULL )
1889 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001890
1891#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001892 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001893 * element, and the failure happened later (when saving metadata
1894 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001895 * element.
1896 * https://github.com/ARMmbed/mbed-crypto/issues/217
1897 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001898
Gilles Peskined7729582019-08-05 15:55:54 +02001899 /* Abort the ongoing transaction if any (there may not be one if
1900 * the creation process failed before starting one, or if the
1901 * key creation is a registration of a key in a secure element).
1902 * Earlier functions must already have done what it takes to undo any
1903 * partial creation. All that's left is to update the transaction data
1904 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001905 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001906#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1907
Gilles Peskine4747d192019-04-17 15:05:45 +02001908 psa_wipe_key_slot( slot );
1909}
1910
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001911/** Validate optional attributes during key creation.
1912 *
1913 * Some key attributes are optional during key creation. If they are
1914 * specified in the attributes structure, check that they are consistent
1915 * with the data in the slot.
1916 *
1917 * This function should be called near the end of key creation, after
1918 * the slot in memory is fully populated but before saving persistent data.
1919 */
1920static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001921 const psa_key_slot_t *slot,
1922 const psa_key_attributes_t *attributes )
1923{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001924 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001925 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001926 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001927 return( PSA_ERROR_INVALID_ARGUMENT );
1928 }
1929
1930 if( attributes->domain_parameters_size != 0 )
1931 {
John Durkop0e005192020-10-31 22:06:54 -07001932#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1933 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001934 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001935 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001936 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001937 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001939
Ronald Cron90857082020-11-25 14:58:33 +01001940 psa_status_t status = mbedtls_psa_rsa_load_representation(
1941 slot->attr.type,
1942 slot->key.data,
1943 slot->key.bytes,
1944 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001945 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001946 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001947
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001948 mbedtls_mpi_init( &actual );
1949 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001950 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001951 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001952 mbedtls_rsa_free( rsa );
1953 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001954 if( ret != 0 )
1955 goto rsa_exit;
1956 ret = mbedtls_mpi_read_binary( &required,
1957 attributes->domain_parameters,
1958 attributes->domain_parameters_size );
1959 if( ret != 0 )
1960 goto rsa_exit;
1961 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1962 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1963 rsa_exit:
1964 mbedtls_mpi_free( &actual );
1965 mbedtls_mpi_free( &required );
1966 if( ret != 0)
1967 return( mbedtls_to_psa_error( ret ) );
1968 }
1969 else
John Durkop9814fa22020-11-04 12:28:15 -08001970#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1971 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001972 {
1973 return( PSA_ERROR_INVALID_ARGUMENT );
1974 }
1975 }
1976
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001977 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001978 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001979 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001980 return( PSA_ERROR_INVALID_ARGUMENT );
1981 }
1982
1983 return( PSA_SUCCESS );
1984}
1985
Gilles Peskine4747d192019-04-17 15:05:45 +02001986psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001987 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001988 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001989 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001990{
1991 psa_status_t status;
1992 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001993 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994
Ronald Cron81709fc2020-11-14 12:10:32 +01001995 *key = MBEDTLS_SVC_KEY_ID_INIT;
1996
Gilles Peskine0f84d622019-09-12 19:03:13 +02001997 /* Reject zero-length symmetric keys (including raw data key objects).
1998 * This also rejects any key which might be encoded as an empty string,
1999 * which is never valid. */
2000 if( data_length == 0 )
2001 return( PSA_ERROR_INVALID_ARGUMENT );
2002
Gilles Peskinedf179142019-07-15 22:02:14 +02002003 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002004 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005 if( status != PSA_SUCCESS )
2006 goto exit;
2007
Gilles Peskine5d309672019-07-12 23:47:28 +02002008#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2009 if( driver != NULL )
2010 {
2011 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002012 /* The driver should set the number of key bits, however in
2013 * case it doesn't, we initialize bits to an invalid value. */
2014 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002015 if( drv->key_management == NULL ||
2016 drv->key_management->p_import == NULL )
2017 {
2018 status = PSA_ERROR_NOT_SUPPORTED;
2019 goto exit;
2020 }
2021 status = drv->key_management->p_import(
2022 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01002023 psa_key_slot_get_slot_number( slot ),
2024 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002025 if( status != PSA_SUCCESS )
2026 goto exit;
2027 if( bits > PSA_MAX_KEY_BITS )
2028 {
2029 status = PSA_ERROR_NOT_SUPPORTED;
2030 goto exit;
2031 }
2032 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002033 }
2034 else
2035#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002036 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2037 {
2038 /* Importing a key with external lifetime through the driver wrapper
2039 * interface is not yet supported. Return as if this was an invalid
2040 * lifetime. */
2041 status = PSA_ERROR_INVALID_ARGUMENT;
2042 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002043 }
2044 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002045 {
2046 status = psa_import_key_into_slot( slot, data, data_length );
2047 if( status != PSA_SUCCESS )
2048 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002049 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002050 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002051 if( status != PSA_SUCCESS )
2052 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002053
Ronald Cron81709fc2020-11-14 12:10:32 +01002054 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002055exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002056 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002057 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002058
Gilles Peskine4747d192019-04-17 15:05:45 +02002059 return( status );
2060}
2061
Gilles Peskined7729582019-08-05 15:55:54 +02002062#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2063psa_status_t mbedtls_psa_register_se_key(
2064 const psa_key_attributes_t *attributes )
2065{
2066 psa_status_t status;
2067 psa_key_slot_t *slot = NULL;
2068 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002069 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002070
2071 /* Leaving attributes unspecified is not currently supported.
2072 * It could make sense to query the key type and size from the
2073 * secure element, but not all secure elements support this
2074 * and the driver HAL doesn't currently support it. */
2075 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2076 return( PSA_ERROR_NOT_SUPPORTED );
2077 if( psa_get_key_bits( attributes ) == 0 )
2078 return( PSA_ERROR_NOT_SUPPORTED );
2079
2080 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002082 if( status != PSA_SUCCESS )
2083 goto exit;
2084
Ronald Cron81709fc2020-11-14 12:10:32 +01002085 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002086
2087exit:
2088 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002089 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002090
Gilles Peskined7729582019-08-05 15:55:54 +02002091 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002092 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002093 return( status );
2094}
2095#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2096
Gilles Peskinef603c712019-01-19 13:40:11 +01002097static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002098 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002099{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002100 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002101 source->key.data,
2102 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002103 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002104 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002105
Steven Cooreman398aee52020-10-13 14:35:45 +02002106 target->attr.type = source->attr.type;
2107 target->attr.bits = source->attr.bits;
2108
2109 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002110}
2111
Ronald Croncf56a0a2020-08-04 09:51:30 +02002112psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002113 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002114 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002115{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002117 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002118 psa_key_slot_t *source_slot = NULL;
2119 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002120 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002121 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002122
Ronald Cron81709fc2020-11-14 12:10:32 +01002123 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2124
Ronald Cron5c522922020-11-14 16:35:34 +01002125 status = psa_get_and_lock_transparent_key_slot_with_policy(
2126 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002127 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002128 goto exit;
2129
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002130 status = psa_validate_optional_attributes( source_slot,
2131 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002132 if( status != PSA_SUCCESS )
2133 goto exit;
2134
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002135 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002136 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002137 if( status != PSA_SUCCESS )
2138 goto exit;
2139
Ronald Cron81709fc2020-11-14 12:10:32 +01002140 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2141 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002142 if( status != PSA_SUCCESS )
2143 goto exit;
2144
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002145#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2146 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002147 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002148 /* Copying to a secure element is not implemented yet. */
2149 status = PSA_ERROR_NOT_SUPPORTED;
2150 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002151 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002152#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002153
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002154 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002155 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002156 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002157
Ronald Cron81709fc2020-11-14 12:10:32 +01002158 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002159exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002160 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002161 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002162
Ronald Cron5c522922020-11-14 16:35:34 +01002163 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002164
Ronald Cron5c522922020-11-14 16:35:34 +01002165 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002166}
2167
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002168
2169
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002170/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002171/* Message digests */
2172/****************************************************************/
2173
John Durkop07cc04a2020-11-16 22:08:34 -08002174#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002175 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2176 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002177 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002178static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002179{
2180 switch( alg )
2181 {
John Durkopee4e6602020-11-27 08:48:46 -08002182#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002183 case PSA_ALG_MD2:
2184 return( &mbedtls_md2_info );
2185#endif
John Durkopee4e6602020-11-27 08:48:46 -08002186#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002187 case PSA_ALG_MD4:
2188 return( &mbedtls_md4_info );
2189#endif
John Durkopee4e6602020-11-27 08:48:46 -08002190#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002191 case PSA_ALG_MD5:
2192 return( &mbedtls_md5_info );
2193#endif
John Durkopee4e6602020-11-27 08:48:46 -08002194#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002195 case PSA_ALG_RIPEMD160:
2196 return( &mbedtls_ripemd160_info );
2197#endif
John Durkopee4e6602020-11-27 08:48:46 -08002198#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002199 case PSA_ALG_SHA_1:
2200 return( &mbedtls_sha1_info );
2201#endif
John Durkopee4e6602020-11-27 08:48:46 -08002202#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002203 case PSA_ALG_SHA_224:
2204 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002205#endif
2206#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002207 case PSA_ALG_SHA_256:
2208 return( &mbedtls_sha256_info );
2209#endif
John Durkopee4e6602020-11-27 08:48:46 -08002210#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002211 case PSA_ALG_SHA_384:
2212 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002213#endif
John Durkopee4e6602020-11-27 08:48:46 -08002214#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002215 case PSA_ALG_SHA_512:
2216 return( &mbedtls_sha512_info );
2217#endif
2218 default:
2219 return( NULL );
2220 }
2221}
John Durkop07cc04a2020-11-16 22:08:34 -08002222#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002223 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2224 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2225 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002226
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2228{
2229 switch( operation->alg )
2230 {
Gilles Peskine81736312018-06-26 15:04:31 +02002231 case 0:
2232 /* The object has (apparently) been initialized but it is not
2233 * in use. It's ok to call abort on such an object, and there's
2234 * nothing to do. */
2235 break;
John Durkopee4e6602020-11-27 08:48:46 -08002236#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002237 case PSA_ALG_MD2:
2238 mbedtls_md2_free( &operation->ctx.md2 );
2239 break;
2240#endif
John Durkopee4e6602020-11-27 08:48:46 -08002241#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242 case PSA_ALG_MD4:
2243 mbedtls_md4_free( &operation->ctx.md4 );
2244 break;
2245#endif
John Durkopee4e6602020-11-27 08:48:46 -08002246#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247 case PSA_ALG_MD5:
2248 mbedtls_md5_free( &operation->ctx.md5 );
2249 break;
2250#endif
John Durkopee4e6602020-11-27 08:48:46 -08002251#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002252 case PSA_ALG_RIPEMD160:
2253 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2254 break;
2255#endif
John Durkopee4e6602020-11-27 08:48:46 -08002256#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257 case PSA_ALG_SHA_1:
2258 mbedtls_sha1_free( &operation->ctx.sha1 );
2259 break;
2260#endif
John Durkop6ca23272020-12-03 06:01:32 -08002261#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002263 mbedtls_sha256_free( &operation->ctx.sha256 );
2264 break;
2265#endif
2266#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267 case PSA_ALG_SHA_256:
2268 mbedtls_sha256_free( &operation->ctx.sha256 );
2269 break;
2270#endif
John Durkop6ca23272020-12-03 06:01:32 -08002271#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002273 mbedtls_sha512_free( &operation->ctx.sha512 );
2274 break;
2275#endif
2276#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002277 case PSA_ALG_SHA_512:
2278 mbedtls_sha512_free( &operation->ctx.sha512 );
2279 break;
2280#endif
2281 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002282 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002283 }
2284 operation->alg = 0;
2285 return( PSA_SUCCESS );
2286}
2287
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002288psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289 psa_algorithm_t alg )
2290{
Janos Follath24eed8d2019-11-22 13:21:35 +00002291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002292
2293 /* A context must be freshly initialized before it can be set up. */
2294 if( operation->alg != 0 )
2295 {
2296 return( PSA_ERROR_BAD_STATE );
2297 }
2298
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299 switch( alg )
2300 {
John Durkopee4e6602020-11-27 08:48:46 -08002301#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002302 case PSA_ALG_MD2:
2303 mbedtls_md2_init( &operation->ctx.md2 );
2304 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2305 break;
2306#endif
John Durkopee4e6602020-11-27 08:48:46 -08002307#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308 case PSA_ALG_MD4:
2309 mbedtls_md4_init( &operation->ctx.md4 );
2310 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2311 break;
2312#endif
John Durkopee4e6602020-11-27 08:48:46 -08002313#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314 case PSA_ALG_MD5:
2315 mbedtls_md5_init( &operation->ctx.md5 );
2316 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2317 break;
2318#endif
John Durkopee4e6602020-11-27 08:48:46 -08002319#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320 case PSA_ALG_RIPEMD160:
2321 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2322 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2323 break;
2324#endif
John Durkopee4e6602020-11-27 08:48:46 -08002325#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326 case PSA_ALG_SHA_1:
2327 mbedtls_sha1_init( &operation->ctx.sha1 );
2328 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2329 break;
2330#endif
John Durkopee4e6602020-11-27 08:48:46 -08002331#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332 case PSA_ALG_SHA_224:
2333 mbedtls_sha256_init( &operation->ctx.sha256 );
2334 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2335 break;
John Durkopee4e6602020-11-27 08:48:46 -08002336#endif
2337#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338 case PSA_ALG_SHA_256:
2339 mbedtls_sha256_init( &operation->ctx.sha256 );
2340 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2341 break;
2342#endif
John Durkopee4e6602020-11-27 08:48:46 -08002343#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002344 case PSA_ALG_SHA_384:
2345 mbedtls_sha512_init( &operation->ctx.sha512 );
2346 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2347 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002348#endif
John Durkopee4e6602020-11-27 08:48:46 -08002349#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350 case PSA_ALG_SHA_512:
2351 mbedtls_sha512_init( &operation->ctx.sha512 );
2352 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2353 break;
2354#endif
2355 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002356 return( PSA_ALG_IS_HASH( alg ) ?
2357 PSA_ERROR_NOT_SUPPORTED :
2358 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002359 }
2360 if( ret == 0 )
2361 operation->alg = alg;
2362 else
2363 psa_hash_abort( operation );
2364 return( mbedtls_to_psa_error( ret ) );
2365}
2366
2367psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2368 const uint8_t *input,
2369 size_t input_length )
2370{
Janos Follath24eed8d2019-11-22 13:21:35 +00002371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002372
2373 /* Don't require hash implementations to behave correctly on a
2374 * zero-length input, which may have an invalid pointer. */
2375 if( input_length == 0 )
2376 return( PSA_SUCCESS );
2377
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002378 switch( operation->alg )
2379 {
John Durkopee4e6602020-11-27 08:48:46 -08002380#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381 case PSA_ALG_MD2:
2382 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2383 input, input_length );
2384 break;
2385#endif
John Durkopee4e6602020-11-27 08:48:46 -08002386#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387 case PSA_ALG_MD4:
2388 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2389 input, input_length );
2390 break;
2391#endif
John Durkopee4e6602020-11-27 08:48:46 -08002392#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002393 case PSA_ALG_MD5:
2394 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2395 input, input_length );
2396 break;
2397#endif
John Durkopee4e6602020-11-27 08:48:46 -08002398#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002399 case PSA_ALG_RIPEMD160:
2400 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2401 input, input_length );
2402 break;
2403#endif
John Durkopee4e6602020-11-27 08:48:46 -08002404#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002405 case PSA_ALG_SHA_1:
2406 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2407 input, input_length );
2408 break;
2409#endif
John Durkop6ca23272020-12-03 06:01:32 -08002410#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002412 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2413 input, input_length );
2414 break;
2415#endif
2416#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002417 case PSA_ALG_SHA_256:
2418 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2419 input, input_length );
2420 break;
2421#endif
John Durkop6ca23272020-12-03 06:01:32 -08002422#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002423 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002424 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2425 input, input_length );
2426 break;
2427#endif
2428#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429 case PSA_ALG_SHA_512:
2430 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2431 input, input_length );
2432 break;
2433#endif
2434 default:
John Durkopee4e6602020-11-27 08:48:46 -08002435 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002436 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002437 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002438
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002439 if( ret != 0 )
2440 psa_hash_abort( operation );
2441 return( mbedtls_to_psa_error( ret ) );
2442}
2443
2444psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2445 uint8_t *hash,
2446 size_t hash_size,
2447 size_t *hash_length )
2448{
itayzafrir40835d42018-08-02 13:14:17 +03002449 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002451 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002452
2453 /* Fill the output buffer with something that isn't a valid hash
2454 * (barring an attack on the hash and deliberately-crafted input),
2455 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002456 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002457 /* If hash_size is 0 then hash may be NULL and then the
2458 * call to memset would have undefined behavior. */
2459 if( hash_size != 0 )
2460 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002461
2462 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002463 {
2464 status = PSA_ERROR_BUFFER_TOO_SMALL;
2465 goto exit;
2466 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002467
2468 switch( operation->alg )
2469 {
John Durkopee4e6602020-11-27 08:48:46 -08002470#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002471 case PSA_ALG_MD2:
2472 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2473 break;
2474#endif
John Durkopee4e6602020-11-27 08:48:46 -08002475#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002476 case PSA_ALG_MD4:
2477 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2478 break;
2479#endif
John Durkopee4e6602020-11-27 08:48:46 -08002480#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002481 case PSA_ALG_MD5:
2482 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2483 break;
2484#endif
John Durkopee4e6602020-11-27 08:48:46 -08002485#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002486 case PSA_ALG_RIPEMD160:
2487 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2488 break;
2489#endif
John Durkopee4e6602020-11-27 08:48:46 -08002490#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002491 case PSA_ALG_SHA_1:
2492 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2493 break;
2494#endif
John Durkop6ca23272020-12-03 06:01:32 -08002495#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002496 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002497 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2498 break;
2499#endif
2500#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002501 case PSA_ALG_SHA_256:
2502 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2503 break;
2504#endif
John Durkop6ca23272020-12-03 06:01:32 -08002505#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002506 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002507 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2508 break;
2509#endif
2510#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002511 case PSA_ALG_SHA_512:
2512 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2513 break;
2514#endif
2515 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002516 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002517 }
itayzafrir40835d42018-08-02 13:14:17 +03002518 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002519
itayzafrir40835d42018-08-02 13:14:17 +03002520exit:
2521 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002522 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002523 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002524 return( psa_hash_abort( operation ) );
2525 }
2526 else
2527 {
2528 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002529 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002530 }
2531}
2532
Gilles Peskine2d277862018-06-18 15:41:12 +02002533psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2534 const uint8_t *hash,
2535 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002536{
2537 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2538 size_t actual_hash_length;
2539 psa_status_t status = psa_hash_finish( operation,
2540 actual_hash, sizeof( actual_hash ),
2541 &actual_hash_length );
2542 if( status != PSA_SUCCESS )
2543 return( status );
2544 if( actual_hash_length != hash_length )
2545 return( PSA_ERROR_INVALID_SIGNATURE );
2546 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2547 return( PSA_ERROR_INVALID_SIGNATURE );
2548 return( PSA_SUCCESS );
2549}
2550
Gilles Peskine0a749c82019-11-28 19:33:58 +01002551psa_status_t psa_hash_compute( psa_algorithm_t alg,
2552 const uint8_t *input, size_t input_length,
2553 uint8_t *hash, size_t hash_size,
2554 size_t *hash_length )
2555{
2556 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2558
2559 *hash_length = hash_size;
2560 status = psa_hash_setup( &operation, alg );
2561 if( status != PSA_SUCCESS )
2562 goto exit;
2563 status = psa_hash_update( &operation, input, input_length );
2564 if( status != PSA_SUCCESS )
2565 goto exit;
2566 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2567 if( status != PSA_SUCCESS )
2568 goto exit;
2569
2570exit:
2571 if( status == PSA_SUCCESS )
2572 status = psa_hash_abort( &operation );
2573 else
2574 psa_hash_abort( &operation );
2575 return( status );
2576}
2577
2578psa_status_t psa_hash_compare( psa_algorithm_t alg,
2579 const uint8_t *input, size_t input_length,
2580 const uint8_t *hash, size_t hash_length )
2581{
2582 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2583 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2584
2585 status = psa_hash_setup( &operation, alg );
2586 if( status != PSA_SUCCESS )
2587 goto exit;
2588 status = psa_hash_update( &operation, input, input_length );
2589 if( status != PSA_SUCCESS )
2590 goto exit;
2591 status = psa_hash_verify( &operation, hash, hash_length );
2592 if( status != PSA_SUCCESS )
2593 goto exit;
2594
2595exit:
2596 if( status == PSA_SUCCESS )
2597 status = psa_hash_abort( &operation );
2598 else
2599 psa_hash_abort( &operation );
2600 return( status );
2601}
2602
Gilles Peskineeb35d782019-01-22 17:56:16 +01002603psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2604 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002605{
2606 if( target_operation->alg != 0 )
2607 return( PSA_ERROR_BAD_STATE );
2608
2609 switch( source_operation->alg )
2610 {
2611 case 0:
2612 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002613#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002614 case PSA_ALG_MD2:
2615 mbedtls_md2_clone( &target_operation->ctx.md2,
2616 &source_operation->ctx.md2 );
2617 break;
2618#endif
John Durkopee4e6602020-11-27 08:48:46 -08002619#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002620 case PSA_ALG_MD4:
2621 mbedtls_md4_clone( &target_operation->ctx.md4,
2622 &source_operation->ctx.md4 );
2623 break;
2624#endif
John Durkopee4e6602020-11-27 08:48:46 -08002625#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002626 case PSA_ALG_MD5:
2627 mbedtls_md5_clone( &target_operation->ctx.md5,
2628 &source_operation->ctx.md5 );
2629 break;
2630#endif
John Durkopee4e6602020-11-27 08:48:46 -08002631#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002632 case PSA_ALG_RIPEMD160:
2633 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2634 &source_operation->ctx.ripemd160 );
2635 break;
2636#endif
John Durkopee4e6602020-11-27 08:48:46 -08002637#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002638 case PSA_ALG_SHA_1:
2639 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2640 &source_operation->ctx.sha1 );
2641 break;
2642#endif
John Durkop6ca23272020-12-03 06:01:32 -08002643#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002644 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002645 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2646 &source_operation->ctx.sha256 );
2647 break;
2648#endif
2649#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002650 case PSA_ALG_SHA_256:
2651 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2652 &source_operation->ctx.sha256 );
2653 break;
2654#endif
John Durkop6ca23272020-12-03 06:01:32 -08002655#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002656 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002657 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2658 &source_operation->ctx.sha512 );
2659 break;
2660#endif
2661#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002662 case PSA_ALG_SHA_512:
2663 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2664 &source_operation->ctx.sha512 );
2665 break;
2666#endif
2667 default:
2668 return( PSA_ERROR_NOT_SUPPORTED );
2669 }
2670
2671 target_operation->alg = source_operation->alg;
2672 return( PSA_SUCCESS );
2673}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002674
2675
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002676/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677/* MAC */
2678/****************************************************************/
2679
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002680static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681 psa_algorithm_t alg,
2682 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002683 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002684 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002687 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002689 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002690 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002691
Gilles Peskine8c9def32018-02-08 10:02:12 +01002692 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2693 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002694 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002696 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697 mode = MBEDTLS_MODE_STREAM;
2698 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699 case PSA_ALG_CTR:
2700 mode = MBEDTLS_MODE_CTR;
2701 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002702 case PSA_ALG_CFB:
2703 mode = MBEDTLS_MODE_CFB;
2704 break;
2705 case PSA_ALG_OFB:
2706 mode = MBEDTLS_MODE_OFB;
2707 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002708 case PSA_ALG_ECB_NO_PADDING:
2709 mode = MBEDTLS_MODE_ECB;
2710 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002711 case PSA_ALG_CBC_NO_PADDING:
2712 mode = MBEDTLS_MODE_CBC;
2713 break;
2714 case PSA_ALG_CBC_PKCS7:
2715 mode = MBEDTLS_MODE_CBC;
2716 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002717 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002718 mode = MBEDTLS_MODE_CCM;
2719 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002720 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002721 mode = MBEDTLS_MODE_GCM;
2722 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002723 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2724 mode = MBEDTLS_MODE_CHACHAPOLY;
2725 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002726 default:
2727 return( NULL );
2728 }
2729 }
2730 else if( alg == PSA_ALG_CMAC )
2731 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732 else
2733 return( NULL );
2734
2735 switch( key_type )
2736 {
2737 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002738 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739 break;
2740 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002741 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2742 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002744 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002745 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002746 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002747 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2748 * but two-key Triple-DES is functionally three-key Triple-DES
2749 * with K1=K3, so that's how we present it to mbedtls. */
2750 if( key_bits == 128 )
2751 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752 break;
2753 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002754 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002755 break;
2756 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002757 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002758 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002759 case PSA_KEY_TYPE_CHACHA20:
2760 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2761 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002762 default:
2763 return( NULL );
2764 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002765 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002766 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002767
Jaeden Amero23bbb752018-06-26 14:16:54 +01002768 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2769 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002770}
2771
John Durkop6ba40d12020-11-10 08:50:04 -08002772#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002773static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002774{
Gilles Peskine2d277862018-06-18 15:41:12 +02002775 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002776 {
2777 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002778 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002779 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002780 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002781 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002782 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002783 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002784 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002785 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002786 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002787 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002788 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002789 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002790 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002791 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002792 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002793 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002794 return( 128 );
2795 default:
2796 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002797 }
2798}
John Durkop07cc04a2020-11-16 22:08:34 -08002799#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002800
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002801/* Initialize the MAC operation structure. Once this function has been
2802 * called, psa_mac_abort can run and will do the right thing. */
2803static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2804 psa_algorithm_t alg )
2805{
2806 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2807
2808 operation->alg = alg;
2809 operation->key_set = 0;
2810 operation->iv_set = 0;
2811 operation->iv_required = 0;
2812 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002813 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002814
2815#if defined(MBEDTLS_CMAC_C)
2816 if( alg == PSA_ALG_CMAC )
2817 {
2818 operation->iv_required = 0;
2819 mbedtls_cipher_init( &operation->ctx.cmac );
2820 status = PSA_SUCCESS;
2821 }
2822 else
2823#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002824#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002825 if( PSA_ALG_IS_HMAC( operation->alg ) )
2826 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002827 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2828 operation->ctx.hmac.hash_ctx.alg = 0;
2829 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002830 }
2831 else
John Durkopd0321952020-10-29 21:37:36 -07002832#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002833 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002834 if( ! PSA_ALG_IS_MAC( alg ) )
2835 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002836 }
2837
2838 if( status != PSA_SUCCESS )
2839 memset( operation, 0, sizeof( *operation ) );
2840 return( status );
2841}
2842
John Durkop6ba40d12020-11-10 08:50:04 -08002843#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002844static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2845{
Gilles Peskine3f108122018-12-07 18:14:53 +01002846 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002847 return( psa_hash_abort( &hmac->hash_ctx ) );
2848}
John Durkop6ba40d12020-11-10 08:50:04 -08002849#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002850
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2852{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002853 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002854 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002855 /* The object has (apparently) been initialized but it is not
2856 * in use. It's ok to call abort on such an object, and there's
2857 * nothing to do. */
2858 return( PSA_SUCCESS );
2859 }
2860 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002861#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002862 if( operation->alg == PSA_ALG_CMAC )
2863 {
2864 mbedtls_cipher_free( &operation->ctx.cmac );
2865 }
2866 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002867#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002868#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002869 if( PSA_ALG_IS_HMAC( operation->alg ) )
2870 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002871 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002872 }
2873 else
John Durkopd0321952020-10-29 21:37:36 -07002874#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002875 {
2876 /* Sanity check (shouldn't happen: operation->alg should
2877 * always have been initialized to a valid value). */
2878 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002879 }
Moran Peker41deec42018-04-04 15:43:05 +03002880
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881 operation->alg = 0;
2882 operation->key_set = 0;
2883 operation->iv_set = 0;
2884 operation->iv_required = 0;
2885 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002886 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002887
Gilles Peskine8c9def32018-02-08 10:02:12 +01002888 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002889
2890bad_state:
2891 /* If abort is called on an uninitialized object, we can't trust
2892 * anything. Wipe the object in case it contains confidential data.
2893 * This may result in a memory leak if a pointer gets overwritten,
2894 * but it's too late to do anything about this. */
2895 memset( operation, 0, sizeof( *operation ) );
2896 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002897}
2898
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002899#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002900static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002901 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002902 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002903 const mbedtls_cipher_info_t *cipher_info )
2904{
Janos Follath24eed8d2019-11-22 13:21:35 +00002905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002906
2907 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002908
2909 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2910 if( ret != 0 )
2911 return( ret );
2912
2913 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002914 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002915 key_bits );
2916 return( ret );
2917}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002918#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002919
John Durkop6ba40d12020-11-10 08:50:04 -08002920#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002921static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2922 const uint8_t *key,
2923 size_t key_length,
2924 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002925{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002926 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002927 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002928 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002929 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002930 psa_status_t status;
2931
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002932 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2933 * overflow below. This should never trigger if the hash algorithm
2934 * is implemented correctly. */
2935 /* The size checks against the ipad and opad buffers cannot be written
2936 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2937 * because that triggers -Wlogical-op on GCC 7.3. */
2938 if( block_size > sizeof( ipad ) )
2939 return( PSA_ERROR_NOT_SUPPORTED );
2940 if( block_size > sizeof( hmac->opad ) )
2941 return( PSA_ERROR_NOT_SUPPORTED );
2942 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002943 return( PSA_ERROR_NOT_SUPPORTED );
2944
Gilles Peskined223b522018-06-11 18:12:58 +02002945 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002946 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002947 status = psa_hash_compute( hash_alg, key, key_length,
2948 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002949 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002950 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002951 }
Gilles Peskine96889972018-07-12 17:07:03 +02002952 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2953 * but it is permitted. It is common when HMAC is used in HKDF, for
2954 * example. Don't call `memcpy` in the 0-length because `key` could be
2955 * an invalid pointer which would make the behavior undefined. */
2956 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002957 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002958
Gilles Peskined223b522018-06-11 18:12:58 +02002959 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2960 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002961 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002962 ipad[i] ^= 0x36;
2963 memset( ipad + key_length, 0x36, block_size - key_length );
2964
2965 /* Copy the key material from ipad to opad, flipping the requisite bits,
2966 * and filling the rest of opad with the requisite constant. */
2967 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002968 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2969 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002970
Gilles Peskine01126fa2018-07-12 17:04:55 +02002971 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002972 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002973 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002974
Gilles Peskine01126fa2018-07-12 17:04:55 +02002975 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002976
2977cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002978 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002979
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002980 return( status );
2981}
John Durkop6ba40d12020-11-10 08:50:04 -08002982#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002983
Gilles Peskine89167cb2018-07-08 20:12:23 +02002984static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002985 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002986 psa_algorithm_t alg,
2987 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002988{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002989 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002990 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002991 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002992 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002993 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002994 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002995 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002996 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002998 /* A context must be freshly initialized before it can be set up. */
2999 if( operation->alg != 0 )
3000 {
3001 return( PSA_ERROR_BAD_STATE );
3002 }
3003
Gilles Peskined911eb72018-08-14 15:18:45 +02003004 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003005 if( status != PSA_SUCCESS )
3006 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003007 if( is_sign )
3008 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009
Ronald Cron5c522922020-11-14 16:35:34 +01003010 status = psa_get_and_lock_transparent_key_slot_with_policy(
3011 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003012 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003013 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003014 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003015
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003017 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003018 {
3019 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003020 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003021 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003023 if( cipher_info == NULL )
3024 {
3025 status = PSA_ERROR_NOT_SUPPORTED;
3026 goto exit;
3027 }
3028 operation->mac_size = cipher_info->block_size;
3029 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3030 status = mbedtls_to_psa_error( ret );
3031 }
3032 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003034#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003035 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003036 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003037 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003038 if( hash_alg == 0 )
3039 {
3040 status = PSA_ERROR_NOT_SUPPORTED;
3041 goto exit;
3042 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003043
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003044 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003045 /* Sanity check. This shouldn't fail on a valid configuration. */
3046 if( operation->mac_size == 0 ||
3047 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3048 {
3049 status = PSA_ERROR_NOT_SUPPORTED;
3050 goto exit;
3051 }
3052
Gilles Peskine8e338702019-07-30 20:06:31 +02003053 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003054 {
3055 status = PSA_ERROR_INVALID_ARGUMENT;
3056 goto exit;
3057 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003058
Gilles Peskine01126fa2018-07-12 17:04:55 +02003059 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003060 slot->key.data,
3061 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003062 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003063 }
3064 else
John Durkopd0321952020-10-29 21:37:36 -07003065#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003066 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003067 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003068 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003070
Gilles Peskined911eb72018-08-14 15:18:45 +02003071 if( truncated == 0 )
3072 {
3073 /* The "normal" case: untruncated algorithm. Nothing to do. */
3074 }
3075 else if( truncated < 4 )
3076 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003077 /* A very short MAC is too short for security since it can be
3078 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3079 * so we make this our minimum, even though 32 bits is still
3080 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003081 status = PSA_ERROR_NOT_SUPPORTED;
3082 }
3083 else if( truncated > operation->mac_size )
3084 {
3085 /* It's impossible to "truncate" to a larger length. */
3086 status = PSA_ERROR_INVALID_ARGUMENT;
3087 }
3088 else
3089 operation->mac_size = truncated;
3090
Gilles Peskinefbfac682018-07-08 20:51:54 +02003091exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003092 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003093 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003094 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003095 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003096 else
3097 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003098 operation->key_set = 1;
3099 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003100
Ronald Cron5c522922020-11-14 16:35:34 +01003101 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003102
Ronald Cron5c522922020-11-14 16:35:34 +01003103 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003104}
3105
Gilles Peskine89167cb2018-07-08 20:12:23 +02003106psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003107 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003108 psa_algorithm_t alg )
3109{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003110 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003111}
3112
3113psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003114 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003115 psa_algorithm_t alg )
3116{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003117 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003118}
3119
Gilles Peskine8c9def32018-02-08 10:02:12 +01003120psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3121 const uint8_t *input,
3122 size_t input_length )
3123{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003124 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003126 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003128 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003129 operation->has_input = 1;
3130
Gilles Peskine8c9def32018-02-08 10:02:12 +01003131#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003132 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003133 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003134 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3135 input, input_length );
3136 status = mbedtls_to_psa_error( ret );
3137 }
3138 else
3139#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003140#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003141 if( PSA_ALG_IS_HMAC( operation->alg ) )
3142 {
3143 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3144 input_length );
3145 }
3146 else
John Durkopd0321952020-10-29 21:37:36 -07003147#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003148 {
3149 /* This shouldn't happen if `operation` was initialized by
3150 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003151 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003152 }
3153
Gilles Peskinefbfac682018-07-08 20:51:54 +02003154 if( status != PSA_SUCCESS )
3155 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003156 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003157}
3158
John Durkop6ba40d12020-11-10 08:50:04 -08003159#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003160static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3161 uint8_t *mac,
3162 size_t mac_size )
3163{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003164 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003165 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3166 size_t hash_size = 0;
3167 size_t block_size = psa_get_hash_block_size( hash_alg );
3168 psa_status_t status;
3169
Gilles Peskine01126fa2018-07-12 17:04:55 +02003170 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3171 if( status != PSA_SUCCESS )
3172 return( status );
3173 /* From here on, tmp needs to be wiped. */
3174
3175 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3176 if( status != PSA_SUCCESS )
3177 goto exit;
3178
3179 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3180 if( status != PSA_SUCCESS )
3181 goto exit;
3182
3183 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3184 if( status != PSA_SUCCESS )
3185 goto exit;
3186
Gilles Peskined911eb72018-08-14 15:18:45 +02003187 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3188 if( status != PSA_SUCCESS )
3189 goto exit;
3190
3191 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003192
3193exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003194 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003195 return( status );
3196}
John Durkop6ba40d12020-11-10 08:50:04 -08003197#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003198
mohammad16036df908f2018-04-02 08:34:15 -07003199static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003200 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003201 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003202{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003203 if( ! operation->key_set )
3204 return( PSA_ERROR_BAD_STATE );
3205 if( operation->iv_required && ! operation->iv_set )
3206 return( PSA_ERROR_BAD_STATE );
3207
Gilles Peskine8c9def32018-02-08 10:02:12 +01003208 if( mac_size < operation->mac_size )
3209 return( PSA_ERROR_BUFFER_TOO_SMALL );
3210
Gilles Peskine8c9def32018-02-08 10:02:12 +01003211#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003212 if( operation->alg == PSA_ALG_CMAC )
3213 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003214 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003215 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3216 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003217 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003218 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003219 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003220 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003221 else
3222#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003223#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003224 if( PSA_ALG_IS_HMAC( operation->alg ) )
3225 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003226 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003227 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003228 }
3229 else
John Durkopd0321952020-10-29 21:37:36 -07003230#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003231 {
3232 /* This shouldn't happen if `operation` was initialized by
3233 * a setup function. */
3234 return( PSA_ERROR_BAD_STATE );
3235 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003236}
3237
Gilles Peskineacd4be32018-07-08 19:56:25 +02003238psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3239 uint8_t *mac,
3240 size_t mac_size,
3241 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003242{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003243 psa_status_t status;
3244
Jaeden Amero252ef282019-02-15 14:05:35 +00003245 if( operation->alg == 0 )
3246 {
3247 return( PSA_ERROR_BAD_STATE );
3248 }
3249
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003250 /* Fill the output buffer with something that isn't a valid mac
3251 * (barring an attack on the mac and deliberately-crafted input),
3252 * in case the caller doesn't check the return status properly. */
3253 *mac_length = mac_size;
3254 /* If mac_size is 0 then mac may be NULL and then the
3255 * call to memset would have undefined behavior. */
3256 if( mac_size != 0 )
3257 memset( mac, '!', mac_size );
3258
Gilles Peskine89167cb2018-07-08 20:12:23 +02003259 if( ! operation->is_sign )
3260 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003261 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003262 }
mohammad16036df908f2018-04-02 08:34:15 -07003263
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003264 status = psa_mac_finish_internal( operation, mac, mac_size );
3265
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003266 if( status == PSA_SUCCESS )
3267 {
3268 status = psa_mac_abort( operation );
3269 if( status == PSA_SUCCESS )
3270 *mac_length = operation->mac_size;
3271 else
3272 memset( mac, '!', mac_size );
3273 }
3274 else
3275 psa_mac_abort( operation );
3276 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003277}
3278
Gilles Peskineacd4be32018-07-08 19:56:25 +02003279psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3280 const uint8_t *mac,
3281 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003282{
Gilles Peskine828ed142018-06-18 23:25:51 +02003283 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003284 psa_status_t status;
3285
Jaeden Amero252ef282019-02-15 14:05:35 +00003286 if( operation->alg == 0 )
3287 {
3288 return( PSA_ERROR_BAD_STATE );
3289 }
3290
Gilles Peskine89167cb2018-07-08 20:12:23 +02003291 if( operation->is_sign )
3292 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003293 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003294 }
3295 if( operation->mac_size != mac_length )
3296 {
3297 status = PSA_ERROR_INVALID_SIGNATURE;
3298 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003299 }
mohammad16036df908f2018-04-02 08:34:15 -07003300
3301 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003302 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003303 if( status != PSA_SUCCESS )
3304 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003305
3306 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3307 status = PSA_ERROR_INVALID_SIGNATURE;
3308
3309cleanup:
3310 if( status == PSA_SUCCESS )
3311 status = psa_mac_abort( operation );
3312 else
3313 psa_mac_abort( operation );
3314
Gilles Peskine3f108122018-12-07 18:14:53 +01003315 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003316
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003317 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003318}
3319
3320
Gilles Peskine20035e32018-02-03 22:44:14 +01003321
Gilles Peskine20035e32018-02-03 22:44:14 +01003322/****************************************************************/
3323/* Asymmetric cryptography */
3324/****************************************************************/
3325
John Durkop6ba40d12020-11-10 08:50:04 -08003326#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3327 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003328/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003329 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003330static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3331 size_t hash_length,
3332 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003333{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003334 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003335 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003336 *md_alg = mbedtls_md_get_type( md_info );
3337
3338 /* The Mbed TLS RSA module uses an unsigned int for hash length
3339 * parameters. Validate that it fits so that we don't risk an
3340 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003341#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003342 if( hash_length > UINT_MAX )
3343 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003344#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003345
John Durkop0e005192020-10-31 22:06:54 -07003346#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003347 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3348 * must be correct. */
3349 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3350 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003351 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003352 if( md_info == NULL )
3353 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003354 if( mbedtls_md_get_size( md_info ) != hash_length )
3355 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003356 }
John Durkop0e005192020-10-31 22:06:54 -07003357#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003358
John Durkop0e005192020-10-31 22:06:54 -07003359#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003360 /* PSS requires a hash internally. */
3361 if( PSA_ALG_IS_RSA_PSS( alg ) )
3362 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003363 if( md_info == NULL )
3364 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003365 }
John Durkop0e005192020-10-31 22:06:54 -07003366#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003367
Gilles Peskine61b91d42018-06-08 16:09:36 +02003368 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003369}
3370
Gilles Peskine2b450e32018-06-27 15:42:46 +02003371static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3372 psa_algorithm_t alg,
3373 const uint8_t *hash,
3374 size_t hash_length,
3375 uint8_t *signature,
3376 size_t signature_size,
3377 size_t *signature_length )
3378{
3379 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003381 mbedtls_md_type_t md_alg;
3382
3383 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3384 if( status != PSA_SUCCESS )
3385 return( status );
3386
Gilles Peskine630a18a2018-06-29 17:49:35 +02003387 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003388 return( PSA_ERROR_BUFFER_TOO_SMALL );
3389
John Durkop0e005192020-10-31 22:06:54 -07003390#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003391 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3392 {
3393 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3394 MBEDTLS_MD_NONE );
3395 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003396 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003397 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003398 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003399 md_alg,
3400 (unsigned int) hash_length,
3401 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003402 signature );
3403 }
3404 else
John Durkop0e005192020-10-31 22:06:54 -07003405#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3406#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003407 if( PSA_ALG_IS_RSA_PSS( alg ) )
3408 {
3409 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3410 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003411 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003412 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003413 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003414 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003415 (unsigned int) hash_length,
3416 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003417 signature );
3418 }
3419 else
John Durkop0e005192020-10-31 22:06:54 -07003420#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003421 {
3422 return( PSA_ERROR_INVALID_ARGUMENT );
3423 }
3424
3425 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003426 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003427 return( mbedtls_to_psa_error( ret ) );
3428}
3429
3430static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3431 psa_algorithm_t alg,
3432 const uint8_t *hash,
3433 size_t hash_length,
3434 const uint8_t *signature,
3435 size_t signature_length )
3436{
3437 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003439 mbedtls_md_type_t md_alg;
3440
3441 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3442 if( status != PSA_SUCCESS )
3443 return( status );
3444
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003445 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3446 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003447
John Durkop0e005192020-10-31 22:06:54 -07003448#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003449 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3450 {
3451 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3452 MBEDTLS_MD_NONE );
3453 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003454 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003455 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003456 MBEDTLS_RSA_PUBLIC,
3457 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003458 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003459 hash,
3460 signature );
3461 }
3462 else
John Durkop0e005192020-10-31 22:06:54 -07003463#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3464#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003465 if( PSA_ALG_IS_RSA_PSS( alg ) )
3466 {
3467 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3468 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003469 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003470 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003471 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003472 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003473 (unsigned int) hash_length,
3474 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003475 signature );
3476 }
3477 else
John Durkop0e005192020-10-31 22:06:54 -07003478#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003479 {
3480 return( PSA_ERROR_INVALID_ARGUMENT );
3481 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003482
3483 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3484 * the rest of the signature is invalid". This has little use in
3485 * practice and PSA doesn't report this distinction. */
3486 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3487 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003488 return( mbedtls_to_psa_error( ret ) );
3489}
John Durkop6ba40d12020-11-10 08:50:04 -08003490#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3491 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003492
John Durkop6ba40d12020-11-10 08:50:04 -08003493#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3494 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003495/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3496 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3497 * (even though these functions don't modify it). */
3498static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3499 psa_algorithm_t alg,
3500 const uint8_t *hash,
3501 size_t hash_length,
3502 uint8_t *signature,
3503 size_t signature_size,
3504 size_t *signature_length )
3505{
Janos Follath24eed8d2019-11-22 13:21:35 +00003506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003507 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003508 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003509 mbedtls_mpi_init( &r );
3510 mbedtls_mpi_init( &s );
3511
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003512 if( signature_size < 2 * curve_bytes )
3513 {
3514 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3515 goto cleanup;
3516 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003517
John Durkop0ea39e02020-10-13 19:58:20 -07003518#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003519 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3520 {
3521 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3522 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3523 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003524 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3525 &ecp->d, hash,
3526 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003527 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003528 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003529 }
3530 else
John Durkop0ea39e02020-10-13 19:58:20 -07003531#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003532 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003533 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003534 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3535 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003536 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003537 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003538 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003539
3540 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3541 signature,
3542 curve_bytes ) );
3543 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3544 signature + curve_bytes,
3545 curve_bytes ) );
3546
3547cleanup:
3548 mbedtls_mpi_free( &r );
3549 mbedtls_mpi_free( &s );
3550 if( ret == 0 )
3551 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003552 return( mbedtls_to_psa_error( ret ) );
3553}
3554
3555static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3556 const uint8_t *hash,
3557 size_t hash_length,
3558 const uint8_t *signature,
3559 size_t signature_length )
3560{
Janos Follath24eed8d2019-11-22 13:21:35 +00003561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003562 mbedtls_mpi r, s;
3563 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3564 mbedtls_mpi_init( &r );
3565 mbedtls_mpi_init( &s );
3566
3567 if( signature_length != 2 * curve_bytes )
3568 return( PSA_ERROR_INVALID_SIGNATURE );
3569
3570 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3571 signature,
3572 curve_bytes ) );
3573 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3574 signature + curve_bytes,
3575 curve_bytes ) );
3576
Steven Cooremanacda8342020-07-24 23:09:52 +02003577 /* Check whether the public part is loaded. If not, load it. */
3578 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3579 {
3580 MBEDTLS_MPI_CHK(
3581 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003582 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003583 }
3584
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003585 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3586 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003587
3588cleanup:
3589 mbedtls_mpi_free( &r );
3590 mbedtls_mpi_free( &s );
3591 return( mbedtls_to_psa_error( ret ) );
3592}
John Durkop6ba40d12020-11-10 08:50:04 -08003593#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3594 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003595
Ronald Croncf56a0a2020-08-04 09:51:30 +02003596psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003597 psa_algorithm_t alg,
3598 const uint8_t *hash,
3599 size_t hash_length,
3600 uint8_t *signature,
3601 size_t signature_size,
3602 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003603{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003605 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003606 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003607
3608 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003609 /* Immediately reject a zero-length signature buffer. This guarantees
3610 * that signature must be a valid pointer. (On the other hand, the hash
3611 * buffer can in principle be empty since it doesn't actually have
3612 * to be a hash.) */
3613 if( signature_size == 0 )
3614 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003615
Ronald Cron5c522922020-11-14 16:35:34 +01003616 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3617 PSA_KEY_USAGE_SIGN_HASH,
3618 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003619 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003620 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003621 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003622 {
3623 status = PSA_ERROR_INVALID_ARGUMENT;
3624 goto exit;
3625 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003626
Steven Cooremancd84cb42020-07-16 20:28:36 +02003627 /* Try any of the available accelerators first */
3628 status = psa_driver_wrapper_sign_hash( slot,
3629 alg,
3630 hash,
3631 hash_length,
3632 signature,
3633 signature_size,
3634 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003635 if( status != PSA_ERROR_NOT_SUPPORTED ||
3636 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003637 goto exit;
3638
Steven Cooreman7a250572020-07-17 16:43:05 +02003639 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003640#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3641 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003642 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003643 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003644 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003645
Ronald Cron90857082020-11-25 14:58:33 +01003646 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3647 slot->key.data,
3648 slot->key.bytes,
3649 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003650 if( status != PSA_SUCCESS )
3651 goto exit;
3652
Steven Cooremana2371e52020-07-28 14:30:39 +02003653 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003654 alg,
3655 hash, hash_length,
3656 signature, signature_size,
3657 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003658
Steven Cooremana2371e52020-07-28 14:30:39 +02003659 mbedtls_rsa_free( rsa );
3660 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003661 }
3662 else
John Durkop6ba40d12020-11-10 08:50:04 -08003663#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3664 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003665 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003666 {
John Durkop9814fa22020-11-04 12:28:15 -08003667#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3668 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003669 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003670#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003671 PSA_ALG_IS_ECDSA( alg )
3672#else
3673 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3674#endif
3675 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003676 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003677 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003678 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3679 slot->key.data,
3680 slot->key.bytes,
3681 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003682 if( status != PSA_SUCCESS )
3683 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003684 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003685 alg,
3686 hash, hash_length,
3687 signature, signature_size,
3688 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003689 mbedtls_ecp_keypair_free( ecp );
3690 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003691 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003692 else
John Durkop9814fa22020-11-04 12:28:15 -08003693#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3694 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003695 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003696 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003697 }
itayzafrir5c753392018-05-08 11:18:38 +03003698 }
3699 else
itayzafrir5c753392018-05-08 11:18:38 +03003700 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003701 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003702 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003703
3704exit:
3705 /* Fill the unused part of the output buffer (the whole buffer on error,
3706 * the trailing part on success) with something that isn't a valid mac
3707 * (barring an attack on the mac and deliberately-crafted input),
3708 * in case the caller doesn't check the return status properly. */
3709 if( status == PSA_SUCCESS )
3710 memset( signature + *signature_length, '!',
3711 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003712 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003713 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003714 /* If signature_size is 0 then we have nothing to do. We must not call
3715 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003716
Ronald Cron5c522922020-11-14 16:35:34 +01003717 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003718
Ronald Cron5c522922020-11-14 16:35:34 +01003719 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003720}
3721
Ronald Croncf56a0a2020-08-04 09:51:30 +02003722psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003723 psa_algorithm_t alg,
3724 const uint8_t *hash,
3725 size_t hash_length,
3726 const uint8_t *signature,
3727 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003728{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003729 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003730 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003731 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003732
Ronald Cron5c522922020-11-14 16:35:34 +01003733 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3734 PSA_KEY_USAGE_VERIFY_HASH,
3735 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003736 if( status != PSA_SUCCESS )
3737 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003738
Steven Cooreman55ae2172020-07-17 19:46:15 +02003739 /* Try any of the available accelerators first */
3740 status = psa_driver_wrapper_verify_hash( slot,
3741 alg,
3742 hash,
3743 hash_length,
3744 signature,
3745 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003746 if( status != PSA_ERROR_NOT_SUPPORTED ||
3747 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003748 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003749
John Durkop6ba40d12020-11-10 08:50:04 -08003750#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3751 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003752 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003753 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003754 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003755
Ronald Cron90857082020-11-25 14:58:33 +01003756 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3757 slot->key.data,
3758 slot->key.bytes,
3759 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003760 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003761 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003762
Steven Cooremana2371e52020-07-28 14:30:39 +02003763 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003764 alg,
3765 hash, hash_length,
3766 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003767 mbedtls_rsa_free( rsa );
3768 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003769 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003770 }
3771 else
John Durkop6ba40d12020-11-10 08:50:04 -08003772#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3773 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003774 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003775 {
John Durkop9814fa22020-11-04 12:28:15 -08003776#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3777 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003778 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003779 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003780 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003781 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3782 slot->key.data,
3783 slot->key.bytes,
3784 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003785 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003786 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003787 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003788 hash, hash_length,
3789 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003790 mbedtls_ecp_keypair_free( ecp );
3791 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003792 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003793 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003794 else
John Durkop9814fa22020-11-04 12:28:15 -08003795#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3796 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003797 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003798 status = PSA_ERROR_INVALID_ARGUMENT;
3799 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003800 }
itayzafrir5c753392018-05-08 11:18:38 +03003801 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003802 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003803 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003804 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003805 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003806
3807exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003808 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003809
Ronald Cron5c522922020-11-14 16:35:34 +01003810 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003811}
3812
John Durkop0e005192020-10-31 22:06:54 -07003813#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003814static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3815 mbedtls_rsa_context *rsa )
3816{
3817 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3818 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3819 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3820 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3821}
John Durkop0e005192020-10-31 22:06:54 -07003822#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003823
Ronald Croncf56a0a2020-08-04 09:51:30 +02003824psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003825 psa_algorithm_t alg,
3826 const uint8_t *input,
3827 size_t input_length,
3828 const uint8_t *salt,
3829 size_t salt_length,
3830 uint8_t *output,
3831 size_t output_size,
3832 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003833{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003834 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003835 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003836 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003837
Darryl Green5cc689a2018-07-24 15:34:10 +01003838 (void) input;
3839 (void) input_length;
3840 (void) salt;
3841 (void) output;
3842 (void) output_size;
3843
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003844 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003845
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003846 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3847 return( PSA_ERROR_INVALID_ARGUMENT );
3848
Ronald Cron5c522922020-11-14 16:35:34 +01003849 status = psa_get_and_lock_transparent_key_slot_with_policy(
3850 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003851 if( status != PSA_SUCCESS )
3852 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003853 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3854 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003855 {
3856 status = PSA_ERROR_INVALID_ARGUMENT;
3857 goto exit;
3858 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003859
John Durkop6ba40d12020-11-10 08:50:04 -08003860#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3861 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003862 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003863 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003864 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003865 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3866 slot->key.data,
3867 slot->key.bytes,
3868 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003869 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003870 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003871
3872 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003873 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003874 status = PSA_ERROR_BUFFER_TOO_SMALL;
3875 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003876 }
John Durkop0e005192020-10-31 22:06:54 -07003877#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003878 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003879 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003880 status = mbedtls_to_psa_error(
3881 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003882 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003883 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003884 MBEDTLS_RSA_PUBLIC,
3885 input_length,
3886 input,
3887 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003888 }
3889 else
John Durkop0e005192020-10-31 22:06:54 -07003890#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3891#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003892 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003893 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003894 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003895 status = mbedtls_to_psa_error(
3896 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003897 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003898 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003899 MBEDTLS_RSA_PUBLIC,
3900 salt, salt_length,
3901 input_length,
3902 input,
3903 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003904 }
3905 else
John Durkop0e005192020-10-31 22:06:54 -07003906#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003907 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003908 status = PSA_ERROR_INVALID_ARGUMENT;
3909 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003910 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003911rsa_exit:
3912 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003913 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003914
Steven Cooremana2371e52020-07-28 14:30:39 +02003915 mbedtls_rsa_free( rsa );
3916 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003917 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918 else
John Durkop9814fa22020-11-04 12:28:15 -08003919#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003920 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003921 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003922 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003923 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003924
3925exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003926 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003927
Ronald Cron5c522922020-11-14 16:35:34 +01003928 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003929}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003930
Ronald Croncf56a0a2020-08-04 09:51:30 +02003931psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003932 psa_algorithm_t alg,
3933 const uint8_t *input,
3934 size_t input_length,
3935 const uint8_t *salt,
3936 size_t salt_length,
3937 uint8_t *output,
3938 size_t output_size,
3939 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003941 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003942 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003943 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003944
Darryl Green5cc689a2018-07-24 15:34:10 +01003945 (void) input;
3946 (void) input_length;
3947 (void) salt;
3948 (void) output;
3949 (void) output_size;
3950
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003951 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003952
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003953 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3954 return( PSA_ERROR_INVALID_ARGUMENT );
3955
Ronald Cron5c522922020-11-14 16:35:34 +01003956 status = psa_get_and_lock_transparent_key_slot_with_policy(
3957 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003958 if( status != PSA_SUCCESS )
3959 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003960 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003961 {
3962 status = PSA_ERROR_INVALID_ARGUMENT;
3963 goto exit;
3964 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003965
John Durkop6ba40d12020-11-10 08:50:04 -08003966#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3967 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003968 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003969 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003970 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003971 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3972 slot->key.data,
3973 slot->key.bytes,
3974 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003975 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003976 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977
Steven Cooremana2371e52020-07-28 14:30:39 +02003978 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003979 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003980 status = PSA_ERROR_INVALID_ARGUMENT;
3981 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003982 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983
John Durkop0e005192020-10-31 22:06:54 -07003984#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003985 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003986 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003987 status = mbedtls_to_psa_error(
3988 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003989 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003990 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003991 MBEDTLS_RSA_PRIVATE,
3992 output_length,
3993 input,
3994 output,
3995 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003996 }
3997 else
John Durkop0e005192020-10-31 22:06:54 -07003998#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3999#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004000 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004001 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004002 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004003 status = mbedtls_to_psa_error(
4004 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004005 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004006 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004007 MBEDTLS_RSA_PRIVATE,
4008 salt, salt_length,
4009 output_length,
4010 input,
4011 output,
4012 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004013 }
4014 else
John Durkop0e005192020-10-31 22:06:54 -07004015#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004016 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004017 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004018 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004019
Steven Cooreman4fed4552020-08-03 14:46:03 +02004020rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004021 mbedtls_rsa_free( rsa );
4022 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004023 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024 else
John Durkop6ba40d12020-11-10 08:50:04 -08004025#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4026 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004028 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004030
4031exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004032 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004033
Ronald Cron5c522922020-11-14 16:35:34 +01004034 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004035}
Gilles Peskine20035e32018-02-03 22:44:14 +01004036
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004037
4038
mohammad1603503973b2018-03-12 15:59:30 +02004039/****************************************************************/
4040/* Symmetric cryptography */
4041/****************************************************************/
4042
Gilles Peskinee553c652018-06-04 16:22:46 +02004043static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004044 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004045 psa_algorithm_t alg,
4046 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004047{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004048 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004049 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004050 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004051 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004052 size_t key_bits;
4053 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004054 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4055 PSA_KEY_USAGE_ENCRYPT :
4056 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004057
Steven Cooremand3feccd2020-09-01 15:56:14 +02004058 /* A context must be freshly initialized before it can be set up. */
4059 if( operation->alg != 0 )
4060 return( PSA_ERROR_BAD_STATE );
4061
Steven Cooremana07b9972020-09-10 14:54:14 +02004062 /* The requested algorithm must be one that can be processed by cipher. */
4063 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004064 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004065
Steven Cooremanef8575e2020-09-11 11:44:50 +02004066 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004067 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004068 if( status != PSA_SUCCESS )
4069 goto exit;
4070
4071 /* Initialize the operation struct members, except for alg. The alg member
4072 * is used to indicate to psa_cipher_abort that there are resources to free,
4073 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004074 operation->key_set = 0;
4075 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004076 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004077 operation->iv_size = 0;
4078 operation->block_size = 0;
4079 if( alg == PSA_ALG_ECB_NO_PADDING )
4080 operation->iv_required = 0;
4081 else
4082 operation->iv_required = 1;
4083
Steven Cooremana07b9972020-09-10 14:54:14 +02004084 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004085 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004086 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004087 slot,
4088 alg );
4089 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004090 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004091 slot,
4092 alg );
4093
Steven Cooremanef8575e2020-09-11 11:44:50 +02004094 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004095 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004096 /* Once the driver context is initialised, it needs to be freed using
4097 * psa_cipher_abort. Indicate this through setting alg. */
4098 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004099 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004100
Steven Cooremand3feccd2020-09-01 15:56:14 +02004101 if( status != PSA_ERROR_NOT_SUPPORTED ||
4102 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4103 goto exit;
4104
Steven Cooremana07b9972020-09-10 14:54:14 +02004105 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004106 * available for the given algorithm & key. */
4107 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004108
Steven Cooremana07b9972020-09-10 14:54:14 +02004109 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004110 * psa_cipher_abort. Indicate there is something to be freed through setting
4111 * alg, and indicate the operation is being done using mbedtls crypto through
4112 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004113 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004114 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004115
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004116 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004117 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004118 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004119 {
4120 status = PSA_ERROR_NOT_SUPPORTED;
4121 goto exit;
4122 }
mohammad1603503973b2018-03-12 15:59:30 +02004123
mohammad1603503973b2018-03-12 15:59:30 +02004124 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004125 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004126 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004127
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004128#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004129 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004130 {
4131 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004132 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004133 memcpy( keys, slot->key.data, 16 );
4134 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004135 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4136 keys,
4137 192, cipher_operation );
4138 }
4139 else
4140#endif
4141 {
4142 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004143 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004144 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004145 }
Moran Peker41deec42018-04-04 15:43:05 +03004146 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004147 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004148
mohammad16038481e742018-03-18 13:57:31 +02004149#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004150 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004151 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004152 case PSA_ALG_CBC_NO_PADDING:
4153 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4154 MBEDTLS_PADDING_NONE );
4155 break;
4156 case PSA_ALG_CBC_PKCS7:
4157 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4158 MBEDTLS_PADDING_PKCS7 );
4159 break;
4160 default:
4161 /* The algorithm doesn't involve padding. */
4162 ret = 0;
4163 break;
4164 }
4165 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004166 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004167#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4168
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004169 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004170 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004171 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4172 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004173 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004174 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004175 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004176#if defined(MBEDTLS_CHACHA20_C)
4177 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004178 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004179 operation->iv_size = 12;
4180#endif
mohammad1603503973b2018-03-12 15:59:30 +02004181
Steven Cooreman7df02922020-09-09 15:28:49 +02004182 status = PSA_SUCCESS;
4183
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004184exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004185 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004186 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004187 if( status == PSA_SUCCESS )
4188 {
4189 /* Update operation flags for both driver and software implementations */
4190 operation->key_set = 1;
4191 }
4192 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004193 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004194
Ronald Cron5c522922020-11-14 16:35:34 +01004195 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004196
Ronald Cron5c522922020-11-14 16:35:34 +01004197 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004198}
4199
Gilles Peskinefe119512018-07-08 21:39:34 +02004200psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004201 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004202 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004203{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004204 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004205}
4206
Gilles Peskinefe119512018-07-08 21:39:34 +02004207psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004208 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004209 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004210{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004211 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004212}
4213
Gilles Peskinefe119512018-07-08 21:39:34 +02004214psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004215 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004216 size_t iv_size,
4217 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004218{
itayzafrir534bd7c2018-08-02 13:56:32 +03004219 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004221 if( operation->iv_set || ! operation->iv_required )
4222 {
4223 return( PSA_ERROR_BAD_STATE );
4224 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004225
Steven Cooremanef8575e2020-09-11 11:44:50 +02004226 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004227 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004228 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004229 iv,
4230 iv_size,
4231 iv_length );
4232 goto exit;
4233 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004234
Moran Peker41deec42018-04-04 15:43:05 +03004235 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004236 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004237 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004238 goto exit;
4239 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004240 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004241 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004242 if( ret != 0 )
4243 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004244 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004245 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004246 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004247
mohammad16038481e742018-03-18 13:57:31 +02004248 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004249 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004250
Moran Peker395db872018-05-31 14:07:14 +03004251exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004252 if( status == PSA_SUCCESS )
4253 operation->iv_set = 1;
4254 else
Moran Peker395db872018-05-31 14:07:14 +03004255 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004256 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004257}
4258
Gilles Peskinefe119512018-07-08 21:39:34 +02004259psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004260 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004261 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004262{
itayzafrir534bd7c2018-08-02 13:56:32 +03004263 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004265 if( operation->iv_set || ! operation->iv_required )
4266 {
4267 return( PSA_ERROR_BAD_STATE );
4268 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004269
Steven Cooremanef8575e2020-09-11 11:44:50 +02004270 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004271 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004272 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004273 iv,
4274 iv_length );
4275 goto exit;
4276 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004277
Moran Pekera28258c2018-05-29 16:25:04 +03004278 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004279 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004280 status = PSA_ERROR_INVALID_ARGUMENT;
4281 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004282 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004283 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4284 status = mbedtls_to_psa_error( ret );
4285exit:
4286 if( status == PSA_SUCCESS )
4287 operation->iv_set = 1;
4288 else
mohammad1603503973b2018-03-12 15:59:30 +02004289 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004290 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004291}
4292
Steven Cooreman177deba2020-09-07 17:14:14 +02004293/* Process input for which the algorithm is set to ECB mode. This requires
4294 * manual processing, since the PSA API is defined as being able to process
4295 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4296 * underlying mbedtls_cipher_update only takes full blocks. */
4297static psa_status_t psa_cipher_update_ecb_internal(
4298 mbedtls_cipher_context_t *ctx,
4299 const uint8_t *input,
4300 size_t input_length,
4301 uint8_t *output,
4302 size_t output_size,
4303 size_t *output_length )
4304{
4305 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4306 size_t block_size = ctx->cipher_info->block_size;
4307 size_t internal_output_length = 0;
4308 *output_length = 0;
4309
4310 if( input_length == 0 )
4311 {
4312 status = PSA_SUCCESS;
4313 goto exit;
4314 }
4315
4316 if( ctx->unprocessed_len > 0 )
4317 {
4318 /* Fill up to block size, and run the block if there's a full one. */
4319 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4320
4321 if( input_length < bytes_to_copy )
4322 bytes_to_copy = input_length;
4323
4324 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4325 input, bytes_to_copy );
4326 input_length -= bytes_to_copy;
4327 input += bytes_to_copy;
4328 ctx->unprocessed_len += bytes_to_copy;
4329
4330 if( ctx->unprocessed_len == block_size )
4331 {
4332 status = mbedtls_to_psa_error(
4333 mbedtls_cipher_update( ctx,
4334 ctx->unprocessed_data,
4335 block_size,
4336 output, &internal_output_length ) );
4337
4338 if( status != PSA_SUCCESS )
4339 goto exit;
4340
4341 output += internal_output_length;
4342 output_size -= internal_output_length;
4343 *output_length += internal_output_length;
4344 ctx->unprocessed_len = 0;
4345 }
4346 }
4347
4348 while( input_length >= block_size )
4349 {
4350 /* Run all full blocks we have, one by one */
4351 status = mbedtls_to_psa_error(
4352 mbedtls_cipher_update( ctx, input,
4353 block_size,
4354 output, &internal_output_length ) );
4355
4356 if( status != PSA_SUCCESS )
4357 goto exit;
4358
4359 input_length -= block_size;
4360 input += block_size;
4361
4362 output += internal_output_length;
4363 output_size -= internal_output_length;
4364 *output_length += internal_output_length;
4365 }
4366
4367 if( input_length > 0 )
4368 {
4369 /* Save unprocessed bytes for later processing */
4370 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4371 input, input_length );
4372 ctx->unprocessed_len += input_length;
4373 }
4374
4375 status = PSA_SUCCESS;
4376
4377exit:
4378 return( status );
4379}
4380
Gilles Peskinee553c652018-06-04 16:22:46 +02004381psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4382 const uint8_t *input,
4383 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004384 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004385 size_t output_size,
4386 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004387{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004388 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004389 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004390 if( operation->alg == 0 )
4391 {
4392 return( PSA_ERROR_BAD_STATE );
4393 }
4394 if( operation->iv_required && ! operation->iv_set )
4395 {
4396 return( PSA_ERROR_BAD_STATE );
4397 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004398
Steven Cooremanef8575e2020-09-11 11:44:50 +02004399 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004400 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004401 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004402 input,
4403 input_length,
4404 output,
4405 output_size,
4406 output_length );
4407 goto exit;
4408 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004409
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004410 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004411 {
4412 /* Take the unprocessed partial block left over from previous
4413 * update calls, if any, plus the input to this call. Remove
4414 * the last partial block, if any. You get the data that will be
4415 * output in this call. */
4416 expected_output_size =
4417 ( operation->ctx.cipher.unprocessed_len + input_length )
4418 / operation->block_size * operation->block_size;
4419 }
4420 else
4421 {
4422 expected_output_size = input_length;
4423 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004424
Gilles Peskine89d789c2018-06-04 17:17:16 +02004425 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004426 {
4427 status = PSA_ERROR_BUFFER_TOO_SMALL;
4428 goto exit;
4429 }
mohammad160382759612018-03-12 18:16:40 +02004430
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004431 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4432 {
4433 /* mbedtls_cipher_update has an API inconsistency: it will only
4434 * process a single block at a time in ECB mode. Abstract away that
4435 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004436 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4437 input,
4438 input_length,
4439 output,
4440 output_size,
4441 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004442 }
4443 else
4444 {
4445 status = mbedtls_to_psa_error(
4446 mbedtls_cipher_update( &operation->ctx.cipher, input,
4447 input_length, output, output_length ) );
4448 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004449exit:
4450 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004451 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004452 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004453}
4454
Gilles Peskinee553c652018-06-04 16:22:46 +02004455psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4456 uint8_t *output,
4457 size_t output_size,
4458 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004459{
David Saadab4ecc272019-02-14 13:48:10 +02004460 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004461 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004462 if( operation->alg == 0 )
4463 {
4464 return( PSA_ERROR_BAD_STATE );
4465 }
4466 if( operation->iv_required && ! operation->iv_set )
4467 {
4468 return( PSA_ERROR_BAD_STATE );
4469 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004470
Steven Cooremanef8575e2020-09-11 11:44:50 +02004471 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004472 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004473 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004474 output,
4475 output_size,
4476 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004477 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004478 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004479
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004480 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004481 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004482 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004483 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004484 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004485 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004486 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004487 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004488 }
4489
Steven Cooremanef8575e2020-09-11 11:44:50 +02004490 status = mbedtls_to_psa_error(
4491 mbedtls_cipher_finish( &operation->ctx.cipher,
4492 temp_output_buffer,
4493 output_length ) );
4494 if( status != PSA_SUCCESS )
4495 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004496
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004497 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004498 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004499 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004500 memcpy( output, temp_output_buffer, *output_length );
4501 else
Janos Follath315b51c2018-07-09 16:04:51 +01004502 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004503
Steven Cooremanef8575e2020-09-11 11:44:50 +02004504exit:
4505 if( operation->mbedtls_in_use == 1 )
4506 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004507
Steven Cooremanef8575e2020-09-11 11:44:50 +02004508 if( status == PSA_SUCCESS )
4509 return( psa_cipher_abort( operation ) );
4510 else
4511 {
4512 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004513 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004514
Steven Cooremanef8575e2020-09-11 11:44:50 +02004515 return( status );
4516 }
mohammad1603503973b2018-03-12 15:59:30 +02004517}
4518
Gilles Peskinee553c652018-06-04 16:22:46 +02004519psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4520{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004521 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004522 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004523 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004524 * in use. It's ok to call abort on such an object, and there's
4525 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004526 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004527 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004528
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004529 /* Sanity check (shouldn't happen: operation->alg should
4530 * always have been initialized to a valid value). */
4531 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4532 return( PSA_ERROR_BAD_STATE );
4533
Steven Cooremanef8575e2020-09-11 11:44:50 +02004534 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004535 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004536 else
4537 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004538
Moran Peker41deec42018-04-04 15:43:05 +03004539 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004540 operation->key_set = 0;
4541 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004542 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004543 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004544 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004545 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004546
Moran Peker395db872018-05-31 14:07:14 +03004547 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004548}
4549
Gilles Peskinea0655c32018-04-30 17:06:50 +02004550
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004551
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004552
mohammad16035955c982018-04-26 00:53:03 +03004553/****************************************************************/
4554/* AEAD */
4555/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004556
Gilles Peskineedf9a652018-08-17 18:11:56 +02004557typedef struct
4558{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004559 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004560 const mbedtls_cipher_info_t *cipher_info;
4561 union
4562 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004563 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004564#if defined(MBEDTLS_CCM_C)
4565 mbedtls_ccm_context ccm;
4566#endif /* MBEDTLS_CCM_C */
4567#if defined(MBEDTLS_GCM_C)
4568 mbedtls_gcm_context gcm;
4569#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004570#if defined(MBEDTLS_CHACHAPOLY_C)
4571 mbedtls_chachapoly_context chachapoly;
4572#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004573 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004574 psa_algorithm_t core_alg;
4575 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004576 uint8_t tag_length;
4577} aead_operation_t;
4578
Ronald Cronf95a2b12020-10-22 15:24:49 +02004579#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4580
Gilles Peskine30a9e412019-01-14 18:36:12 +01004581static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004582{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004583 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004584 {
4585#if defined(MBEDTLS_CCM_C)
4586 case PSA_ALG_CCM:
4587 mbedtls_ccm_free( &operation->ctx.ccm );
4588 break;
4589#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004590#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004591 case PSA_ALG_GCM:
4592 mbedtls_gcm_free( &operation->ctx.gcm );
4593 break;
4594#endif /* MBEDTLS_GCM_C */
4595 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004596
Ronald Cron5c522922020-11-14 16:35:34 +01004597 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004598}
4599
4600static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004601 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004602 psa_key_usage_t usage,
4603 psa_algorithm_t alg )
4604{
4605 psa_status_t status;
4606 size_t key_bits;
4607 mbedtls_cipher_id_t cipher_id;
4608
Ronald Cron5c522922020-11-14 16:35:34 +01004609 status = psa_get_and_lock_transparent_key_slot_with_policy(
4610 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004611 if( status != PSA_SUCCESS )
4612 return( status );
4613
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004614 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004615
4616 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004617 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004618 &cipher_id );
4619 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004620 {
4621 status = PSA_ERROR_NOT_SUPPORTED;
4622 goto cleanup;
4623 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004624
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004625 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004626 {
4627#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004628 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4629 operation->core_alg = PSA_ALG_CCM;
4630 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004631 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4632 * The call to mbedtls_ccm_encrypt_and_tag or
4633 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004634 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004635 {
4636 status = PSA_ERROR_INVALID_ARGUMENT;
4637 goto cleanup;
4638 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004639 mbedtls_ccm_init( &operation->ctx.ccm );
4640 status = mbedtls_to_psa_error(
4641 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004642 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004643 (unsigned int) key_bits ) );
4644 if( status != 0 )
4645 goto cleanup;
4646 break;
4647#endif /* MBEDTLS_CCM_C */
4648
4649#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004650 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4651 operation->core_alg = PSA_ALG_GCM;
4652 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004653 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4654 * The call to mbedtls_gcm_crypt_and_tag or
4655 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004656 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004657 {
4658 status = PSA_ERROR_INVALID_ARGUMENT;
4659 goto cleanup;
4660 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004661 mbedtls_gcm_init( &operation->ctx.gcm );
4662 status = mbedtls_to_psa_error(
4663 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004664 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004665 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004666 if( status != 0 )
4667 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004668 break;
4669#endif /* MBEDTLS_GCM_C */
4670
Gilles Peskine26869f22019-05-06 15:25:00 +02004671#if defined(MBEDTLS_CHACHAPOLY_C)
4672 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4673 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4674 operation->full_tag_length = 16;
4675 /* We only support the default tag length. */
4676 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004677 {
4678 status = PSA_ERROR_NOT_SUPPORTED;
4679 goto cleanup;
4680 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004681 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4682 status = mbedtls_to_psa_error(
4683 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004684 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004685 if( status != 0 )
4686 goto cleanup;
4687 break;
4688#endif /* MBEDTLS_CHACHAPOLY_C */
4689
Gilles Peskineedf9a652018-08-17 18:11:56 +02004690 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004691 status = PSA_ERROR_NOT_SUPPORTED;
4692 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004693 }
4694
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004695 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4696 {
4697 status = PSA_ERROR_INVALID_ARGUMENT;
4698 goto cleanup;
4699 }
4700 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004701
Gilles Peskineedf9a652018-08-17 18:11:56 +02004702 return( PSA_SUCCESS );
4703
4704cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004705 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004706 return( status );
4707}
4708
Ronald Croncf56a0a2020-08-04 09:51:30 +02004709psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004710 psa_algorithm_t alg,
4711 const uint8_t *nonce,
4712 size_t nonce_length,
4713 const uint8_t *additional_data,
4714 size_t additional_data_length,
4715 const uint8_t *plaintext,
4716 size_t plaintext_length,
4717 uint8_t *ciphertext,
4718 size_t ciphertext_size,
4719 size_t *ciphertext_length )
4720{
mohammad16035955c982018-04-26 00:53:03 +03004721 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004722 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004723 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004724
mohammad1603f08a5502018-06-03 15:05:47 +03004725 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004726
Ronald Croncf56a0a2020-08-04 09:51:30 +02004727 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004728 if( status != PSA_SUCCESS )
4729 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004730
Gilles Peskineedf9a652018-08-17 18:11:56 +02004731 /* For all currently supported modes, the tag is at the end of the
4732 * ciphertext. */
4733 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4734 {
4735 status = PSA_ERROR_BUFFER_TOO_SMALL;
4736 goto exit;
4737 }
4738 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004739
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004740#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004741 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004742 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004743 status = mbedtls_to_psa_error(
4744 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4745 MBEDTLS_GCM_ENCRYPT,
4746 plaintext_length,
4747 nonce, nonce_length,
4748 additional_data, additional_data_length,
4749 plaintext, ciphertext,
4750 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004751 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004752 else
4753#endif /* MBEDTLS_GCM_C */
4754#if defined(MBEDTLS_CCM_C)
4755 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004756 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004757 status = mbedtls_to_psa_error(
4758 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4759 plaintext_length,
4760 nonce, nonce_length,
4761 additional_data,
4762 additional_data_length,
4763 plaintext, ciphertext,
4764 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004765 }
mohammad16035c8845f2018-05-09 05:40:09 -07004766 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004767#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004768#if defined(MBEDTLS_CHACHAPOLY_C)
4769 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4770 {
4771 if( nonce_length != 12 || operation.tag_length != 16 )
4772 {
4773 status = PSA_ERROR_NOT_SUPPORTED;
4774 goto exit;
4775 }
4776 status = mbedtls_to_psa_error(
4777 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4778 plaintext_length,
4779 nonce,
4780 additional_data,
4781 additional_data_length,
4782 plaintext,
4783 ciphertext,
4784 tag ) );
4785 }
4786 else
4787#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004788 {
mohammad1603554faad2018-06-03 15:07:38 +03004789 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004790 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004791
Gilles Peskineedf9a652018-08-17 18:11:56 +02004792 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4793 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004794
Gilles Peskineedf9a652018-08-17 18:11:56 +02004795exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004796 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004797 if( status == PSA_SUCCESS )
4798 *ciphertext_length = plaintext_length + operation.tag_length;
4799 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004800}
4801
Gilles Peskineee652a32018-06-01 19:23:52 +02004802/* Locate the tag in a ciphertext buffer containing the encrypted data
4803 * followed by the tag. Return the length of the part preceding the tag in
4804 * *plaintext_length. This is the size of the plaintext in modes where
4805 * the encrypted data has the same size as the plaintext, such as
4806 * CCM and GCM. */
4807static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4808 const uint8_t *ciphertext,
4809 size_t ciphertext_length,
4810 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004811 const uint8_t **p_tag )
4812{
4813 size_t payload_length;
4814 if( tag_length > ciphertext_length )
4815 return( PSA_ERROR_INVALID_ARGUMENT );
4816 payload_length = ciphertext_length - tag_length;
4817 if( payload_length > plaintext_size )
4818 return( PSA_ERROR_BUFFER_TOO_SMALL );
4819 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004820 return( PSA_SUCCESS );
4821}
4822
Ronald Croncf56a0a2020-08-04 09:51:30 +02004823psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004824 psa_algorithm_t alg,
4825 const uint8_t *nonce,
4826 size_t nonce_length,
4827 const uint8_t *additional_data,
4828 size_t additional_data_length,
4829 const uint8_t *ciphertext,
4830 size_t ciphertext_length,
4831 uint8_t *plaintext,
4832 size_t plaintext_size,
4833 size_t *plaintext_length )
4834{
mohammad16035955c982018-04-26 00:53:03 +03004835 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004836 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004837 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004838
Gilles Peskineee652a32018-06-01 19:23:52 +02004839 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004840
Ronald Croncf56a0a2020-08-04 09:51:30 +02004841 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004842 if( status != PSA_SUCCESS )
4843 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004844
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004845 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4846 ciphertext, ciphertext_length,
4847 plaintext_size, &tag );
4848 if( status != PSA_SUCCESS )
4849 goto exit;
4850
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004851#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004852 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004853 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004854 status = mbedtls_to_psa_error(
4855 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4856 ciphertext_length - operation.tag_length,
4857 nonce, nonce_length,
4858 additional_data,
4859 additional_data_length,
4860 tag, operation.tag_length,
4861 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004862 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004863 else
4864#endif /* MBEDTLS_GCM_C */
4865#if defined(MBEDTLS_CCM_C)
4866 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004867 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004868 status = mbedtls_to_psa_error(
4869 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4870 ciphertext_length - operation.tag_length,
4871 nonce, nonce_length,
4872 additional_data,
4873 additional_data_length,
4874 ciphertext, plaintext,
4875 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004876 }
mohammad160339574652018-06-01 04:39:53 -07004877 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004878#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004879#if defined(MBEDTLS_CHACHAPOLY_C)
4880 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4881 {
4882 if( nonce_length != 12 || operation.tag_length != 16 )
4883 {
4884 status = PSA_ERROR_NOT_SUPPORTED;
4885 goto exit;
4886 }
4887 status = mbedtls_to_psa_error(
4888 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4889 ciphertext_length - operation.tag_length,
4890 nonce,
4891 additional_data,
4892 additional_data_length,
4893 tag,
4894 ciphertext,
4895 plaintext ) );
4896 }
4897 else
4898#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004899 {
mohammad1603554faad2018-06-03 15:07:38 +03004900 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004901 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004902
Gilles Peskineedf9a652018-08-17 18:11:56 +02004903 if( status != PSA_SUCCESS && plaintext_size != 0 )
4904 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004905
Gilles Peskineedf9a652018-08-17 18:11:56 +02004906exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004907 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004908 if( status == PSA_SUCCESS )
4909 *plaintext_length = ciphertext_length - operation.tag_length;
4910 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004911}
4912
Gilles Peskinea0655c32018-04-30 17:06:50 +02004913
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004914
Gilles Peskine20035e32018-02-03 22:44:14 +01004915/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004916/* Generators */
4917/****************************************************************/
4918
John Durkop07cc04a2020-11-16 22:08:34 -08004919#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4920 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4921 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4922#define AT_LEAST_ONE_BUILTIN_KDF
4923#endif
4924
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004925#define HKDF_STATE_INIT 0 /* no input yet */
4926#define HKDF_STATE_STARTED 1 /* got salt */
4927#define HKDF_STATE_KEYED 2 /* got key */
4928#define HKDF_STATE_OUTPUT 3 /* output started */
4929
Gilles Peskinecbe66502019-05-16 16:59:18 +02004930static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004932{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004933 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4934 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004935 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004937}
4938
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004940{
4941 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004942 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004943 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004944 {
4945 /* The object has (apparently) been initialized but it is not
4946 * in use. It's ok to call abort on such an object, and there's
4947 * nothing to do. */
4948 }
4949 else
John Durkopd0321952020-10-29 21:37:36 -07004950#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004951 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004952 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 mbedtls_free( operation->ctx.hkdf.info );
4954 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004955 }
John Durkop07cc04a2020-11-16 22:08:34 -08004956 else
4957#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4958#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4959 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4960 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004962 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004963 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004964 if( operation->ctx.tls12_prf.seed != NULL )
4965 {
4966 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4967 operation->ctx.tls12_prf.seed_length );
4968 mbedtls_free( operation->ctx.tls12_prf.seed );
4969 }
4970
4971 if( operation->ctx.tls12_prf.label != NULL )
4972 {
4973 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4974 operation->ctx.tls12_prf.label_length );
4975 mbedtls_free( operation->ctx.tls12_prf.label );
4976 }
4977
4978 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4979
4980 /* We leave the fields Ai and output_block to be erased safely by the
4981 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004982 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004983 else
John Durkop07cc04a2020-11-16 22:08:34 -08004984#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4985 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004986 {
4987 status = PSA_ERROR_BAD_STATE;
4988 }
Janos Follath083036a2019-06-11 10:22:26 +01004989 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004990 return( status );
4991}
4992
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004993psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004994 size_t *capacity)
4995{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004996 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004997 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004998 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004999 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005000 }
5001
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005003 return( PSA_SUCCESS );
5004}
5005
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005006psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005007 size_t capacity )
5008{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005010 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005011 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005012 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005013 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005014 return( PSA_SUCCESS );
5015}
5016
John Durkopd0321952020-10-29 21:37:36 -07005017#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005018/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005019 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005020static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005021 psa_algorithm_t hash_alg,
5022 uint8_t *output,
5023 size_t output_length )
5024{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005025 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005026 psa_status_t status;
5027
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005028 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5029 return( PSA_ERROR_BAD_STATE );
5030 hkdf->state = HKDF_STATE_OUTPUT;
5031
Gilles Peskinebef7f142018-07-12 17:22:21 +02005032 while( output_length != 0 )
5033 {
5034 /* Copy what remains of the current block */
5035 uint8_t n = hash_length - hkdf->offset_in_block;
5036 if( n > output_length )
5037 n = (uint8_t) output_length;
5038 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5039 output += n;
5040 output_length -= n;
5041 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005042 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005043 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005044 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005045 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005047 * object was corrupted or if this function is called directly
5048 * inside the library. */
5049 if( hkdf->block_number == 0xff )
5050 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005051
5052 /* We need a new block */
5053 ++hkdf->block_number;
5054 hkdf->offset_in_block = 0;
5055 status = psa_hmac_setup_internal( &hkdf->hmac,
5056 hkdf->prk, hash_length,
5057 hash_alg );
5058 if( status != PSA_SUCCESS )
5059 return( status );
5060 if( hkdf->block_number != 1 )
5061 {
5062 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5063 hkdf->output_block,
5064 hash_length );
5065 if( status != PSA_SUCCESS )
5066 return( status );
5067 }
5068 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5069 hkdf->info,
5070 hkdf->info_length );
5071 if( status != PSA_SUCCESS )
5072 return( status );
5073 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5074 &hkdf->block_number, 1 );
5075 if( status != PSA_SUCCESS )
5076 return( status );
5077 status = psa_hmac_finish_internal( &hkdf->hmac,
5078 hkdf->output_block,
5079 sizeof( hkdf->output_block ) );
5080 if( status != PSA_SUCCESS )
5081 return( status );
5082 }
5083
5084 return( PSA_SUCCESS );
5085}
John Durkop07cc04a2020-11-16 22:08:34 -08005086#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005087
John Durkop07cc04a2020-11-16 22:08:34 -08005088#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5089 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005090static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5091 psa_tls12_prf_key_derivation_t *tls12_prf,
5092 psa_algorithm_t alg )
5093{
5094 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005095 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005096 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5097 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005098
Janos Follath7742fee2019-06-17 12:58:10 +01005099 /* We can't be wanting more output after block 0xff, otherwise
5100 * the capacity check in psa_key_derivation_output_bytes() would have
5101 * prevented this call. It could happen only if the operation
5102 * object was corrupted or if this function is called directly
5103 * inside the library. */
5104 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005105 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005106
5107 /* We need a new block */
5108 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005109 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005110
5111 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5112 *
5113 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5114 *
5115 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5116 * HMAC_hash(secret, A(2) + seed) +
5117 * HMAC_hash(secret, A(3) + seed) + ...
5118 *
5119 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005120 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005121 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005122 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005123 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005124 * is currently extracted as `output_block` and where i is
5125 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005126 */
5127
Janos Follathea29bfb2019-06-19 12:21:20 +01005128 /* Save the hash context before using it, to preserve the hash state with
5129 * only the inner padding in it. We need this, because inner padding depends
5130 * on the key (secret in the RFC's terminology). */
5131 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5132 if( status != PSA_SUCCESS )
5133 goto cleanup;
5134
5135 /* Calculate A(i) where i = tls12_prf->block_number. */
5136 if( tls12_prf->block_number == 1 )
5137 {
5138 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5139 * the variable seed and in this instance means it in the context of the
5140 * P_hash function, where seed = label + seed.) */
5141 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5142 tls12_prf->label, tls12_prf->label_length );
5143 if( status != PSA_SUCCESS )
5144 goto cleanup;
5145 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5146 tls12_prf->seed, tls12_prf->seed_length );
5147 if( status != PSA_SUCCESS )
5148 goto cleanup;
5149 }
5150 else
5151 {
5152 /* A(i) = HMAC_hash(secret, A(i-1)) */
5153 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5154 tls12_prf->Ai, hash_length );
5155 if( status != PSA_SUCCESS )
5156 goto cleanup;
5157 }
5158
5159 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5160 tls12_prf->Ai, hash_length );
5161 if( status != PSA_SUCCESS )
5162 goto cleanup;
5163 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5164 if( status != PSA_SUCCESS )
5165 goto cleanup;
5166
5167 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5168 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5169 tls12_prf->Ai, hash_length );
5170 if( status != PSA_SUCCESS )
5171 goto cleanup;
5172 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5173 tls12_prf->label, tls12_prf->label_length );
5174 if( status != PSA_SUCCESS )
5175 goto cleanup;
5176 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5177 tls12_prf->seed, tls12_prf->seed_length );
5178 if( status != PSA_SUCCESS )
5179 goto cleanup;
5180 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5181 tls12_prf->output_block, hash_length );
5182 if( status != PSA_SUCCESS )
5183 goto cleanup;
5184 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5185 if( status != PSA_SUCCESS )
5186 goto cleanup;
5187
Janos Follath7742fee2019-06-17 12:58:10 +01005188
5189cleanup:
5190
Janos Follathea29bfb2019-06-19 12:21:20 +01005191 cleanup_status = psa_hash_abort( &backup );
5192 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5193 status = cleanup_status;
5194
5195 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005196}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005197
Janos Follath844eb0e2019-06-19 12:10:49 +01005198static psa_status_t psa_key_derivation_tls12_prf_read(
5199 psa_tls12_prf_key_derivation_t *tls12_prf,
5200 psa_algorithm_t alg,
5201 uint8_t *output,
5202 size_t output_length )
5203{
5204 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005205 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005206 psa_status_t status;
5207 uint8_t offset, length;
5208
5209 while( output_length != 0 )
5210 {
5211 /* Check if we have fully processed the current block. */
5212 if( tls12_prf->left_in_block == 0 )
5213 {
5214 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5215 alg );
5216 if( status != PSA_SUCCESS )
5217 return( status );
5218
5219 continue;
5220 }
5221
5222 if( tls12_prf->left_in_block > output_length )
5223 length = (uint8_t) output_length;
5224 else
5225 length = tls12_prf->left_in_block;
5226
5227 offset = hash_length - tls12_prf->left_in_block;
5228 memcpy( output, tls12_prf->output_block + offset, length );
5229 output += length;
5230 output_length -= length;
5231 tls12_prf->left_in_block -= length;
5232 }
5233
5234 return( PSA_SUCCESS );
5235}
John Durkop07cc04a2020-11-16 22:08:34 -08005236#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5237 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005238
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005239psa_status_t psa_key_derivation_output_bytes(
5240 psa_key_derivation_operation_t *operation,
5241 uint8_t *output,
5242 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005243{
5244 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005246
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005248 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005250 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005251 }
5252
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005254 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005256 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005258 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005259 goto exit;
5260 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005262 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005264 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005265 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5266 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005268 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005269 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005270 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005272
John Durkopd0321952020-10-29 21:37:36 -07005273#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005274 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005275 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005276 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005277 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005278 output, output_length );
5279 }
Janos Follathadbec812019-06-14 11:05:39 +01005280 else
John Durkop07cc04a2020-11-16 22:08:34 -08005281#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5282#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5283 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005284 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005285 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005286 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005288 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005289 output_length );
5290 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005291 else
John Durkop07cc04a2020-11-16 22:08:34 -08005292#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5293 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005294 {
5295 return( PSA_ERROR_BAD_STATE );
5296 }
5297
5298exit:
5299 if( status != PSA_SUCCESS )
5300 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005301 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005302 * This allows us to differentiate between exhausted operations and
5303 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5304 * operations. */
5305 psa_algorithm_t alg = operation->alg;
5306 psa_key_derivation_abort( operation );
5307 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005308 memset( output, '!', output_length );
5309 }
5310 return( status );
5311}
5312
Gilles Peskine08542d82018-07-19 17:05:42 +02005313#if defined(MBEDTLS_DES_C)
5314static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5315{
5316 if( data_size >= 8 )
5317 mbedtls_des_key_set_parity( data );
5318 if( data_size >= 16 )
5319 mbedtls_des_key_set_parity( data + 8 );
5320 if( data_size >= 24 )
5321 mbedtls_des_key_set_parity( data + 16 );
5322}
5323#endif /* MBEDTLS_DES_C */
5324
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005325static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005326 psa_key_slot_t *slot,
5327 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005328 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005329{
5330 uint8_t *data = NULL;
5331 size_t bytes = PSA_BITS_TO_BYTES( bits );
5332 psa_status_t status;
5333
Gilles Peskine8e338702019-07-30 20:06:31 +02005334 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005335 return( PSA_ERROR_INVALID_ARGUMENT );
5336 if( bits % 8 != 0 )
5337 return( PSA_ERROR_INVALID_ARGUMENT );
5338 data = mbedtls_calloc( 1, bytes );
5339 if( data == NULL )
5340 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5341
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005343 if( status != PSA_SUCCESS )
5344 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005345#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005346 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005347 psa_des_set_key_parity( data, bytes );
5348#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005349 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005350
5351exit:
5352 mbedtls_free( data );
5353 return( status );
5354}
5355
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005356psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005357 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005358 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005359{
5360 psa_status_t status;
5361 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005362 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005363
Ronald Cron81709fc2020-11-14 12:10:32 +01005364 *key = MBEDTLS_SVC_KEY_ID_INIT;
5365
Gilles Peskine0f84d622019-09-12 19:03:13 +02005366 /* Reject any attempt to create a zero-length key so that we don't
5367 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5368 if( psa_get_key_bits( attributes ) == 0 )
5369 return( PSA_ERROR_INVALID_ARGUMENT );
5370
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005371 if( ! operation->can_output_key )
5372 return( PSA_ERROR_NOT_PERMITTED );
5373
Ronald Cron81709fc2020-11-14 12:10:32 +01005374 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5375 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005376#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5377 if( driver != NULL )
5378 {
5379 /* Deriving a key in a secure element is not implemented yet. */
5380 status = PSA_ERROR_NOT_SUPPORTED;
5381 }
5382#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005383 if( status == PSA_SUCCESS )
5384 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005385 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005386 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005387 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005388 }
5389 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005390 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005391 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005392 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005393
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005394 return( status );
5395}
5396
Gilles Peskineeab56e42018-07-12 17:12:33 +02005397
5398
5399/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005400/* Key derivation */
5401/****************************************************************/
5402
John Durkop07cc04a2020-11-16 22:08:34 -08005403#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005404static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005405 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005406 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005407{
John Durkop07cc04a2020-11-16 22:08:34 -08005408 int is_kdf_alg_supported;
5409
Janos Follath5fe19732019-06-20 15:09:30 +01005410 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5411 * initialisers for this union leave some bytes unspecified.) */
5412 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5413
Gilles Peskine969c5d62019-01-16 15:53:06 +01005414 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005415#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005416 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5417 is_kdf_alg_supported = 1;
5418 else
5419#endif
5420#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5421 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5422 is_kdf_alg_supported = 1;
5423 else
5424#endif
5425#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5426 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5427 is_kdf_alg_supported = 1;
5428 else
5429#endif
5430 is_kdf_alg_supported = 0;
5431
5432 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005433 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005434 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005435 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005436 if( hash_size == 0 )
5437 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005438 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5439 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005440 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005441 {
5442 return( PSA_ERROR_NOT_SUPPORTED );
5443 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005445 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005446 }
John Durkop07cc04a2020-11-16 22:08:34 -08005447
5448 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005449}
John Durkop07cc04a2020-11-16 22:08:34 -08005450#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005451
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005453 psa_algorithm_t alg )
5454{
5455 psa_status_t status;
5456
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005457 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005458 return( PSA_ERROR_BAD_STATE );
5459
Gilles Peskine6843c292019-01-18 16:44:49 +01005460 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5461 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005462#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005463 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005464 {
5465 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005466 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005467 }
5468 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5469 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005471 }
John Durkop07cc04a2020-11-16 22:08:34 -08005472#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005473 else
5474 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005475
5476 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005477 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005478 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005479}
5480
John Durkopd0321952020-10-29 21:37:36 -07005481#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005482static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005483 psa_algorithm_t hash_alg,
5484 psa_key_derivation_step_t step,
5485 const uint8_t *data,
5486 size_t data_length )
5487{
5488 psa_status_t status;
5489 switch( step )
5490 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005491 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005492 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005493 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005494 status = psa_hmac_setup_internal( &hkdf->hmac,
5495 data, data_length,
5496 hash_alg );
5497 if( status != PSA_SUCCESS )
5498 return( status );
5499 hkdf->state = HKDF_STATE_STARTED;
5500 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005501 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005502 /* If no salt was provided, use an empty salt. */
5503 if( hkdf->state == HKDF_STATE_INIT )
5504 {
5505 status = psa_hmac_setup_internal( &hkdf->hmac,
5506 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005507 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005508 if( status != PSA_SUCCESS )
5509 return( status );
5510 hkdf->state = HKDF_STATE_STARTED;
5511 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005512 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005513 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005514 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5515 data, data_length );
5516 if( status != PSA_SUCCESS )
5517 return( status );
5518 status = psa_hmac_finish_internal( &hkdf->hmac,
5519 hkdf->prk,
5520 sizeof( hkdf->prk ) );
5521 if( status != PSA_SUCCESS )
5522 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005523 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005524 hkdf->block_number = 0;
5525 hkdf->state = HKDF_STATE_KEYED;
5526 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005527 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005528 if( hkdf->state == HKDF_STATE_OUTPUT )
5529 return( PSA_ERROR_BAD_STATE );
5530 if( hkdf->info_set )
5531 return( PSA_ERROR_BAD_STATE );
5532 hkdf->info_length = data_length;
5533 if( data_length != 0 )
5534 {
5535 hkdf->info = mbedtls_calloc( 1, data_length );
5536 if( hkdf->info == NULL )
5537 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5538 memcpy( hkdf->info, data, data_length );
5539 }
5540 hkdf->info_set = 1;
5541 return( PSA_SUCCESS );
5542 default:
5543 return( PSA_ERROR_INVALID_ARGUMENT );
5544 }
5545}
John Durkop07cc04a2020-11-16 22:08:34 -08005546#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005547
John Durkop07cc04a2020-11-16 22:08:34 -08005548#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5549 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005550static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5551 const uint8_t *data,
5552 size_t data_length )
5553{
5554 if( prf->state != TLS12_PRF_STATE_INIT )
5555 return( PSA_ERROR_BAD_STATE );
5556
Janos Follathd6dce9f2019-07-04 09:11:38 +01005557 if( data_length != 0 )
5558 {
5559 prf->seed = mbedtls_calloc( 1, data_length );
5560 if( prf->seed == NULL )
5561 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005562
Janos Follathd6dce9f2019-07-04 09:11:38 +01005563 memcpy( prf->seed, data, data_length );
5564 prf->seed_length = data_length;
5565 }
Janos Follathf08e2652019-06-13 09:05:41 +01005566
5567 prf->state = TLS12_PRF_STATE_SEED_SET;
5568
5569 return( PSA_SUCCESS );
5570}
5571
Janos Follath81550542019-06-13 14:26:34 +01005572static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5573 psa_algorithm_t hash_alg,
5574 const uint8_t *data,
5575 size_t data_length )
5576{
5577 psa_status_t status;
5578 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5579 return( PSA_ERROR_BAD_STATE );
5580
5581 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5582 if( status != PSA_SUCCESS )
5583 return( status );
5584
5585 prf->state = TLS12_PRF_STATE_KEY_SET;
5586
5587 return( PSA_SUCCESS );
5588}
5589
Janos Follath63028dd2019-06-13 09:15:47 +01005590static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5591 const uint8_t *data,
5592 size_t data_length )
5593{
5594 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5595 return( PSA_ERROR_BAD_STATE );
5596
Janos Follathd6dce9f2019-07-04 09:11:38 +01005597 if( data_length != 0 )
5598 {
5599 prf->label = mbedtls_calloc( 1, data_length );
5600 if( prf->label == NULL )
5601 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005602
Janos Follathd6dce9f2019-07-04 09:11:38 +01005603 memcpy( prf->label, data, data_length );
5604 prf->label_length = data_length;
5605 }
Janos Follath63028dd2019-06-13 09:15:47 +01005606
5607 prf->state = TLS12_PRF_STATE_LABEL_SET;
5608
5609 return( PSA_SUCCESS );
5610}
5611
Janos Follathb03233e2019-06-11 15:30:30 +01005612static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5613 psa_algorithm_t hash_alg,
5614 psa_key_derivation_step_t step,
5615 const uint8_t *data,
5616 size_t data_length )
5617{
Janos Follathb03233e2019-06-11 15:30:30 +01005618 switch( step )
5619 {
Janos Follathf08e2652019-06-13 09:05:41 +01005620 case PSA_KEY_DERIVATION_INPUT_SEED:
5621 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005622 case PSA_KEY_DERIVATION_INPUT_SECRET:
5623 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005624 case PSA_KEY_DERIVATION_INPUT_LABEL:
5625 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005626 default:
5627 return( PSA_ERROR_INVALID_ARGUMENT );
5628 }
5629}
John Durkop07cc04a2020-11-16 22:08:34 -08005630#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5631 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5632
5633#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5634static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5635 psa_tls12_prf_key_derivation_t *prf,
5636 psa_algorithm_t hash_alg,
5637 const uint8_t *data,
5638 size_t data_length )
5639{
5640 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005641 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005642 uint8_t *cur = pms;
5643
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005644 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005645 return( PSA_ERROR_INVALID_ARGUMENT );
5646
5647 /* Quoting RFC 4279, Section 2:
5648 *
5649 * The premaster secret is formed as follows: if the PSK is N octets
5650 * long, concatenate a uint16 with the value N, N zero octets, a second
5651 * uint16 with the value N, and the PSK itself.
5652 */
5653
5654 *cur++ = ( data_length >> 8 ) & 0xff;
5655 *cur++ = ( data_length >> 0 ) & 0xff;
5656 memset( cur, 0, data_length );
5657 cur += data_length;
5658 *cur++ = pms[0];
5659 *cur++ = pms[1];
5660 memcpy( cur, data, data_length );
5661 cur += data_length;
5662
5663 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5664
5665 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5666 return( status );
5667}
Janos Follath6660f0e2019-06-17 08:44:03 +01005668
5669static psa_status_t psa_tls12_prf_psk_to_ms_input(
5670 psa_tls12_prf_key_derivation_t *prf,
5671 psa_algorithm_t hash_alg,
5672 psa_key_derivation_step_t step,
5673 const uint8_t *data,
5674 size_t data_length )
5675{
5676 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005677 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005678 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5679 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005680 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005681
5682 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5683}
John Durkop07cc04a2020-11-16 22:08:34 -08005684#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005685
Gilles Peskineb8965192019-09-24 16:21:10 +02005686/** Check whether the given key type is acceptable for the given
5687 * input step of a key derivation.
5688 *
5689 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5690 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5691 * Both secret and non-secret inputs can alternatively have the type
5692 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5693 * that the input was passed as a buffer rather than via a key object.
5694 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005695static int psa_key_derivation_check_input_type(
5696 psa_key_derivation_step_t step,
5697 psa_key_type_t key_type )
5698{
5699 switch( step )
5700 {
5701 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005702 if( key_type == PSA_KEY_TYPE_DERIVE )
5703 return( PSA_SUCCESS );
5704 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005705 return( PSA_SUCCESS );
5706 break;
5707 case PSA_KEY_DERIVATION_INPUT_LABEL:
5708 case PSA_KEY_DERIVATION_INPUT_SALT:
5709 case PSA_KEY_DERIVATION_INPUT_INFO:
5710 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005711 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5712 return( PSA_SUCCESS );
5713 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005714 return( PSA_SUCCESS );
5715 break;
5716 }
5717 return( PSA_ERROR_INVALID_ARGUMENT );
5718}
5719
Janos Follathb80a94e2019-06-12 15:54:46 +01005720static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005721 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005722 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005723 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005724 const uint8_t *data,
5725 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005726{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005727 psa_status_t status;
5728 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5729
5730 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005731 if( status != PSA_SUCCESS )
5732 goto exit;
5733
John Durkopd0321952020-10-29 21:37:36 -07005734#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005735 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005736 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005737 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005738 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005739 step, data, data_length );
5740 }
John Durkop07cc04a2020-11-16 22:08:34 -08005741 else
5742#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5743#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5744 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005745 {
Janos Follathb03233e2019-06-11 15:30:30 +01005746 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5747 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5748 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005749 }
John Durkop07cc04a2020-11-16 22:08:34 -08005750 else
5751#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5752#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5753 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005754 {
5755 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5756 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5757 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005758 }
5759 else
John Durkop07cc04a2020-11-16 22:08:34 -08005760#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005761 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005762 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005763 return( PSA_ERROR_BAD_STATE );
5764 }
5765
Gilles Peskine224b0d62019-09-23 18:13:17 +02005766exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005767 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005769 return( status );
5770}
5771
Janos Follath51f4a0f2019-06-14 11:35:55 +01005772psa_status_t psa_key_derivation_input_bytes(
5773 psa_key_derivation_operation_t *operation,
5774 psa_key_derivation_step_t step,
5775 const uint8_t *data,
5776 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005777{
Gilles Peskineb8965192019-09-24 16:21:10 +02005778 return( psa_key_derivation_input_internal( operation, step,
5779 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005780 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005781}
5782
Janos Follath51f4a0f2019-06-14 11:35:55 +01005783psa_status_t psa_key_derivation_input_key(
5784 psa_key_derivation_operation_t *operation,
5785 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005786 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005787{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005788 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005789 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005790 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005791
Ronald Cron5c522922020-11-14 16:35:34 +01005792 status = psa_get_and_lock_transparent_key_slot_with_policy(
5793 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005794 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005795 {
5796 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005797 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005798 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005799
5800 /* Passing a key object as a SECRET input unlocks the permission
5801 * to output to a key object. */
5802 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5803 operation->can_output_key = 1;
5804
Ronald Cronf95a2b12020-10-22 15:24:49 +02005805 status = psa_key_derivation_input_internal( operation,
5806 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005807 slot->key.data,
5808 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005809
Ronald Cron5c522922020-11-14 16:35:34 +01005810 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005811
Ronald Cron5c522922020-11-14 16:35:34 +01005812 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005813}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005814
5815
5816
5817/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005818/* Key agreement */
5819/****************************************************************/
5820
John Durkop6ba40d12020-11-10 08:50:04 -08005821#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005822static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5823 size_t peer_key_length,
5824 const mbedtls_ecp_keypair *our_key,
5825 uint8_t *shared_secret,
5826 size_t shared_secret_size,
5827 size_t *shared_secret_length )
5828{
Steven Cooremand4867872020-08-05 16:31:39 +02005829 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005830 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005831 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005832 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005833 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005834 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005835
Ronald Cron90857082020-11-25 14:58:33 +01005836 status = mbedtls_psa_ecp_load_representation(
5837 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5838 peer_key,
5839 peer_key_length,
5840 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005841 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005842 goto exit;
5843
Jaeden Amero97271b32019-01-10 19:38:51 +00005844 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005845 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005846 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005847 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005848 status = mbedtls_to_psa_error(
5849 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5850 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005851 goto exit;
5852
Jaeden Amero97271b32019-01-10 19:38:51 +00005853 status = mbedtls_to_psa_error(
5854 mbedtls_ecdh_calc_secret( &ecdh,
5855 shared_secret_length,
5856 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005857 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005858 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005859 if( status != PSA_SUCCESS )
5860 goto exit;
5861 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5862 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005863
5864exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005865 if( status != PSA_SUCCESS )
5866 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005867 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005868 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005869 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005870
Jaeden Amero97271b32019-01-10 19:38:51 +00005871 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005872}
John Durkop6ba40d12020-11-10 08:50:04 -08005873#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005874
Gilles Peskine01d718c2018-09-18 12:01:02 +02005875#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5876
Gilles Peskine0216fe12019-04-11 21:23:21 +02005877static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5878 psa_key_slot_t *private_key,
5879 const uint8_t *peer_key,
5880 size_t peer_key_length,
5881 uint8_t *shared_secret,
5882 size_t shared_secret_size,
5883 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005884{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005885 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005886 {
John Durkop6ba40d12020-11-10 08:50:04 -08005887#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005888 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005889 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005890 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005891 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005892 psa_status_t status = mbedtls_psa_ecp_load_representation(
5893 private_key->attr.type,
5894 private_key->key.data,
5895 private_key->key.bytes,
5896 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005897 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005898 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005899 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005900 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005901 shared_secret, shared_secret_size,
5902 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005903 mbedtls_ecp_keypair_free( ecp );
5904 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005905 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005906#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005907 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005908 (void) private_key;
5909 (void) peer_key;
5910 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005911 (void) shared_secret;
5912 (void) shared_secret_size;
5913 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005914 return( PSA_ERROR_NOT_SUPPORTED );
5915 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005916}
5917
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005918/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005919 * to potentially free embedded data structures and wipe confidential data.
5920 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005921static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005922 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005923 psa_key_slot_t *private_key,
5924 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005925 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005926{
5927 psa_status_t status;
5928 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5929 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005930 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005931
5932 /* Step 1: run the secret agreement algorithm to generate the shared
5933 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005934 status = psa_key_agreement_raw_internal( ka_alg,
5935 private_key,
5936 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005937 shared_secret,
5938 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005939 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005940 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005941 goto exit;
5942
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005943 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005944 * the shared secret. A shared secret is permitted wherever a key
5945 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005946 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005947 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005948 shared_secret,
5949 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005950exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005951 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005952 return( status );
5953}
5954
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005955psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005956 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005957 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005958 const uint8_t *peer_key,
5959 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005960{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005961 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005962 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005963 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005964
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005965 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005966 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005967 status = psa_get_and_lock_transparent_key_slot_with_policy(
5968 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005969 if( status != PSA_SUCCESS )
5970 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005971 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005972 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005973 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005974 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005975 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005976 else
5977 {
5978 /* If a private key has been added as SECRET, we allow the derived
5979 * key material to be used as a key in PSA Crypto. */
5980 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5981 operation->can_output_key = 1;
5982 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005983
Ronald Cron5c522922020-11-14 16:35:34 +01005984 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005985
Ronald Cron5c522922020-11-14 16:35:34 +01005986 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005987}
5988
Gilles Peskinebe697d82019-05-16 18:00:41 +02005989psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005990 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005991 const uint8_t *peer_key,
5992 size_t peer_key_length,
5993 uint8_t *output,
5994 size_t output_size,
5995 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005996{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005997 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005998 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005999 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006000
6001 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6002 {
6003 status = PSA_ERROR_INVALID_ARGUMENT;
6004 goto exit;
6005 }
Ronald Cron5c522922020-11-14 16:35:34 +01006006 status = psa_get_and_lock_transparent_key_slot_with_policy(
6007 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006008 if( status != PSA_SUCCESS )
6009 goto exit;
6010
6011 status = psa_key_agreement_raw_internal( alg, slot,
6012 peer_key, peer_key_length,
6013 output, output_size,
6014 output_length );
6015
6016exit:
6017 if( status != PSA_SUCCESS )
6018 {
6019 /* If an error happens and is not handled properly, the output
6020 * may be used as a key to protect sensitive data. Arrange for such
6021 * a key to be random, which is likely to result in decryption or
6022 * verification errors. This is better than filling the buffer with
6023 * some constant data such as zeros, which would result in the data
6024 * being protected with a reproducible, easily knowable key.
6025 */
6026 psa_generate_random( output, output_size );
6027 *output_length = output_size;
6028 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006029
Ronald Cron5c522922020-11-14 16:35:34 +01006030 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006031
Ronald Cron5c522922020-11-14 16:35:34 +01006032 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006033}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006034
6035
Gilles Peskine30524eb2020-11-13 17:02:26 +01006036
Gilles Peskine86a440b2018-11-12 18:39:40 +01006037/****************************************************************/
6038/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006039/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006040
Gilles Peskine30524eb2020-11-13 17:02:26 +01006041/** Initialize the PSA random generator.
6042 */
6043static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6044{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006045#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6046 memset( rng, 0, sizeof( *rng ) );
6047#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6048
Gilles Peskine30524eb2020-11-13 17:02:26 +01006049 /* Set default configuration if
6050 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6051 if( rng->entropy_init == NULL )
6052 rng->entropy_init = mbedtls_entropy_init;
6053 if( rng->entropy_free == NULL )
6054 rng->entropy_free = mbedtls_entropy_free;
6055
6056 rng->entropy_init( &rng->entropy );
6057#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6058 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6059 /* The PSA entropy injection feature depends on using NV seed as an entropy
6060 * source. Add NV seed as an entropy source for PSA entropy injection. */
6061 mbedtls_entropy_add_source( &rng->entropy,
6062 mbedtls_nv_seed_poll, NULL,
6063 MBEDTLS_ENTROPY_BLOCK_SIZE,
6064 MBEDTLS_ENTROPY_SOURCE_STRONG );
6065#endif
6066
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006067 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006068#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006069}
6070
6071/** Deinitialize the PSA random generator.
6072 */
6073static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6074{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006075#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6076 memset( rng, 0, sizeof( *rng ) );
6077#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006078 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006079 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006080#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006081}
6082
6083/** Seed the PSA random generator.
6084 */
6085static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6086{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006087#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6088 /* Do nothing: the external RNG seeds itself. */
6089 (void) rng;
6090 return( PSA_SUCCESS );
6091#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006092 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006093 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6094 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006095 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006096#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006097}
6098
Gilles Peskinee59236f2018-01-27 23:32:46 +01006099psa_status_t psa_generate_random( uint8_t *output,
6100 size_t output_size )
6101{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006102 GUARD_MODULE_INITIALIZED;
6103
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006104#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6105
6106 size_t output_length = 0;
6107 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6108 output, output_size,
6109 &output_length );
6110 if( status != PSA_SUCCESS )
6111 return( status );
6112 /* Breaking up a request into smaller chunks is currently not supported
6113 * for the extrernal RNG interface. */
6114 if( output_length != output_size )
6115 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6116 return( PSA_SUCCESS );
6117
6118#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6119
Gilles Peskine71ddab92021-01-04 21:01:07 +01006120 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006121 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006122 size_t request_size =
6123 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6124 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6125 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006126 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6127 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006128 if( ret != 0 )
6129 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006130 output_size -= request_size;
6131 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006132 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006133 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006134#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006135}
6136
Gilles Peskine8814fc42020-12-14 15:33:44 +01006137/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006138 *
6139 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6140 * `psa_generate_random(...)`. The state parameter is ignored since the
6141 * PSA API doesn't support passing an explicit state.
6142 *
6143 * In the non-external case, psa_generate_random() calls an
6144 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6145 * and semantics as mbedtls_psa_get_random(). As an optimization,
6146 * instead of doing this back-and-forth between the PSA API and the
6147 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6148 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006149 */
6150#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6151int mbedtls_psa_get_random( void *p_rng,
6152 unsigned char *output,
6153 size_t output_size )
6154{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006155 /* This function takes a pointer to the RNG state because that's what
6156 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6157 * its own state internally and doesn't let the caller access that state.
6158 * So we just ignore the state parameter, and in practice we'll pass
6159 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006160 (void) p_rng;
6161 psa_status_t status = psa_generate_random( output, output_size );
6162 if( status == PSA_SUCCESS )
6163 return( 0 );
6164 else
6165 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6166}
6167#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6168
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006169#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6170#include "mbedtls/entropy_poll.h"
6171
Gilles Peskine7228da22019-07-15 11:06:15 +02006172psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006173 size_t seed_size )
6174{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006175 if( global_data.initialized )
6176 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006177
6178 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6179 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6180 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6181 return( PSA_ERROR_INVALID_ARGUMENT );
6182
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006183 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006184}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006185#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006186
John Durkop07cc04a2020-11-16 22:08:34 -08006187#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006188static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6189 size_t domain_parameters_size,
6190 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006191{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006192 size_t i;
6193 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006194
Gilles Peskinee56e8782019-04-26 17:34:02 +02006195 if( domain_parameters_size == 0 )
6196 {
6197 *exponent = 65537;
6198 return( PSA_SUCCESS );
6199 }
6200
6201 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6202 * support values that fit in a 32-bit integer, which is larger than
6203 * int on just about every platform anyway. */
6204 if( domain_parameters_size > sizeof( acc ) )
6205 return( PSA_ERROR_NOT_SUPPORTED );
6206 for( i = 0; i < domain_parameters_size; i++ )
6207 acc = ( acc << 8 ) | domain_parameters[i];
6208 if( acc > INT_MAX )
6209 return( PSA_ERROR_NOT_SUPPORTED );
6210 *exponent = acc;
6211 return( PSA_SUCCESS );
6212}
John Durkop07cc04a2020-11-16 22:08:34 -08006213#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006214
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006215static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006216 psa_key_slot_t *slot, size_t bits,
6217 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006218{
Gilles Peskine8e338702019-07-30 20:06:31 +02006219 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006220
Gilles Peskinee56e8782019-04-26 17:34:02 +02006221 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006222 return( PSA_ERROR_INVALID_ARGUMENT );
6223
Gilles Peskinee59236f2018-01-27 23:32:46 +01006224 if( key_type_is_raw_bytes( type ) )
6225 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006226 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006227
6228 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006229 if( status != PSA_SUCCESS )
6230 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006231
6232 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006233 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6234 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006235 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006236
Ronald Cronea0f8a62020-11-25 17:52:23 +01006237 status = psa_generate_random( slot->key.data,
6238 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006239 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006240 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006241
6242 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006243#if defined(MBEDTLS_DES_C)
6244 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006245 psa_des_set_key_parity( slot->key.data,
6246 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006247#endif /* MBEDTLS_DES_C */
6248 }
6249 else
6250
John Durkop07cc04a2020-11-16 22:08:34 -08006251#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006252 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006253 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006254 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006256 int exponent;
6257 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006258 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6259 return( PSA_ERROR_NOT_SUPPORTED );
6260 /* Accept only byte-aligned keys, for the same reasons as
6261 * in psa_import_rsa_key(). */
6262 if( bits % 8 != 0 )
6263 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006264 status = psa_read_rsa_exponent( domain_parameters,
6265 domain_parameters_size,
6266 &exponent );
6267 if( status != PSA_SUCCESS )
6268 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006269 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6270 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006271 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006272 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006273 (unsigned int) bits,
6274 exponent );
6275 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006276 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006277
6278 /* Make sure to always have an export representation available */
6279 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6280
Steven Cooreman75b74362020-07-28 14:30:13 +02006281 status = psa_allocate_buffer_to_slot( slot, bytes );
6282 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006283 {
6284 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006285 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006286 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006287
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01006288 status = mbedtls_psa_rsa_export_key( type,
6289 &rsa,
6290 slot->key.data,
6291 bytes,
6292 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006293 mbedtls_rsa_free( &rsa );
6294 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006295 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006296 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006297 }
6298 else
John Durkop07cc04a2020-11-16 22:08:34 -08006299#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006300
John Durkop9814fa22020-11-04 12:28:15 -08006301#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006302 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006303 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006304 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006305 mbedtls_ecp_group_id grp_id =
6306 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006307 const mbedtls_ecp_curve_info *curve_info =
6308 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006309 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006311 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006312 return( PSA_ERROR_NOT_SUPPORTED );
6313 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6314 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006315 mbedtls_ecp_keypair_init( &ecp );
6316 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006317 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006318 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006319 if( ret != 0 )
6320 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006321 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006322 return( mbedtls_to_psa_error( ret ) );
6323 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006324
6325
6326 /* Make sure to always have an export representation available */
6327 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006328 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6329 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006330 {
6331 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006332 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006333 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006334
6335 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006336 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006337
6338 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006339 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006340 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006341 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006342 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006343 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006344 }
6345 else
John Durkop9814fa22020-11-04 12:28:15 -08006346#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006347 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006348 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006349 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006350
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006351 return( PSA_SUCCESS );
6352}
Darryl Green0c6575a2018-11-07 16:05:30 +00006353
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006354psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006355 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006356{
6357 psa_status_t status;
6358 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006359 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006360
Ronald Cron81709fc2020-11-14 12:10:32 +01006361 *key = MBEDTLS_SVC_KEY_ID_INIT;
6362
Gilles Peskine0f84d622019-09-12 19:03:13 +02006363 /* Reject any attempt to create a zero-length key so that we don't
6364 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6365 if( psa_get_key_bits( attributes ) == 0 )
6366 return( PSA_ERROR_INVALID_ARGUMENT );
6367
Ronald Cron81709fc2020-11-14 12:10:32 +01006368 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6369 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006370 if( status != PSA_SUCCESS )
6371 goto exit;
6372
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006373 status = psa_driver_wrapper_generate_key( attributes,
6374 slot );
6375 if( status != PSA_ERROR_NOT_SUPPORTED ||
6376 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6377 goto exit;
6378
6379 status = psa_generate_key_internal(
6380 slot, attributes->core.bits,
6381 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006382
6383exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006384 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006385 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006386 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006387 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006388
Darryl Green0c6575a2018-11-07 16:05:30 +00006389 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006390}
6391
6392
Gilles Peskinee59236f2018-01-27 23:32:46 +01006393
6394/****************************************************************/
6395/* Module setup */
6396/****************************************************************/
6397
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006398#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006399psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6400 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6401 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6402{
6403 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6404 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006405 global_data.rng.entropy_init = entropy_init;
6406 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006407 return( PSA_SUCCESS );
6408}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006409#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006410
Gilles Peskinee59236f2018-01-27 23:32:46 +01006411void mbedtls_psa_crypto_free( void )
6412{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006413 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006414 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6415 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006416 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006417 }
6418 /* Wipe all remaining data, including configuration.
6419 * In particular, this sets all state indicator to the value
6420 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006421 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006422#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006423 /* Unregister all secure element drivers, so that we restart from
6424 * a pristine state. */
6425 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006426#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006427}
6428
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006429#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6430/** Recover a transaction that was interrupted by a power failure.
6431 *
6432 * This function is called during initialization, before psa_crypto_init()
6433 * returns. If this function returns a failure status, the initialization
6434 * fails.
6435 */
6436static psa_status_t psa_crypto_recover_transaction(
6437 const psa_crypto_transaction_t *transaction )
6438{
6439 switch( transaction->unknown.type )
6440 {
6441 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6442 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006443 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006444 * is implemented.
6445 * https://github.com/ARMmbed/mbed-crypto/issues/218
6446 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006447 default:
6448 /* We found an unsupported transaction in the storage.
6449 * We don't know what state the storage is in. Give up. */
6450 return( PSA_ERROR_STORAGE_FAILURE );
6451 }
6452}
6453#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6454
Gilles Peskinee59236f2018-01-27 23:32:46 +01006455psa_status_t psa_crypto_init( void )
6456{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006457 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006458
Gilles Peskinec6b69072018-11-20 21:42:52 +01006459 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006460 if( global_data.initialized != 0 )
6461 return( PSA_SUCCESS );
6462
Gilles Peskine30524eb2020-11-13 17:02:26 +01006463 /* Initialize and seed the random generator. */
6464 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006465 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006466 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006467 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006468 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006469 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006470
Gilles Peskine66fb1262018-12-10 16:29:04 +01006471 status = psa_initialize_key_slots( );
6472 if( status != PSA_SUCCESS )
6473 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006474
Gilles Peskined9348f22019-10-01 15:22:29 +02006475#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6476 status = psa_init_all_se_drivers( );
6477 if( status != PSA_SUCCESS )
6478 goto exit;
6479#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6480
Gilles Peskine4b734222019-07-24 15:56:31 +02006481#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006482 status = psa_crypto_load_transaction( );
6483 if( status == PSA_SUCCESS )
6484 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006485 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6486 if( status != PSA_SUCCESS )
6487 goto exit;
6488 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006489 }
6490 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6491 {
6492 /* There's no transaction to complete. It's all good. */
6493 status = PSA_SUCCESS;
6494 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006495#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006496
Gilles Peskinec6b69072018-11-20 21:42:52 +01006497 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006498 global_data.initialized = 1;
6499
6500exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006501 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006502 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006503 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006504}
6505
6506#endif /* MBEDTLS_PSA_CRYPTO_C */