blob: 96d25b3e86cb0a8433d1316d9369af986305d9d0 [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 Durkop9814fa22020-11-04 12:28:15 -0800536#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800537 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200538
Ronald Cron79cc5482020-11-08 14:54:25 +0100539/** Import an ECP key in binary format.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200540 *
Ronald Cron79cc5482020-11-08 14:54:25 +0100541 * \note The signature of this function is that of a PSA driver
542 * import_key entry point. This function behaves as an import_key
543 * entry point as defined in the PSA driver interface specification for
544 * transparent drivers.
545 *
546 * \param[in] attributes The attributes for the key to import.
547 * \param[in] data The buffer containing the key data in import
548 * format.
549 * \param[in] data_length Size of the \p data buffer in bytes.
550 * \param[out] key_buffer The buffer containing the key data in output
551 * format.
552 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
553 * size is greater or equal to \p data_length.
554 * \param[out] key_buffer_length The length of the data written in \p
555 * key_buffer in bytes.
556 * \param[out] bits The key size in number of bits.
557 *
558 * \retval #PSA_SUCCESS The ECP key was imported successfully.
559 * \retval #PSA_ERROR_INVALID_ARGUMENT
560 * The key data is not correctly formatted.
561 * \retval #PSA_ERROR_NOT_SUPPORTED
562 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
563 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Steven Cooreman4fed4552020-08-03 14:46:03 +0200564 */
Ronald Cronb14dbbe2020-11-22 16:36:49 +0100565static psa_status_t mbedtls_psa_ecp_import_key(
Ronald Cron79cc5482020-11-08 14:54:25 +0100566 const psa_key_attributes_t *attributes,
567 const uint8_t *data, size_t data_length,
568 uint8_t *key_buffer, size_t key_buffer_size,
569 size_t *key_buffer_length, size_t *bits )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100570{
Steven Cooremanacda8342020-07-24 23:09:52 +0200571 psa_status_t status;
Steven Cooremana2371e52020-07-28 14:30:39 +0200572 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100573
Steven Cooremanacda8342020-07-24 23:09:52 +0200574 /* Parse input */
Ronald Cron79cc5482020-11-08 14:54:25 +0100575 status = mbedtls_psa_ecp_load_representation( attributes->core.type,
Ronald Cron90857082020-11-25 14:58:33 +0100576 data,
577 data_length,
578 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100579 if( status != PSA_SUCCESS )
580 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100581
Ronald Cron79cc5482020-11-08 14:54:25 +0100582 if( PSA_KEY_TYPE_ECC_GET_FAMILY( attributes->core.type ) ==
583 PSA_ECC_FAMILY_MONTGOMERY )
584 *bits = ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200585 else
Ronald Cron79cc5482020-11-08 14:54:25 +0100586 *bits = ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200587
Steven Cooremanacda8342020-07-24 23:09:52 +0200588 /* Re-export the data to PSA export format. There is currently no support
589 * for other input formats then the export format, so this is a 1-1
590 * copy operation. */
Ronald Cron79cc5482020-11-08 14:54:25 +0100591 status = mbedtls_psa_ecp_export_key( attributes->core.type,
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100592 ecp,
Ronald Cron13f8b092020-11-29 10:48:58 +0100593 key_buffer,
594 key_buffer_size,
595 key_buffer_length );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100596exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200597 /* Always free the PK object (will also free contained ECP context) */
598 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200599 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200600
Ronald Cron13f8b092020-11-29 10:48:58 +0100601 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100602}
John Durkop9814fa22020-11-04 12:28:15 -0800603#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
604 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100605
Gilles Peskineb46bef22019-07-30 21:32:04 +0200606/** Return the size of the key in the given slot, in bits.
607 *
608 * \param[in] slot A key slot.
609 *
610 * \return The key size in bits, read from the metadata in the slot.
611 */
612static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
613{
614 return( slot->attr.bits );
615}
616
Steven Cooreman75b74362020-07-28 14:30:13 +0200617/** Try to allocate a buffer to an empty key slot.
618 *
619 * \param[in,out] slot Key slot to attach buffer to.
620 * \param[in] buffer_length Requested size of the buffer.
621 *
622 * \retval #PSA_SUCCESS
623 * The buffer has been successfully allocated.
624 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
625 * Not enough memory was available for allocation.
626 * \retval #PSA_ERROR_ALREADY_EXISTS
627 * Trying to allocate a buffer to a non-empty key slot.
628 */
629static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
630 size_t buffer_length )
631{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100632 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200633 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200634
Ronald Cronea0f8a62020-11-25 17:52:23 +0100635 slot->key.data = mbedtls_calloc( 1, buffer_length );
636 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200637 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200638
Ronald Cronea0f8a62020-11-25 17:52:23 +0100639 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200640 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200641}
642
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200643psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
644 const uint8_t* data,
645 size_t data_length )
646{
647 psa_status_t status = psa_allocate_buffer_to_slot( slot,
648 data_length );
649 if( status != PSA_SUCCESS )
650 return( status );
651
Ronald Cronea0f8a62020-11-25 17:52:23 +0100652 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200653 return( PSA_SUCCESS );
654}
655
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200656/** Import key data into a slot.
657 *
658 * `slot->type` must have been set previously.
659 * This function assumes that the slot does not contain any key material yet.
660 * On failure, the slot content is unchanged.
661 *
662 * Persistent storage is not affected.
663 *
664 * \param[in,out] slot The key slot to import data into.
665 * Its `type` field must have previously been set to
666 * the desired key type.
667 * It must not contain any key material yet.
668 * \param[in] data Buffer containing the key material to parse and import.
669 * \param data_length Size of \p data in bytes.
670 *
671 * \retval #PSA_SUCCESS
672 * \retval #PSA_ERROR_INVALID_ARGUMENT
673 * \retval #PSA_ERROR_NOT_SUPPORTED
674 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
675 */
676static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
677 const uint8_t *data,
678 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200680 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +0200681 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200683 /* zero-length keys are never supported. */
684 if( data_length == 0 )
685 return( PSA_ERROR_NOT_SUPPORTED );
686
Gilles Peskine8e338702019-07-30 20:06:31 +0200687 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100688 {
Steven Cooreman04524762020-10-13 17:43:44 +0200689 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200690
Steven Cooreman75b74362020-07-28 14:30:13 +0200691 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
692 if( data_length > SIZE_MAX / 8 )
693 return( PSA_ERROR_NOT_SUPPORTED );
694
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200695 /* Enforce a size limit, and in particular ensure that the bit
696 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200697 if( bit_size > PSA_MAX_KEY_BITS )
698 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200699
700 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200701 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200702 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200703
704 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200705 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +0200706 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200707 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200708
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200709 /* Write the actual key size to the slot.
710 * psa_start_key_creation() wrote the size declared by the
711 * caller, which may be 0 (meaning unspecified) or wrong. */
712 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +0100713
714 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200716 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200717 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200718 /* Try validation through accelerators first. */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200719 psa_key_attributes_t attributes = {
720 .core = slot->attr
721 };
Ronald Cron83282872020-11-22 14:02:39 +0100722
723 status = psa_allocate_buffer_to_slot( slot, data_length );
724 if( status != PSA_SUCCESS )
725 return( status );
726
727 bit_size = slot->attr.bits;
728 status = psa_driver_wrapper_import_key( &attributes,
729 data, data_length,
730 slot->key.data,
731 slot->key.bytes,
732 &slot->key.bytes,
733 &bit_size );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200734 if( status == PSA_SUCCESS )
735 {
Ronald Cron83282872020-11-22 14:02:39 +0100736 if( slot->attr.bits == 0 )
737 slot->attr.bits = (psa_key_bits_t) bit_size;
738 else if( bit_size != slot->attr.bits )
739 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200740
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200741 return( PSA_SUCCESS );
742 }
Ronald Cron83282872020-11-22 14:02:39 +0100743 else
744 {
745 if( status != PSA_ERROR_NOT_SUPPORTED )
746 return( status );
747 }
748
749 mbedtls_platform_zeroize( slot->key.data, data_length );
750 mbedtls_free( slot->key.data );
751 slot->key.data = NULL;
752 slot->key.bytes = 0;
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200753
754 /* Key format is not supported by any accelerator, try software fallback
755 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -0800756#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
757 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200758 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
759 {
Ronald Cron13f8b092020-11-29 10:48:58 +0100760 status = psa_allocate_buffer_to_slot( slot, data_length );
761 if( status != PSA_SUCCESS )
762 return( status );
763
Ronald Cronb14dbbe2020-11-22 16:36:49 +0100764 status = mbedtls_psa_ecp_import_key( &attributes,
765 data, data_length,
766 slot->key.data, data_length,
767 &slot->key.bytes,
768 &bit_size );
Ronald Cron79cc5482020-11-08 14:54:25 +0100769 slot->attr.bits = (psa_key_bits_t) bit_size;
Ronald Cron13f8b092020-11-29 10:48:58 +0100770 return( status );
Steven Cooreman40120f62020-10-29 11:42:22 +0100771 }
John Durkop9814fa22020-11-04 12:28:15 -0800772#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
773 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700774#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
775 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +0100776 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200777 {
Ronald Cron8f813ee2020-11-07 18:22:09 +0100778 status = psa_allocate_buffer_to_slot( slot, data_length );
779 if( status != PSA_SUCCESS )
780 return( status );
781
Ronald Cronb6420e32020-11-22 16:15:38 +0100782 status = mbedtls_psa_rsa_import_key( &attributes,
783 data, data_length,
784 slot->key.data, data_length,
785 &slot->key.bytes,
786 &bit_size );
Ronald Cron4f2a7f02020-11-08 14:09:44 +0100787 slot->attr.bits = (psa_key_bits_t) bit_size;
Ronald Cron8f813ee2020-11-07 18:22:09 +0100788 return( status );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200789 }
John Durkop9814fa22020-11-04 12:28:15 -0800790#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
791 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +0100792
793 /* Fell through the fallback as well, so have nothing else to try. */
794 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100795 }
796 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100797 {
Steven Cooreman19fd5742020-07-24 23:31:01 +0200798 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100799 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +0100800 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100801}
802
Gilles Peskinef603c712019-01-19 13:40:11 +0100803/** Calculate the intersection of two algorithm usage policies.
804 *
805 * Return 0 (which allows no operation) on incompatibility.
806 */
807static psa_algorithm_t psa_key_policy_algorithm_intersection(
808 psa_algorithm_t alg1,
809 psa_algorithm_t alg2 )
810{
Gilles Peskine549ea862019-05-22 11:45:59 +0200811 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100812 if( alg1 == alg2 )
813 return( alg1 );
814 /* If the policies are from the same hash-and-sign family, check
815 * if one is a wildcard. If so the other has the specific algorithm. */
816 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
817 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
818 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
819 {
820 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
821 return( alg2 );
822 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
823 return( alg1 );
824 }
825 /* If the policies are incompatible, allow nothing. */
826 return( 0 );
827}
828
Gilles Peskined6f371b2019-05-10 19:33:38 +0200829static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
830 psa_algorithm_t requested_alg )
831{
Gilles Peskine549ea862019-05-22 11:45:59 +0200832 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833 if( requested_alg == policy_alg )
834 return( 1 );
835 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200836 * and requested_alg is the same hash-and-sign family with any hash,
837 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200838 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
839 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
840 {
841 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
842 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
843 }
Steven Cooremance48e852020-10-05 16:02:45 +0200844 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200845 * a key derivation with that key agreement should also be allowed. This
846 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200847 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
848 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
849 {
850 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
851 policy_alg );
852 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200853 /* If it isn't permitted, it's forbidden. */
854 return( 0 );
855}
856
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857/** Test whether a policy permits an algorithm.
858 *
859 * The caller must test usage flags separately.
860 */
861static int psa_key_policy_permits( const psa_key_policy_t *policy,
862 psa_algorithm_t alg )
863{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200864 return( psa_key_algorithm_permits( policy->alg, alg ) ||
865 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100866}
867
Gilles Peskinef603c712019-01-19 13:40:11 +0100868/** Restrict a key policy based on a constraint.
869 *
870 * \param[in,out] policy The policy to restrict.
871 * \param[in] constraint The policy constraint to apply.
872 *
873 * \retval #PSA_SUCCESS
874 * \c *policy contains the intersection of the original value of
875 * \c *policy and \c *constraint.
876 * \retval #PSA_ERROR_INVALID_ARGUMENT
877 * \c *policy and \c *constraint are incompatible.
878 * \c *policy is unchanged.
879 */
880static psa_status_t psa_restrict_key_policy(
881 psa_key_policy_t *policy,
882 const psa_key_policy_t *constraint )
883{
884 psa_algorithm_t intersection_alg =
885 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886 psa_algorithm_t intersection_alg2 =
887 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100888 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
889 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200890 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
891 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100892 policy->usage &= constraint->usage;
893 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200894 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100895 return( PSA_SUCCESS );
896}
897
Ronald Cron5c522922020-11-14 16:35:34 +0100898/** Get the description of a key given its identifier and policy constraints
899 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200900 *
Ronald Cron5c522922020-11-14 16:35:34 +0100901 * The key must have allow all the usage flags set in \p usage. If \p alg is
902 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +0100903 *
Ronald Cron5c522922020-11-14 16:35:34 +0100904 * In case of a persistent key, the function loads the description of the key
905 * into a key slot if not already done.
906 *
907 * On success, the returned key slot is locked. It is the responsibility of
908 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200909 */
Ronald Cron5c522922020-11-14 16:35:34 +0100910static psa_status_t psa_get_and_lock_key_slot_with_policy(
911 mbedtls_svc_key_id_t key,
912 psa_key_slot_t **p_slot,
913 psa_key_usage_t usage,
914 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100915{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200916 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
917 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100918
Ronald Cron5c522922020-11-14 16:35:34 +0100919 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100920 if( status != PSA_SUCCESS )
921 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200922 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100923
924 /* Enforce that usage policy for the key slot contains all the flags
925 * required by the usage parameter. There is one exception: public
926 * keys can always be exported, so we treat public key objects as
927 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200928 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100929 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200930
931 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +0200932 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200933 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100934
935 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200936 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200937 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +0100938
Darryl Green06fd18d2018-07-16 11:21:11 +0100939 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200940
941error:
942 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100943 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200944
945 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100946}
Darryl Green940d72c2018-07-13 13:18:51 +0100947
Ronald Cron5c522922020-11-14 16:35:34 +0100948/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200949 *
950 * A transparent key is a key for which the key material is directly
951 * available, as opposed to a key in a secure element.
952 *
Ronald Cron5c522922020-11-14 16:35:34 +0100953 * This is a temporary function to use instead of
954 * psa_get_and_lock_key_slot_with_policy() until secure element support is
955 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200956 *
Ronald Cron5c522922020-11-14 16:35:34 +0100957 * On success, the returned key slot is locked. It is the responsibility of the
958 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200959 */
960#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +0100961static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
962 mbedtls_svc_key_id_t key,
963 psa_key_slot_t **p_slot,
964 psa_key_usage_t usage,
965 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200966{
Ronald Cron5c522922020-11-14 16:35:34 +0100967 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
968 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200969 if( status != PSA_SUCCESS )
970 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200971
Gilles Peskineadad8132019-07-25 11:31:23 +0200972 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200973 {
Ronald Cron5c522922020-11-14 16:35:34 +0100974 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200975 *p_slot = NULL;
976 return( PSA_ERROR_NOT_SUPPORTED );
977 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200978
Gilles Peskine28f8f302019-07-24 13:30:31 +0200979 return( PSA_SUCCESS );
980}
981#else /* MBEDTLS_PSA_CRYPTO_SE_C */
982/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +0100983#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
984 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200985#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
986
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100987/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100988static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000989{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100990 /* Data pointer will always be either a valid pointer or NULL in an
991 * initialized slot, so we can just free it. */
992 if( slot->key.data != NULL )
993 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
994
995 mbedtls_free( slot->key.data );
996 slot->key.data = NULL;
997 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000998
999 return( PSA_SUCCESS );
1000}
1001
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001002/** Completely wipe a slot in memory, including its policy.
1003 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001004psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001005{
1006 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001007
1008 /*
1009 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001010 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001011 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1012 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001013 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001014 */
Ronald Cron5c522922020-11-14 16:35:34 +01001015 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001016 {
1017#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001018 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001019#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001020 status = PSA_ERROR_CORRUPTION_DETECTED;
1021 }
1022
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001023 /* Multipart operations may still be using the key. This is safe
1024 * because all multipart operation objects are independent from
1025 * the key slot: if they need to access the key after the setup
1026 * phase, they have a copy of the key. Note that this means that
1027 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001028 /* At this point, key material and other type-specific content has
1029 * been wiped. Clear remaining metadata. We can call memset and not
1030 * zeroize because the metadata is not particularly sensitive. */
1031 memset( slot, 0, sizeof( *slot ) );
1032 return( status );
1033}
1034
Ronald Croncf56a0a2020-08-04 09:51:30 +02001035psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001037 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001038 psa_status_t status; /* status of the last operation */
1039 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001040#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1041 psa_se_drv_table_entry_t *driver;
1042#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043
Ronald Croncf56a0a2020-08-04 09:51:30 +02001044 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001045 return( PSA_SUCCESS );
1046
Ronald Cronf2911112020-10-29 17:51:10 +01001047 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001048 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001049 * key, this will load the key description from persistent memory if not
1050 * done yet. We cannot avoid this loading as without it we don't know if
1051 * the key is operated by an SE or not and this information is needed by
1052 * the current implementation.
1053 */
Ronald Cron5c522922020-11-14 16:35:34 +01001054 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001055 if( status != PSA_SUCCESS )
1056 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001057
Ronald Cronf2911112020-10-29 17:51:10 +01001058 /*
1059 * If the key slot containing the key description is under access by the
1060 * library (apart from the present access), the key cannot be destroyed
1061 * yet. For the time being, just return in error. Eventually (to be
1062 * implemented), the key should be destroyed when all accesses have
1063 * stopped.
1064 */
Ronald Cron5c522922020-11-14 16:35:34 +01001065 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001066 {
Ronald Cron5c522922020-11-14 16:35:34 +01001067 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001068 return( PSA_ERROR_GENERIC_ERROR );
1069 }
1070
Gilles Peskine354f7672019-07-12 23:46:38 +02001071#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001072 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001073 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001074 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001075 /* For a key in a secure element, we need to do three things:
1076 * remove the key file in internal storage, destroy the
1077 * key inside the secure element, and update the driver's
1078 * persistent data. Start a transaction that will encompass these
1079 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001080 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001081 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001082 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001083 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001084 status = psa_crypto_save_transaction( );
1085 if( status != PSA_SUCCESS )
1086 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001087 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001088 /* We should still try to destroy the key in the secure
1089 * element and the key metadata in storage. This is especially
1090 * important if the error is that the storage is full.
1091 * But how to do it exactly without risking an inconsistent
1092 * state after a reset?
1093 * https://github.com/ARMmbed/mbed-crypto/issues/215
1094 */
1095 overall_status = status;
1096 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001097 }
1098
Ronald Cronea0f8a62020-11-25 17:52:23 +01001099 status = psa_destroy_se_key( driver,
1100 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001101 if( overall_status == PSA_SUCCESS )
1102 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001103 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001104#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1105
Darryl Greend49a4992018-06-18 17:27:26 +01001106#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001107 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001108 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001109 status = psa_destroy_persistent_key( slot->attr.id );
1110 if( overall_status == PSA_SUCCESS )
1111 overall_status = status;
1112
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001113 /* TODO: other slots may have a copy of the same key. We should
1114 * invalidate them.
1115 * https://github.com/ARMmbed/mbed-crypto/issues/214
1116 */
Darryl Greend49a4992018-06-18 17:27:26 +01001117 }
1118#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001119
Gilles Peskinefc762652019-07-22 19:30:34 +02001120#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1121 if( driver != NULL )
1122 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001123 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001124 if( overall_status == PSA_SUCCESS )
1125 overall_status = status;
1126 status = psa_crypto_stop_transaction( );
1127 if( overall_status == PSA_SUCCESS )
1128 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001129 }
1130#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1131
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001132#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1133exit:
1134#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001135 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001136 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1137 if( overall_status == PSA_SUCCESS )
1138 overall_status = status;
1139 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001140}
1141
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001142void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001143{
Gilles Peskineb699f072019-04-26 16:06:02 +02001144 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001145 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001146}
1147
Gilles Peskineb699f072019-04-26 16:06:02 +02001148psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1149 psa_key_type_t type,
1150 const uint8_t *data,
1151 size_t data_length )
1152{
1153 uint8_t *copy = NULL;
1154
1155 if( data_length != 0 )
1156 {
1157 copy = mbedtls_calloc( 1, data_length );
1158 if( copy == NULL )
1159 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1160 memcpy( copy, data, data_length );
1161 }
1162 /* After this point, this function is guaranteed to succeed, so it
1163 * can start modifying `*attributes`. */
1164
1165 if( attributes->domain_parameters != NULL )
1166 {
1167 mbedtls_free( attributes->domain_parameters );
1168 attributes->domain_parameters = NULL;
1169 attributes->domain_parameters_size = 0;
1170 }
1171
1172 attributes->domain_parameters = copy;
1173 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001174 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001175 return( PSA_SUCCESS );
1176}
1177
1178psa_status_t psa_get_key_domain_parameters(
1179 const psa_key_attributes_t *attributes,
1180 uint8_t *data, size_t data_size, size_t *data_length )
1181{
1182 if( attributes->domain_parameters_size > data_size )
1183 return( PSA_ERROR_BUFFER_TOO_SMALL );
1184 *data_length = attributes->domain_parameters_size;
1185 if( attributes->domain_parameters_size != 0 )
1186 memcpy( data, attributes->domain_parameters,
1187 attributes->domain_parameters_size );
1188 return( PSA_SUCCESS );
1189}
1190
John Durkop07cc04a2020-11-16 22:08:34 -08001191#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001192 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001193static psa_status_t psa_get_rsa_public_exponent(
1194 const mbedtls_rsa_context *rsa,
1195 psa_key_attributes_t *attributes )
1196{
1197 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001199 uint8_t *buffer = NULL;
1200 size_t buflen;
1201 mbedtls_mpi_init( &mpi );
1202
1203 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1204 if( ret != 0 )
1205 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001206 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1207 {
1208 /* It's the default value, which is reported as an empty string,
1209 * so there's nothing to do. */
1210 goto exit;
1211 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001212
1213 buflen = mbedtls_mpi_size( &mpi );
1214 buffer = mbedtls_calloc( 1, buflen );
1215 if( buffer == NULL )
1216 {
1217 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1218 goto exit;
1219 }
1220 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1221 if( ret != 0 )
1222 goto exit;
1223 attributes->domain_parameters = buffer;
1224 attributes->domain_parameters_size = buflen;
1225
1226exit:
1227 mbedtls_mpi_free( &mpi );
1228 if( ret != 0 )
1229 mbedtls_free( buffer );
1230 return( mbedtls_to_psa_error( ret ) );
1231}
John Durkop07cc04a2020-11-16 22:08:34 -08001232#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001233 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001234
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001235/** Retrieve all the publicly-accessible attributes of a key.
1236 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001237psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001238 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001239{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001241 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001242 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001243
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001244 psa_reset_key_attributes( attributes );
1245
Ronald Cron5c522922020-11-14 16:35:34 +01001246 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001247 if( status != PSA_SUCCESS )
1248 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001249
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001250 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001251 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1252 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1253
1254#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1255 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001256 psa_set_key_slot_number( attributes,
1257 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001258#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001259
Gilles Peskine8e338702019-07-30 20:06:31 +02001260 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001261 {
John Durkop07cc04a2020-11-16 22:08:34 -08001262#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001263 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001264 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001265 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001266#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001267 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001268 * is not yet implemented.
1269 * https://github.com/ARMmbed/mbed-crypto/issues/216
1270 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001271 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001272 break;
1273#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001274 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001275 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001276
Ronald Cron90857082020-11-25 14:58:33 +01001277 status = mbedtls_psa_rsa_load_representation(
1278 slot->attr.type,
1279 slot->key.data,
1280 slot->key.bytes,
1281 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001282 if( status != PSA_SUCCESS )
1283 break;
1284
Steven Cooremana2371e52020-07-28 14:30:39 +02001285 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001286 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001287 mbedtls_rsa_free( rsa );
1288 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001289 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001290 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001291#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001292 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001293 default:
1294 /* Nothing else to do. */
1295 break;
1296 }
1297
1298 if( status != PSA_SUCCESS )
1299 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001300
Ronald Cron5c522922020-11-14 16:35:34 +01001301 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001302
Ronald Cron5c522922020-11-14 16:35:34 +01001303 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304}
1305
Gilles Peskinec8000c02019-08-02 20:15:51 +02001306#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1307psa_status_t psa_get_key_slot_number(
1308 const psa_key_attributes_t *attributes,
1309 psa_key_slot_number_t *slot_number )
1310{
1311 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1312 {
1313 *slot_number = attributes->slot_number;
1314 return( PSA_SUCCESS );
1315 }
1316 else
1317 return( PSA_ERROR_INVALID_ARGUMENT );
1318}
1319#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1320
Ronald Crond18b5f82020-11-25 16:46:05 +01001321static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1322 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001323 uint8_t *data,
1324 size_t data_size,
1325 size_t *data_length )
1326{
Ronald Crond18b5f82020-11-25 16:46:05 +01001327 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001328 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001329 memcpy( data, key_buffer, key_buffer_size );
1330 memset( data + key_buffer_size, 0,
1331 data_size - key_buffer_size );
1332 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001333 return( PSA_SUCCESS );
1334}
1335
Ronald Cron7285cda2020-11-26 14:46:37 +01001336psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001337 const psa_key_attributes_t *attributes,
1338 const uint8_t *key_buffer, size_t key_buffer_size,
1339 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340{
Ronald Crond18b5f82020-11-25 16:46:05 +01001341 psa_key_type_t type = attributes->core.type;
1342
Ronald Crond18b5f82020-11-25 16:46:05 +01001343 if( key_type_is_raw_bytes( type ) ||
1344 PSA_KEY_TYPE_IS_RSA( type ) ||
1345 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001346 {
1347 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001348 key_buffer, key_buffer_size,
1349 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001350 }
1351 else
1352 {
1353 /* This shouldn't happen in the reference implementation, but
1354 it is valid for a special-purpose implementation to omit
1355 support for exporting certain key types. */
1356 return( PSA_ERROR_NOT_SUPPORTED );
1357 }
1358}
1359
1360psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1361 uint8_t *data,
1362 size_t data_size,
1363 size_t *data_length )
1364{
1365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1366 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1367 psa_key_slot_t *slot;
1368
1369 /* Set the key to empty now, so that even when there are errors, we always
1370 * set data_length to a value between 0 and data_size. On error, setting
1371 * the key to empty is a good choice because an empty key representation is
1372 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001373 *data_length = 0;
1374
Ronald Cron9486f9d2020-11-26 13:36:23 +01001375 /* Export requires the EXPORT flag. There is an exception for public keys,
1376 * which don't require any flag, but
1377 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1378 */
1379 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1380 PSA_KEY_USAGE_EXPORT, 0 );
1381 if( status != PSA_SUCCESS )
1382 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001383
Gilles Peskinef9168942019-09-12 19:20:29 +02001384 /* Reject a zero-length output buffer now, since this can never be a
1385 * valid key representation. This way we know that data must be a valid
1386 * pointer and we can do things like memset(data, ..., data_size). */
1387 if( data_size == 0 )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 {
1389 status = PSA_ERROR_BUFFER_TOO_SMALL;
1390 goto exit;
1391 }
1392
Ronald Crond18b5f82020-11-25 16:46:05 +01001393 psa_key_attributes_t attributes = {
1394 .core = slot->attr
1395 };
Ronald Cron67227982020-11-26 15:16:05 +01001396 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001397 slot->key.data, slot->key.bytes,
1398 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001399
1400exit:
1401 unlock_status = psa_unlock_key_slot( slot );
1402
1403 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1404}
1405
Ronald Cron7285cda2020-11-26 14:46:37 +01001406psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001407 const psa_key_attributes_t *attributes,
1408 const uint8_t *key_buffer,
1409 size_t key_buffer_size,
1410 uint8_t *data,
1411 size_t data_size,
1412 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001413{
Ronald Crond18b5f82020-11-25 16:46:05 +01001414 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001415
Ronald Crond18b5f82020-11-25 16:46:05 +01001416 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001417 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001418 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001419 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001420 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001421 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001422 key_buffer, key_buffer_size,
1423 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001424 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001425
Ronald Crond18b5f82020-11-25 16:46:05 +01001426 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001427 {
John Durkop0e005192020-10-31 22:06:54 -07001428#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1429 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001430 return( mbedtls_psa_rsa_export_public_key( attributes,
1431 key_buffer,
1432 key_buffer_size,
1433 data,
1434 data_size,
1435 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001436#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001437 /* We don't know how to convert a private RSA key to public. */
1438 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001439#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1440 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001441 }
1442 else
Moran Pekera998bc62018-04-16 18:16:20 +03001443 {
John Durkop6ba40d12020-11-10 08:50:04 -08001444#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1445 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001446 return( mbedtls_psa_ecp_export_public_key( attributes,
1447 key_buffer,
1448 key_buffer_size,
1449 data,
1450 data_size,
1451 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001452#else
1453 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001454 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001455#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1456 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001457 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001458 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001459 else
1460 {
1461 /* This shouldn't happen in the reference implementation, but
1462 it is valid for a special-purpose implementation to omit
1463 support for exporting certain key types. */
1464 return( PSA_ERROR_NOT_SUPPORTED );
1465 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001466}
Ronald Crond18b5f82020-11-25 16:46:05 +01001467
Ronald Croncf56a0a2020-08-04 09:51:30 +02001468psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001469 uint8_t *data,
1470 size_t data_size,
1471 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001472{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001474 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001475 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001476
1477 /* Set the key to empty now, so that even when there are errors, we always
1478 * set data_length to a value between 0 and data_size. On error, setting
1479 * the key to empty is a good choice because an empty key representation is
1480 * unlikely to be accepted anywhere. */
1481 *data_length = 0;
1482
1483 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001484 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001485 if( status != PSA_SUCCESS )
1486 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001487
Ronald Cron9486f9d2020-11-26 13:36:23 +01001488 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1489 {
1490 status = PSA_ERROR_INVALID_ARGUMENT;
1491 goto exit;
1492 }
1493
1494 /* Reject a zero-length output buffer now, since this can never be a
1495 * valid key representation. This way we know that data must be a valid
1496 * pointer and we can do things like memset(data, ..., data_size). */
1497 if( data_size == 0 )
1498 {
1499 status = PSA_ERROR_BUFFER_TOO_SMALL;
1500 goto exit;
1501 }
1502
Ronald Crond18b5f82020-11-25 16:46:05 +01001503 psa_key_attributes_t attributes = {
1504 .core = slot->attr
1505 };
Ronald Cron67227982020-11-26 15:16:05 +01001506 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001507 &attributes, slot->key.data, slot->key.bytes,
1508 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001509
1510exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001511 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001512
Ronald Cron5c522922020-11-14 16:35:34 +01001513 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001514}
1515
Gilles Peskine91e8c332019-08-02 19:19:39 +02001516#if defined(static_assert)
1517static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1518 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001519static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001520 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001521static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001522 "One or more key attribute flag is listed as both internal-only and external-only" );
1523#endif
1524
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001525/** Validate that a key policy is internally well-formed.
1526 *
1527 * This function only rejects invalid policies. It does not validate the
1528 * consistency of the policy with respect to other attributes of the key
1529 * such as the key type.
1530 */
1531static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001532{
1533 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001534 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001535 PSA_KEY_USAGE_ENCRYPT |
1536 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001537 PSA_KEY_USAGE_SIGN_HASH |
1538 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001539 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1540 return( PSA_ERROR_INVALID_ARGUMENT );
1541
Gilles Peskine4747d192019-04-17 15:05:45 +02001542 return( PSA_SUCCESS );
1543}
1544
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001545/** Validate the internal consistency of key attributes.
1546 *
1547 * This function only rejects invalid attribute values. If does not
1548 * validate the consistency of the attributes with any key data that may
1549 * be involved in the creation of the key.
1550 *
1551 * Call this function early in the key creation process.
1552 *
1553 * \param[in] attributes Key attributes for the new key.
1554 * \param[out] p_drv On any return, the driver for the key, if any.
1555 * NULL for a transparent key.
1556 *
1557 */
1558static psa_status_t psa_validate_key_attributes(
1559 const psa_key_attributes_t *attributes,
1560 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001561{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001562 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001563 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001564 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001565
Ronald Cron54b90082020-10-29 15:26:43 +01001566 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001567 if( status != PSA_SUCCESS )
1568 return( status );
1569
Ronald Crond2ed4812020-07-17 16:11:30 +02001570 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001571 if( status != PSA_SUCCESS )
1572 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001573
Ronald Cron65f38a32020-10-23 17:11:13 +02001574 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1575 {
1576 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1577 return( PSA_ERROR_INVALID_ARGUMENT );
1578 }
1579 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001580 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001581 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001582 if( status != PSA_SUCCESS )
1583 return( status );
1584 }
1585
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001586 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001587 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001588 return( status );
1589
1590 /* Refuse to create overly large keys.
1591 * Note that this doesn't trigger on import if the attributes don't
1592 * explicitly specify a size (so psa_get_key_bits returns 0), so
1593 * psa_import_key() needs its own checks. */
1594 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1595 return( PSA_ERROR_NOT_SUPPORTED );
1596
Gilles Peskine91e8c332019-08-02 19:19:39 +02001597 /* Reject invalid flags. These should not be reachable through the API. */
1598 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1599 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1600 return( PSA_ERROR_INVALID_ARGUMENT );
1601
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001602 return( PSA_SUCCESS );
1603}
1604
Gilles Peskine4747d192019-04-17 15:05:45 +02001605/** Prepare a key slot to receive key material.
1606 *
1607 * This function allocates a key slot and sets its metadata.
1608 *
1609 * If this function fails, call psa_fail_key_creation().
1610 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001611 * This function is intended to be used as follows:
1612 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001613 * it with the specified attributes, and in case of a volatile key assign it
1614 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001615 * -# Populate the slot with the key material.
1616 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1617 * In case of failure at any step, stop the sequence and call
1618 * psa_fail_key_creation().
1619 *
Ronald Cron5c522922020-11-14 16:35:34 +01001620 * On success, the key slot is locked. It is the responsibility of the caller
1621 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001622 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001623 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001624 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001625 * \param[out] p_slot On success, a pointer to the prepared slot.
1626 * \param[out] p_drv On any return, the driver for the key, if any.
1627 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001628 *
1629 * \retval #PSA_SUCCESS
1630 * The key slot is ready to receive key material.
1631 * \return If this function fails, the key slot is an invalid state.
1632 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 */
1634static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001635 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001636 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001637 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001638 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001639{
1640 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001641 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001642 psa_key_slot_t *slot;
1643
Gilles Peskinedf179142019-07-15 22:02:14 +02001644 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001645 *p_drv = NULL;
1646
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001647 status = psa_validate_key_attributes( attributes, p_drv );
1648 if( status != PSA_SUCCESS )
1649 return( status );
1650
Ronald Cronc4d1b512020-07-31 11:26:37 +02001651 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 if( status != PSA_SUCCESS )
1653 return( status );
1654 slot = *p_slot;
1655
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001656 /* We're storing the declared bit-size of the key. It's up to each
1657 * creation mechanism to verify that this information is correct.
1658 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001659 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001660 * is optional (import, copy). In case of a volatile key, assign it the
1661 * volatile key identifier associated to the slot returned to contain its
1662 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001663
1664 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001665 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1666 {
1667#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1668 slot->attr.id = volatile_key_id;
1669#else
1670 slot->attr.id.key_id = volatile_key_id;
1671#endif
1672 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001673
Gilles Peskine91e8c332019-08-02 19:19:39 +02001674 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001675 * external-only flags, query `attributes`. Thanks to the check
1676 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001677 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001678 * may have set. */
1679 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001680
Gilles Peskinecbaff462019-07-12 23:46:04 +02001681#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001682 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001683 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001684 * create the key file in internal storage, create the
1685 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001686 * persistent data. This is done by starting a transaction that will
1687 * encompass these three actions.
1688 * For registering a volatile key, we just need to find an appropriate
1689 * slot number inside the SE. Since the key is designated volatile, creating
1690 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001691 /* The first thing to do is to find a slot number for the new key.
1692 * We save the slot number in persistent storage as part of the
1693 * transaction data. It will be needed to recover if the power
1694 * fails during the key creation process, to clean up on the secure
1695 * element side after restarting. Obtaining a slot number from the
1696 * secure element driver updates its persistent state, but we do not yet
1697 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001698 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001699 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001700 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001701 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001702 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001703 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001704 if( status != PSA_SUCCESS )
1705 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001706
1707 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001708 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001709 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1710 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001711 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001712 psa_crypto_transaction.key.id = slot->attr.id;
1713 status = psa_crypto_save_transaction( );
1714 if( status != PSA_SUCCESS )
1715 {
1716 (void) psa_crypto_stop_transaction( );
1717 return( status );
1718 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001719 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001720
1721 status = psa_copy_key_material_into_slot(
1722 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001723 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001724
1725 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1726 {
1727 /* Key registration only makes sense with a secure element. */
1728 return( PSA_ERROR_INVALID_ARGUMENT );
1729 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001730#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1731
Ronald Cronc4d1b512020-07-31 11:26:37 +02001732 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001733}
Gilles Peskine4747d192019-04-17 15:05:45 +02001734
1735/** Finalize the creation of a key once its key material has been set.
1736 *
1737 * This entails writing the key to persistent storage.
1738 *
1739 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001740 * See the documentation of psa_start_key_creation() for the intended use
1741 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001742 *
Ronald Cron5c522922020-11-14 16:35:34 +01001743 * If the finalization succeeds, the function unlocks the key slot (it was
1744 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1745 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001746 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001747 * \param[in,out] slot Pointer to the slot with key material.
1748 * \param[in] driver The secure element driver for the key,
1749 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001750 * \param[out] key On success, identifier of the key. Note that the
1751 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001752 *
1753 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001754 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001755 * \return If this function fails, the key slot is an invalid state.
1756 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001757 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001758static psa_status_t psa_finish_key_creation(
1759 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001760 psa_se_drv_table_entry_t *driver,
1761 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001762{
1763 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001764 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001765 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001766
1767#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001768 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001769 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001770#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1771 if( driver != NULL )
1772 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001773 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001774 psa_key_slot_number_t slot_number =
1775 psa_key_slot_get_slot_number( slot ) ;
1776
Gilles Peskineb46bef22019-07-30 21:32:04 +02001777#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001778 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001779 sizeof( data.slot_number ),
1780 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001781#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001782 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001783 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001784 (uint8_t*) &data,
1785 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001786 }
1787 else
1788#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1789 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001790 /* Key material is saved in export representation in the slot, so
1791 * just pass the slot buffer for storage. */
1792 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001793 slot->key.data,
1794 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001795 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001796 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001797#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1798
Gilles Peskinecbaff462019-07-12 23:46:04 +02001799#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001800 /* Finish the transaction for a key creation. This does not
1801 * happen when registering an existing key. Detect this case
1802 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001803 * creation of a persistent key in a secure element requires a transaction,
1804 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001805 if( driver != NULL &&
1806 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001807 {
1808 status = psa_save_se_persistent_data( driver );
1809 if( status != PSA_SUCCESS )
1810 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001811 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001812 return( status );
1813 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001814 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001815 }
1816#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1817
Ronald Cron50972942020-11-14 11:28:25 +01001818 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001819 {
1820 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001821 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001822 if( status != PSA_SUCCESS )
1823 *key = MBEDTLS_SVC_KEY_ID_INIT;
1824 }
Ronald Cron50972942020-11-14 11:28:25 +01001825
Gilles Peskine4747d192019-04-17 15:05:45 +02001826 return( status );
1827}
1828
1829/** Abort the creation of a key.
1830 *
1831 * You may call this function after calling psa_start_key_creation(),
1832 * or after psa_finish_key_creation() fails. In other circumstances, this
1833 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001834 * See the documentation of psa_start_key_creation() for the intended use
1835 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001836 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001837 * \param[in,out] slot Pointer to the slot with key material.
1838 * \param[in] driver The secure element driver for the key,
1839 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001840 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001841static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001842 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001843{
Gilles Peskine011e4282019-06-26 18:34:38 +02001844 (void) driver;
1845
Gilles Peskine4747d192019-04-17 15:05:45 +02001846 if( slot == NULL )
1847 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001848
1849#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001850 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001851 * element, and the failure happened later (when saving metadata
1852 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001853 * element.
1854 * https://github.com/ARMmbed/mbed-crypto/issues/217
1855 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001856
Gilles Peskined7729582019-08-05 15:55:54 +02001857 /* Abort the ongoing transaction if any (there may not be one if
1858 * the creation process failed before starting one, or if the
1859 * key creation is a registration of a key in a secure element).
1860 * Earlier functions must already have done what it takes to undo any
1861 * partial creation. All that's left is to update the transaction data
1862 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001863 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001864#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1865
Gilles Peskine4747d192019-04-17 15:05:45 +02001866 psa_wipe_key_slot( slot );
1867}
1868
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001869/** Validate optional attributes during key creation.
1870 *
1871 * Some key attributes are optional during key creation. If they are
1872 * specified in the attributes structure, check that they are consistent
1873 * with the data in the slot.
1874 *
1875 * This function should be called near the end of key creation, after
1876 * the slot in memory is fully populated but before saving persistent data.
1877 */
1878static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001879 const psa_key_slot_t *slot,
1880 const psa_key_attributes_t *attributes )
1881{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001882 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001883 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001884 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001885 return( PSA_ERROR_INVALID_ARGUMENT );
1886 }
1887
1888 if( attributes->domain_parameters_size != 0 )
1889 {
John Durkop0e005192020-10-31 22:06:54 -07001890#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1891 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001892 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001893 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001894 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001895 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001896 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001897
Ronald Cron90857082020-11-25 14:58:33 +01001898 psa_status_t status = mbedtls_psa_rsa_load_representation(
1899 slot->attr.type,
1900 slot->key.data,
1901 slot->key.bytes,
1902 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001903 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001904 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001905
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001906 mbedtls_mpi_init( &actual );
1907 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001908 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001909 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001910 mbedtls_rsa_free( rsa );
1911 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001912 if( ret != 0 )
1913 goto rsa_exit;
1914 ret = mbedtls_mpi_read_binary( &required,
1915 attributes->domain_parameters,
1916 attributes->domain_parameters_size );
1917 if( ret != 0 )
1918 goto rsa_exit;
1919 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1920 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1921 rsa_exit:
1922 mbedtls_mpi_free( &actual );
1923 mbedtls_mpi_free( &required );
1924 if( ret != 0)
1925 return( mbedtls_to_psa_error( ret ) );
1926 }
1927 else
John Durkop9814fa22020-11-04 12:28:15 -08001928#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1929 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001930 {
1931 return( PSA_ERROR_INVALID_ARGUMENT );
1932 }
1933 }
1934
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001935 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001936 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001937 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001938 return( PSA_ERROR_INVALID_ARGUMENT );
1939 }
1940
1941 return( PSA_SUCCESS );
1942}
1943
Gilles Peskine4747d192019-04-17 15:05:45 +02001944psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001945 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001946 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001947 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001948{
1949 psa_status_t status;
1950 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001951 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001952
Ronald Cron81709fc2020-11-14 12:10:32 +01001953 *key = MBEDTLS_SVC_KEY_ID_INIT;
1954
Gilles Peskine0f84d622019-09-12 19:03:13 +02001955 /* Reject zero-length symmetric keys (including raw data key objects).
1956 * This also rejects any key which might be encoded as an empty string,
1957 * which is never valid. */
1958 if( data_length == 0 )
1959 return( PSA_ERROR_INVALID_ARGUMENT );
1960
Gilles Peskinedf179142019-07-15 22:02:14 +02001961 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001962 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963 if( status != PSA_SUCCESS )
1964 goto exit;
1965
Gilles Peskine5d309672019-07-12 23:47:28 +02001966#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1967 if( driver != NULL )
1968 {
1969 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001970 /* The driver should set the number of key bits, however in
1971 * case it doesn't, we initialize bits to an invalid value. */
1972 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001973 if( drv->key_management == NULL ||
1974 drv->key_management->p_import == NULL )
1975 {
1976 status = PSA_ERROR_NOT_SUPPORTED;
1977 goto exit;
1978 }
1979 status = drv->key_management->p_import(
1980 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01001981 psa_key_slot_get_slot_number( slot ),
1982 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001983 if( status != PSA_SUCCESS )
1984 goto exit;
1985 if( bits > PSA_MAX_KEY_BITS )
1986 {
1987 status = PSA_ERROR_NOT_SUPPORTED;
1988 goto exit;
1989 }
1990 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001991 }
1992 else
1993#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01001994 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
1995 {
1996 /* Importing a key with external lifetime through the driver wrapper
1997 * interface is not yet supported. Return as if this was an invalid
1998 * lifetime. */
1999 status = PSA_ERROR_INVALID_ARGUMENT;
2000 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002001 }
2002 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002003 {
2004 status = psa_import_key_into_slot( slot, data, data_length );
2005 if( status != PSA_SUCCESS )
2006 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002007 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002008 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002009 if( status != PSA_SUCCESS )
2010 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011
Ronald Cron81709fc2020-11-14 12:10:32 +01002012 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002013exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002014 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002015 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002016
Gilles Peskine4747d192019-04-17 15:05:45 +02002017 return( status );
2018}
2019
Gilles Peskined7729582019-08-05 15:55:54 +02002020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2021psa_status_t mbedtls_psa_register_se_key(
2022 const psa_key_attributes_t *attributes )
2023{
2024 psa_status_t status;
2025 psa_key_slot_t *slot = NULL;
2026 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002027 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002028
2029 /* Leaving attributes unspecified is not currently supported.
2030 * It could make sense to query the key type and size from the
2031 * secure element, but not all secure elements support this
2032 * and the driver HAL doesn't currently support it. */
2033 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2034 return( PSA_ERROR_NOT_SUPPORTED );
2035 if( psa_get_key_bits( attributes ) == 0 )
2036 return( PSA_ERROR_NOT_SUPPORTED );
2037
2038 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002039 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002040 if( status != PSA_SUCCESS )
2041 goto exit;
2042
Ronald Cron81709fc2020-11-14 12:10:32 +01002043 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002044
2045exit:
2046 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002047 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002048
Gilles Peskined7729582019-08-05 15:55:54 +02002049 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002050 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002051 return( status );
2052}
2053#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2054
Gilles Peskinef603c712019-01-19 13:40:11 +01002055static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002056 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002057{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002058 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002059 source->key.data,
2060 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002061 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002062 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002063
Steven Cooreman398aee52020-10-13 14:35:45 +02002064 target->attr.type = source->attr.type;
2065 target->attr.bits = source->attr.bits;
2066
2067 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002068}
2069
Ronald Croncf56a0a2020-08-04 09:51:30 +02002070psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002071 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002072 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002073{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002074 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002075 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002076 psa_key_slot_t *source_slot = NULL;
2077 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002078 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002079 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002080
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2082
Ronald Cron5c522922020-11-14 16:35:34 +01002083 status = psa_get_and_lock_transparent_key_slot_with_policy(
2084 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002085 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002086 goto exit;
2087
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002088 status = psa_validate_optional_attributes( source_slot,
2089 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002090 if( status != PSA_SUCCESS )
2091 goto exit;
2092
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002093 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002094 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002095 if( status != PSA_SUCCESS )
2096 goto exit;
2097
Ronald Cron81709fc2020-11-14 12:10:32 +01002098 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2099 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002100 if( status != PSA_SUCCESS )
2101 goto exit;
2102
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002103#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2104 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002105 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002106 /* Copying to a secure element is not implemented yet. */
2107 status = PSA_ERROR_NOT_SUPPORTED;
2108 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002109 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002110#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002111
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002112 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002113 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002114 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002115
Ronald Cron81709fc2020-11-14 12:10:32 +01002116 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002117exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002118 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002119 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002120
Ronald Cron5c522922020-11-14 16:35:34 +01002121 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002122
Ronald Cron5c522922020-11-14 16:35:34 +01002123 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002124}
2125
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002126
2127
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002128/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002129/* Message digests */
2130/****************************************************************/
2131
John Durkop07cc04a2020-11-16 22:08:34 -08002132#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002133 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2134 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002135 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002136static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002137{
2138 switch( alg )
2139 {
John Durkopee4e6602020-11-27 08:48:46 -08002140#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002141 case PSA_ALG_MD2:
2142 return( &mbedtls_md2_info );
2143#endif
John Durkopee4e6602020-11-27 08:48:46 -08002144#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002145 case PSA_ALG_MD4:
2146 return( &mbedtls_md4_info );
2147#endif
John Durkopee4e6602020-11-27 08:48:46 -08002148#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002149 case PSA_ALG_MD5:
2150 return( &mbedtls_md5_info );
2151#endif
John Durkopee4e6602020-11-27 08:48:46 -08002152#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002153 case PSA_ALG_RIPEMD160:
2154 return( &mbedtls_ripemd160_info );
2155#endif
John Durkopee4e6602020-11-27 08:48:46 -08002156#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002157 case PSA_ALG_SHA_1:
2158 return( &mbedtls_sha1_info );
2159#endif
John Durkopee4e6602020-11-27 08:48:46 -08002160#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002161 case PSA_ALG_SHA_224:
2162 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002163#endif
2164#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002165 case PSA_ALG_SHA_256:
2166 return( &mbedtls_sha256_info );
2167#endif
John Durkopee4e6602020-11-27 08:48:46 -08002168#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002169 case PSA_ALG_SHA_384:
2170 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002171#endif
John Durkopee4e6602020-11-27 08:48:46 -08002172#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002173 case PSA_ALG_SHA_512:
2174 return( &mbedtls_sha512_info );
2175#endif
2176 default:
2177 return( NULL );
2178 }
2179}
John Durkop07cc04a2020-11-16 22:08:34 -08002180#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002181 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2182 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2183 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002184
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002185psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2186{
2187 switch( operation->alg )
2188 {
Gilles Peskine81736312018-06-26 15:04:31 +02002189 case 0:
2190 /* The object has (apparently) been initialized but it is not
2191 * in use. It's ok to call abort on such an object, and there's
2192 * nothing to do. */
2193 break;
John Durkopee4e6602020-11-27 08:48:46 -08002194#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195 case PSA_ALG_MD2:
2196 mbedtls_md2_free( &operation->ctx.md2 );
2197 break;
2198#endif
John Durkopee4e6602020-11-27 08:48:46 -08002199#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002200 case PSA_ALG_MD4:
2201 mbedtls_md4_free( &operation->ctx.md4 );
2202 break;
2203#endif
John Durkopee4e6602020-11-27 08:48:46 -08002204#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002205 case PSA_ALG_MD5:
2206 mbedtls_md5_free( &operation->ctx.md5 );
2207 break;
2208#endif
John Durkopee4e6602020-11-27 08:48:46 -08002209#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210 case PSA_ALG_RIPEMD160:
2211 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2212 break;
2213#endif
John Durkopee4e6602020-11-27 08:48:46 -08002214#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002215 case PSA_ALG_SHA_1:
2216 mbedtls_sha1_free( &operation->ctx.sha1 );
2217 break;
2218#endif
John Durkop6ca23272020-12-03 06:01:32 -08002219#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002220 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002221 mbedtls_sha256_free( &operation->ctx.sha256 );
2222 break;
2223#endif
2224#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002225 case PSA_ALG_SHA_256:
2226 mbedtls_sha256_free( &operation->ctx.sha256 );
2227 break;
2228#endif
John Durkop6ca23272020-12-03 06:01:32 -08002229#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002230 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002231 mbedtls_sha512_free( &operation->ctx.sha512 );
2232 break;
2233#endif
2234#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002235 case PSA_ALG_SHA_512:
2236 mbedtls_sha512_free( &operation->ctx.sha512 );
2237 break;
2238#endif
2239 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002240 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241 }
2242 operation->alg = 0;
2243 return( PSA_SUCCESS );
2244}
2245
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002246psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247 psa_algorithm_t alg )
2248{
Janos Follath24eed8d2019-11-22 13:21:35 +00002249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002250
2251 /* A context must be freshly initialized before it can be set up. */
2252 if( operation->alg != 0 )
2253 {
2254 return( PSA_ERROR_BAD_STATE );
2255 }
2256
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257 switch( alg )
2258 {
John Durkopee4e6602020-11-27 08:48:46 -08002259#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 case PSA_ALG_MD2:
2261 mbedtls_md2_init( &operation->ctx.md2 );
2262 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2263 break;
2264#endif
John Durkopee4e6602020-11-27 08:48:46 -08002265#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266 case PSA_ALG_MD4:
2267 mbedtls_md4_init( &operation->ctx.md4 );
2268 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2269 break;
2270#endif
John Durkopee4e6602020-11-27 08:48:46 -08002271#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272 case PSA_ALG_MD5:
2273 mbedtls_md5_init( &operation->ctx.md5 );
2274 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2275 break;
2276#endif
John Durkopee4e6602020-11-27 08:48:46 -08002277#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278 case PSA_ALG_RIPEMD160:
2279 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2280 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2281 break;
2282#endif
John Durkopee4e6602020-11-27 08:48:46 -08002283#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284 case PSA_ALG_SHA_1:
2285 mbedtls_sha1_init( &operation->ctx.sha1 );
2286 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2287 break;
2288#endif
John Durkopee4e6602020-11-27 08:48:46 -08002289#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290 case PSA_ALG_SHA_224:
2291 mbedtls_sha256_init( &operation->ctx.sha256 );
2292 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2293 break;
John Durkopee4e6602020-11-27 08:48:46 -08002294#endif
2295#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002296 case PSA_ALG_SHA_256:
2297 mbedtls_sha256_init( &operation->ctx.sha256 );
2298 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2299 break;
2300#endif
John Durkopee4e6602020-11-27 08:48:46 -08002301#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002302 case PSA_ALG_SHA_384:
2303 mbedtls_sha512_init( &operation->ctx.sha512 );
2304 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2305 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002306#endif
John Durkopee4e6602020-11-27 08:48:46 -08002307#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308 case PSA_ALG_SHA_512:
2309 mbedtls_sha512_init( &operation->ctx.sha512 );
2310 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2311 break;
2312#endif
2313 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002314 return( PSA_ALG_IS_HASH( alg ) ?
2315 PSA_ERROR_NOT_SUPPORTED :
2316 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317 }
2318 if( ret == 0 )
2319 operation->alg = alg;
2320 else
2321 psa_hash_abort( operation );
2322 return( mbedtls_to_psa_error( ret ) );
2323}
2324
2325psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2326 const uint8_t *input,
2327 size_t input_length )
2328{
Janos Follath24eed8d2019-11-22 13:21:35 +00002329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002330
2331 /* Don't require hash implementations to behave correctly on a
2332 * zero-length input, which may have an invalid pointer. */
2333 if( input_length == 0 )
2334 return( PSA_SUCCESS );
2335
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336 switch( operation->alg )
2337 {
John Durkopee4e6602020-11-27 08:48:46 -08002338#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 case PSA_ALG_MD2:
2340 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2341 input, input_length );
2342 break;
2343#endif
John Durkopee4e6602020-11-27 08:48:46 -08002344#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 case PSA_ALG_MD4:
2346 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2347 input, input_length );
2348 break;
2349#endif
John Durkopee4e6602020-11-27 08:48:46 -08002350#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351 case PSA_ALG_MD5:
2352 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2353 input, input_length );
2354 break;
2355#endif
John Durkopee4e6602020-11-27 08:48:46 -08002356#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002357 case PSA_ALG_RIPEMD160:
2358 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2359 input, input_length );
2360 break;
2361#endif
John Durkopee4e6602020-11-27 08:48:46 -08002362#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002363 case PSA_ALG_SHA_1:
2364 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2365 input, input_length );
2366 break;
2367#endif
John Durkop6ca23272020-12-03 06:01:32 -08002368#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002369 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002370 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2371 input, input_length );
2372 break;
2373#endif
2374#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375 case PSA_ALG_SHA_256:
2376 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2377 input, input_length );
2378 break;
2379#endif
John Durkop6ca23272020-12-03 06:01:32 -08002380#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002382 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2383 input, input_length );
2384 break;
2385#endif
2386#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002387 case PSA_ALG_SHA_512:
2388 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2389 input, input_length );
2390 break;
2391#endif
2392 default:
John Durkopee4e6602020-11-27 08:48:46 -08002393 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002394 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002395 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002396
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002397 if( ret != 0 )
2398 psa_hash_abort( operation );
2399 return( mbedtls_to_psa_error( ret ) );
2400}
2401
2402psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2403 uint8_t *hash,
2404 size_t hash_size,
2405 size_t *hash_length )
2406{
itayzafrir40835d42018-08-02 13:14:17 +03002407 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002409 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410
2411 /* Fill the output buffer with something that isn't a valid hash
2412 * (barring an attack on the hash and deliberately-crafted input),
2413 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002414 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002415 /* If hash_size is 0 then hash may be NULL and then the
2416 * call to memset would have undefined behavior. */
2417 if( hash_size != 0 )
2418 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002419
2420 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002421 {
2422 status = PSA_ERROR_BUFFER_TOO_SMALL;
2423 goto exit;
2424 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002425
2426 switch( operation->alg )
2427 {
John Durkopee4e6602020-11-27 08:48:46 -08002428#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429 case PSA_ALG_MD2:
2430 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2431 break;
2432#endif
John Durkopee4e6602020-11-27 08:48:46 -08002433#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002434 case PSA_ALG_MD4:
2435 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2436 break;
2437#endif
John Durkopee4e6602020-11-27 08:48:46 -08002438#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002439 case PSA_ALG_MD5:
2440 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2441 break;
2442#endif
John Durkopee4e6602020-11-27 08:48:46 -08002443#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002444 case PSA_ALG_RIPEMD160:
2445 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2446 break;
2447#endif
John Durkopee4e6602020-11-27 08:48:46 -08002448#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002449 case PSA_ALG_SHA_1:
2450 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2451 break;
2452#endif
John Durkop6ca23272020-12-03 06:01:32 -08002453#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002454 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002455 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2456 break;
2457#endif
2458#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002459 case PSA_ALG_SHA_256:
2460 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2461 break;
2462#endif
John Durkop6ca23272020-12-03 06:01:32 -08002463#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002464 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002465 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2466 break;
2467#endif
2468#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002469 case PSA_ALG_SHA_512:
2470 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2471 break;
2472#endif
2473 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002474 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002475 }
itayzafrir40835d42018-08-02 13:14:17 +03002476 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002477
itayzafrir40835d42018-08-02 13:14:17 +03002478exit:
2479 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002480 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002481 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002482 return( psa_hash_abort( operation ) );
2483 }
2484 else
2485 {
2486 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002487 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488 }
2489}
2490
Gilles Peskine2d277862018-06-18 15:41:12 +02002491psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2492 const uint8_t *hash,
2493 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494{
2495 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2496 size_t actual_hash_length;
2497 psa_status_t status = psa_hash_finish( operation,
2498 actual_hash, sizeof( actual_hash ),
2499 &actual_hash_length );
2500 if( status != PSA_SUCCESS )
2501 return( status );
2502 if( actual_hash_length != hash_length )
2503 return( PSA_ERROR_INVALID_SIGNATURE );
2504 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2505 return( PSA_ERROR_INVALID_SIGNATURE );
2506 return( PSA_SUCCESS );
2507}
2508
Gilles Peskine0a749c82019-11-28 19:33:58 +01002509psa_status_t psa_hash_compute( psa_algorithm_t alg,
2510 const uint8_t *input, size_t input_length,
2511 uint8_t *hash, size_t hash_size,
2512 size_t *hash_length )
2513{
2514 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2515 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2516
2517 *hash_length = hash_size;
2518 status = psa_hash_setup( &operation, alg );
2519 if( status != PSA_SUCCESS )
2520 goto exit;
2521 status = psa_hash_update( &operation, input, input_length );
2522 if( status != PSA_SUCCESS )
2523 goto exit;
2524 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2525 if( status != PSA_SUCCESS )
2526 goto exit;
2527
2528exit:
2529 if( status == PSA_SUCCESS )
2530 status = psa_hash_abort( &operation );
2531 else
2532 psa_hash_abort( &operation );
2533 return( status );
2534}
2535
2536psa_status_t psa_hash_compare( psa_algorithm_t alg,
2537 const uint8_t *input, size_t input_length,
2538 const uint8_t *hash, size_t hash_length )
2539{
2540 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2541 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2542
2543 status = psa_hash_setup( &operation, alg );
2544 if( status != PSA_SUCCESS )
2545 goto exit;
2546 status = psa_hash_update( &operation, input, input_length );
2547 if( status != PSA_SUCCESS )
2548 goto exit;
2549 status = psa_hash_verify( &operation, hash, hash_length );
2550 if( status != PSA_SUCCESS )
2551 goto exit;
2552
2553exit:
2554 if( status == PSA_SUCCESS )
2555 status = psa_hash_abort( &operation );
2556 else
2557 psa_hash_abort( &operation );
2558 return( status );
2559}
2560
Gilles Peskineeb35d782019-01-22 17:56:16 +01002561psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2562 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002563{
2564 if( target_operation->alg != 0 )
2565 return( PSA_ERROR_BAD_STATE );
2566
2567 switch( source_operation->alg )
2568 {
2569 case 0:
2570 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002571#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002572 case PSA_ALG_MD2:
2573 mbedtls_md2_clone( &target_operation->ctx.md2,
2574 &source_operation->ctx.md2 );
2575 break;
2576#endif
John Durkopee4e6602020-11-27 08:48:46 -08002577#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002578 case PSA_ALG_MD4:
2579 mbedtls_md4_clone( &target_operation->ctx.md4,
2580 &source_operation->ctx.md4 );
2581 break;
2582#endif
John Durkopee4e6602020-11-27 08:48:46 -08002583#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002584 case PSA_ALG_MD5:
2585 mbedtls_md5_clone( &target_operation->ctx.md5,
2586 &source_operation->ctx.md5 );
2587 break;
2588#endif
John Durkopee4e6602020-11-27 08:48:46 -08002589#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002590 case PSA_ALG_RIPEMD160:
2591 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2592 &source_operation->ctx.ripemd160 );
2593 break;
2594#endif
John Durkopee4e6602020-11-27 08:48:46 -08002595#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002596 case PSA_ALG_SHA_1:
2597 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2598 &source_operation->ctx.sha1 );
2599 break;
2600#endif
John Durkop6ca23272020-12-03 06:01:32 -08002601#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002602 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002603 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2604 &source_operation->ctx.sha256 );
2605 break;
2606#endif
2607#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002608 case PSA_ALG_SHA_256:
2609 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2610 &source_operation->ctx.sha256 );
2611 break;
2612#endif
John Durkop6ca23272020-12-03 06:01:32 -08002613#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002614 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002615 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2616 &source_operation->ctx.sha512 );
2617 break;
2618#endif
2619#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002620 case PSA_ALG_SHA_512:
2621 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2622 &source_operation->ctx.sha512 );
2623 break;
2624#endif
2625 default:
2626 return( PSA_ERROR_NOT_SUPPORTED );
2627 }
2628
2629 target_operation->alg = source_operation->alg;
2630 return( PSA_SUCCESS );
2631}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002632
2633
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002634/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002635/* MAC */
2636/****************************************************************/
2637
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002638static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002639 psa_algorithm_t alg,
2640 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002641 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002642 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002645 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002646
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002647 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002648 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002649
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2651 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002652 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002653 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002654 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002655 mode = MBEDTLS_MODE_STREAM;
2656 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657 case PSA_ALG_CTR:
2658 mode = MBEDTLS_MODE_CTR;
2659 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002660 case PSA_ALG_CFB:
2661 mode = MBEDTLS_MODE_CFB;
2662 break;
2663 case PSA_ALG_OFB:
2664 mode = MBEDTLS_MODE_OFB;
2665 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002666 case PSA_ALG_ECB_NO_PADDING:
2667 mode = MBEDTLS_MODE_ECB;
2668 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002669 case PSA_ALG_CBC_NO_PADDING:
2670 mode = MBEDTLS_MODE_CBC;
2671 break;
2672 case PSA_ALG_CBC_PKCS7:
2673 mode = MBEDTLS_MODE_CBC;
2674 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002675 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002676 mode = MBEDTLS_MODE_CCM;
2677 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002678 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679 mode = MBEDTLS_MODE_GCM;
2680 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002681 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2682 mode = MBEDTLS_MODE_CHACHAPOLY;
2683 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684 default:
2685 return( NULL );
2686 }
2687 }
2688 else if( alg == PSA_ALG_CMAC )
2689 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002690 else
2691 return( NULL );
2692
2693 switch( key_type )
2694 {
2695 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002696 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697 break;
2698 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002699 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2700 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002702 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002703 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002704 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002705 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2706 * but two-key Triple-DES is functionally three-key Triple-DES
2707 * with K1=K3, so that's how we present it to mbedtls. */
2708 if( key_bits == 128 )
2709 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710 break;
2711 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002712 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002713 break;
2714 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002715 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002717 case PSA_KEY_TYPE_CHACHA20:
2718 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2719 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002720 default:
2721 return( NULL );
2722 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002723 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002724 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002725
Jaeden Amero23bbb752018-06-26 14:16:54 +01002726 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2727 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728}
2729
John Durkop6ba40d12020-11-10 08:50:04 -08002730#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002731static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002732{
Gilles Peskine2d277862018-06-18 15:41:12 +02002733 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002734 {
2735 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002736 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002737 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002738 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002739 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002740 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002741 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002742 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002743 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002744 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002745 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002746 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002747 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002748 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002749 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002750 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002751 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002752 return( 128 );
2753 default:
2754 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002755 }
2756}
John Durkop07cc04a2020-11-16 22:08:34 -08002757#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002758
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002759/* Initialize the MAC operation structure. Once this function has been
2760 * called, psa_mac_abort can run and will do the right thing. */
2761static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2762 psa_algorithm_t alg )
2763{
2764 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2765
2766 operation->alg = alg;
2767 operation->key_set = 0;
2768 operation->iv_set = 0;
2769 operation->iv_required = 0;
2770 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002771 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002772
2773#if defined(MBEDTLS_CMAC_C)
2774 if( alg == PSA_ALG_CMAC )
2775 {
2776 operation->iv_required = 0;
2777 mbedtls_cipher_init( &operation->ctx.cmac );
2778 status = PSA_SUCCESS;
2779 }
2780 else
2781#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002782#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002783 if( PSA_ALG_IS_HMAC( operation->alg ) )
2784 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002785 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2786 operation->ctx.hmac.hash_ctx.alg = 0;
2787 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002788 }
2789 else
John Durkopd0321952020-10-29 21:37:36 -07002790#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002791 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002792 if( ! PSA_ALG_IS_MAC( alg ) )
2793 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002794 }
2795
2796 if( status != PSA_SUCCESS )
2797 memset( operation, 0, sizeof( *operation ) );
2798 return( status );
2799}
2800
John Durkop6ba40d12020-11-10 08:50:04 -08002801#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002802static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2803{
Gilles Peskine3f108122018-12-07 18:14:53 +01002804 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002805 return( psa_hash_abort( &hmac->hash_ctx ) );
2806}
John Durkop6ba40d12020-11-10 08:50:04 -08002807#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002808
Gilles Peskine8c9def32018-02-08 10:02:12 +01002809psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2810{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002811 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002812 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002813 /* The object has (apparently) been initialized but it is not
2814 * in use. It's ok to call abort on such an object, and there's
2815 * nothing to do. */
2816 return( PSA_SUCCESS );
2817 }
2818 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002819#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002820 if( operation->alg == PSA_ALG_CMAC )
2821 {
2822 mbedtls_cipher_free( &operation->ctx.cmac );
2823 }
2824 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002825#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002826#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002827 if( PSA_ALG_IS_HMAC( operation->alg ) )
2828 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002829 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002830 }
2831 else
John Durkopd0321952020-10-29 21:37:36 -07002832#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002833 {
2834 /* Sanity check (shouldn't happen: operation->alg should
2835 * always have been initialized to a valid value). */
2836 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837 }
Moran Peker41deec42018-04-04 15:43:05 +03002838
Gilles Peskine8c9def32018-02-08 10:02:12 +01002839 operation->alg = 0;
2840 operation->key_set = 0;
2841 operation->iv_set = 0;
2842 operation->iv_required = 0;
2843 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002844 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002845
Gilles Peskine8c9def32018-02-08 10:02:12 +01002846 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002847
2848bad_state:
2849 /* If abort is called on an uninitialized object, we can't trust
2850 * anything. Wipe the object in case it contains confidential data.
2851 * This may result in a memory leak if a pointer gets overwritten,
2852 * but it's too late to do anything about this. */
2853 memset( operation, 0, sizeof( *operation ) );
2854 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002855}
2856
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002857#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002858static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002859 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002860 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002861 const mbedtls_cipher_info_t *cipher_info )
2862{
Janos Follath24eed8d2019-11-22 13:21:35 +00002863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002864
2865 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002866
2867 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2868 if( ret != 0 )
2869 return( ret );
2870
2871 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002872 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002873 key_bits );
2874 return( ret );
2875}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002876#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002877
John Durkop6ba40d12020-11-10 08:50:04 -08002878#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002879static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2880 const uint8_t *key,
2881 size_t key_length,
2882 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002883{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002884 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002885 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002886 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002887 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002888 psa_status_t status;
2889
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002890 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2891 * overflow below. This should never trigger if the hash algorithm
2892 * is implemented correctly. */
2893 /* The size checks against the ipad and opad buffers cannot be written
2894 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2895 * because that triggers -Wlogical-op on GCC 7.3. */
2896 if( block_size > sizeof( ipad ) )
2897 return( PSA_ERROR_NOT_SUPPORTED );
2898 if( block_size > sizeof( hmac->opad ) )
2899 return( PSA_ERROR_NOT_SUPPORTED );
2900 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002901 return( PSA_ERROR_NOT_SUPPORTED );
2902
Gilles Peskined223b522018-06-11 18:12:58 +02002903 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002904 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002905 status = psa_hash_compute( hash_alg, key, key_length,
2906 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002907 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002908 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002909 }
Gilles Peskine96889972018-07-12 17:07:03 +02002910 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2911 * but it is permitted. It is common when HMAC is used in HKDF, for
2912 * example. Don't call `memcpy` in the 0-length because `key` could be
2913 * an invalid pointer which would make the behavior undefined. */
2914 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002915 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002916
Gilles Peskined223b522018-06-11 18:12:58 +02002917 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2918 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002919 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002920 ipad[i] ^= 0x36;
2921 memset( ipad + key_length, 0x36, block_size - key_length );
2922
2923 /* Copy the key material from ipad to opad, flipping the requisite bits,
2924 * and filling the rest of opad with the requisite constant. */
2925 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002926 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2927 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002928
Gilles Peskine01126fa2018-07-12 17:04:55 +02002929 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002930 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002931 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002932
Gilles Peskine01126fa2018-07-12 17:04:55 +02002933 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002934
2935cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002936 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002937
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002938 return( status );
2939}
John Durkop6ba40d12020-11-10 08:50:04 -08002940#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002941
Gilles Peskine89167cb2018-07-08 20:12:23 +02002942static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002943 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002944 psa_algorithm_t alg,
2945 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002946{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002947 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002948 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002949 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002950 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002951 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002952 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002953 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002954 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002955
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002956 /* A context must be freshly initialized before it can be set up. */
2957 if( operation->alg != 0 )
2958 {
2959 return( PSA_ERROR_BAD_STATE );
2960 }
2961
Gilles Peskined911eb72018-08-14 15:18:45 +02002962 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002963 if( status != PSA_SUCCESS )
2964 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002965 if( is_sign )
2966 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002967
Ronald Cron5c522922020-11-14 16:35:34 +01002968 status = psa_get_and_lock_transparent_key_slot_with_policy(
2969 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002970 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002971 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002972 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002973
Gilles Peskine8c9def32018-02-08 10:02:12 +01002974#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002975 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002976 {
2977 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002978 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002979 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002980 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002981 if( cipher_info == NULL )
2982 {
2983 status = PSA_ERROR_NOT_SUPPORTED;
2984 goto exit;
2985 }
2986 operation->mac_size = cipher_info->block_size;
2987 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2988 status = mbedtls_to_psa_error( ret );
2989 }
2990 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002991#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002992#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02002993 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002994 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002995 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002996 if( hash_alg == 0 )
2997 {
2998 status = PSA_ERROR_NOT_SUPPORTED;
2999 goto exit;
3000 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003001
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003002 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003003 /* Sanity check. This shouldn't fail on a valid configuration. */
3004 if( operation->mac_size == 0 ||
3005 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3006 {
3007 status = PSA_ERROR_NOT_SUPPORTED;
3008 goto exit;
3009 }
3010
Gilles Peskine8e338702019-07-30 20:06:31 +02003011 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003012 {
3013 status = PSA_ERROR_INVALID_ARGUMENT;
3014 goto exit;
3015 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003016
Gilles Peskine01126fa2018-07-12 17:04:55 +02003017 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003018 slot->key.data,
3019 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003020 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003021 }
3022 else
John Durkopd0321952020-10-29 21:37:36 -07003023#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003024 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003025 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003026 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003027 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003028
Gilles Peskined911eb72018-08-14 15:18:45 +02003029 if( truncated == 0 )
3030 {
3031 /* The "normal" case: untruncated algorithm. Nothing to do. */
3032 }
3033 else if( truncated < 4 )
3034 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003035 /* A very short MAC is too short for security since it can be
3036 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3037 * so we make this our minimum, even though 32 bits is still
3038 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003039 status = PSA_ERROR_NOT_SUPPORTED;
3040 }
3041 else if( truncated > operation->mac_size )
3042 {
3043 /* It's impossible to "truncate" to a larger length. */
3044 status = PSA_ERROR_INVALID_ARGUMENT;
3045 }
3046 else
3047 operation->mac_size = truncated;
3048
Gilles Peskinefbfac682018-07-08 20:51:54 +02003049exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003050 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003051 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003052 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003053 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003054 else
3055 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003056 operation->key_set = 1;
3057 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003058
Ronald Cron5c522922020-11-14 16:35:34 +01003059 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003060
Ronald Cron5c522922020-11-14 16:35:34 +01003061 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003062}
3063
Gilles Peskine89167cb2018-07-08 20:12:23 +02003064psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003065 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003066 psa_algorithm_t alg )
3067{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003068 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003069}
3070
3071psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003072 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003073 psa_algorithm_t alg )
3074{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003075 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003076}
3077
Gilles Peskine8c9def32018-02-08 10:02:12 +01003078psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3079 const uint8_t *input,
3080 size_t input_length )
3081{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003082 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003083 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003084 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003085 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003086 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003087 operation->has_input = 1;
3088
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003090 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003091 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003092 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3093 input, input_length );
3094 status = mbedtls_to_psa_error( ret );
3095 }
3096 else
3097#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003098#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003099 if( PSA_ALG_IS_HMAC( operation->alg ) )
3100 {
3101 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3102 input_length );
3103 }
3104 else
John Durkopd0321952020-10-29 21:37:36 -07003105#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003106 {
3107 /* This shouldn't happen if `operation` was initialized by
3108 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003109 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003110 }
3111
Gilles Peskinefbfac682018-07-08 20:51:54 +02003112 if( status != PSA_SUCCESS )
3113 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003114 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003115}
3116
John Durkop6ba40d12020-11-10 08:50:04 -08003117#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003118static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3119 uint8_t *mac,
3120 size_t mac_size )
3121{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003122 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003123 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3124 size_t hash_size = 0;
3125 size_t block_size = psa_get_hash_block_size( hash_alg );
3126 psa_status_t status;
3127
Gilles Peskine01126fa2018-07-12 17:04:55 +02003128 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3129 if( status != PSA_SUCCESS )
3130 return( status );
3131 /* From here on, tmp needs to be wiped. */
3132
3133 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3134 if( status != PSA_SUCCESS )
3135 goto exit;
3136
3137 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3138 if( status != PSA_SUCCESS )
3139 goto exit;
3140
3141 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3142 if( status != PSA_SUCCESS )
3143 goto exit;
3144
Gilles Peskined911eb72018-08-14 15:18:45 +02003145 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3146 if( status != PSA_SUCCESS )
3147 goto exit;
3148
3149 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003150
3151exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003152 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003153 return( status );
3154}
John Durkop6ba40d12020-11-10 08:50:04 -08003155#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003156
mohammad16036df908f2018-04-02 08:34:15 -07003157static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003158 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003159 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003160{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003161 if( ! operation->key_set )
3162 return( PSA_ERROR_BAD_STATE );
3163 if( operation->iv_required && ! operation->iv_set )
3164 return( PSA_ERROR_BAD_STATE );
3165
Gilles Peskine8c9def32018-02-08 10:02:12 +01003166 if( mac_size < operation->mac_size )
3167 return( PSA_ERROR_BUFFER_TOO_SMALL );
3168
Gilles Peskine8c9def32018-02-08 10:02:12 +01003169#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003170 if( operation->alg == PSA_ALG_CMAC )
3171 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003172 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003173 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3174 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003175 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003176 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003177 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003178 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003179 else
3180#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003181#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003182 if( PSA_ALG_IS_HMAC( operation->alg ) )
3183 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003184 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003185 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003186 }
3187 else
John Durkopd0321952020-10-29 21:37:36 -07003188#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003189 {
3190 /* This shouldn't happen if `operation` was initialized by
3191 * a setup function. */
3192 return( PSA_ERROR_BAD_STATE );
3193 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003194}
3195
Gilles Peskineacd4be32018-07-08 19:56:25 +02003196psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3197 uint8_t *mac,
3198 size_t mac_size,
3199 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003200{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003201 psa_status_t status;
3202
Jaeden Amero252ef282019-02-15 14:05:35 +00003203 if( operation->alg == 0 )
3204 {
3205 return( PSA_ERROR_BAD_STATE );
3206 }
3207
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003208 /* Fill the output buffer with something that isn't a valid mac
3209 * (barring an attack on the mac and deliberately-crafted input),
3210 * in case the caller doesn't check the return status properly. */
3211 *mac_length = mac_size;
3212 /* If mac_size is 0 then mac may be NULL and then the
3213 * call to memset would have undefined behavior. */
3214 if( mac_size != 0 )
3215 memset( mac, '!', mac_size );
3216
Gilles Peskine89167cb2018-07-08 20:12:23 +02003217 if( ! operation->is_sign )
3218 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003219 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003220 }
mohammad16036df908f2018-04-02 08:34:15 -07003221
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003222 status = psa_mac_finish_internal( operation, mac, mac_size );
3223
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003224 if( status == PSA_SUCCESS )
3225 {
3226 status = psa_mac_abort( operation );
3227 if( status == PSA_SUCCESS )
3228 *mac_length = operation->mac_size;
3229 else
3230 memset( mac, '!', mac_size );
3231 }
3232 else
3233 psa_mac_abort( operation );
3234 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003235}
3236
Gilles Peskineacd4be32018-07-08 19:56:25 +02003237psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3238 const uint8_t *mac,
3239 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003240{
Gilles Peskine828ed142018-06-18 23:25:51 +02003241 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003242 psa_status_t status;
3243
Jaeden Amero252ef282019-02-15 14:05:35 +00003244 if( operation->alg == 0 )
3245 {
3246 return( PSA_ERROR_BAD_STATE );
3247 }
3248
Gilles Peskine89167cb2018-07-08 20:12:23 +02003249 if( operation->is_sign )
3250 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003251 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003252 }
3253 if( operation->mac_size != mac_length )
3254 {
3255 status = PSA_ERROR_INVALID_SIGNATURE;
3256 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003257 }
mohammad16036df908f2018-04-02 08:34:15 -07003258
3259 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003260 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003261 if( status != PSA_SUCCESS )
3262 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003263
3264 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3265 status = PSA_ERROR_INVALID_SIGNATURE;
3266
3267cleanup:
3268 if( status == PSA_SUCCESS )
3269 status = psa_mac_abort( operation );
3270 else
3271 psa_mac_abort( operation );
3272
Gilles Peskine3f108122018-12-07 18:14:53 +01003273 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003274
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003275 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003276}
3277
3278
Gilles Peskine20035e32018-02-03 22:44:14 +01003279
Gilles Peskine20035e32018-02-03 22:44:14 +01003280/****************************************************************/
3281/* Asymmetric cryptography */
3282/****************************************************************/
3283
John Durkop6ba40d12020-11-10 08:50:04 -08003284#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3285 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003286/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003287 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003288static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3289 size_t hash_length,
3290 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003291{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003292 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003293 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003294 *md_alg = mbedtls_md_get_type( md_info );
3295
3296 /* The Mbed TLS RSA module uses an unsigned int for hash length
3297 * parameters. Validate that it fits so that we don't risk an
3298 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003299#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003300 if( hash_length > UINT_MAX )
3301 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003302#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003303
John Durkop0e005192020-10-31 22:06:54 -07003304#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003305 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3306 * must be correct. */
3307 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3308 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003309 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003310 if( md_info == NULL )
3311 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003312 if( mbedtls_md_get_size( md_info ) != hash_length )
3313 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003314 }
John Durkop0e005192020-10-31 22:06:54 -07003315#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003316
John Durkop0e005192020-10-31 22:06:54 -07003317#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003318 /* PSS requires a hash internally. */
3319 if( PSA_ALG_IS_RSA_PSS( alg ) )
3320 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003321 if( md_info == NULL )
3322 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003323 }
John Durkop0e005192020-10-31 22:06:54 -07003324#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003325
Gilles Peskine61b91d42018-06-08 16:09:36 +02003326 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003327}
3328
Gilles Peskine2b450e32018-06-27 15:42:46 +02003329static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3330 psa_algorithm_t alg,
3331 const uint8_t *hash,
3332 size_t hash_length,
3333 uint8_t *signature,
3334 size_t signature_size,
3335 size_t *signature_length )
3336{
3337 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003339 mbedtls_md_type_t md_alg;
3340
3341 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3342 if( status != PSA_SUCCESS )
3343 return( status );
3344
Gilles Peskine630a18a2018-06-29 17:49:35 +02003345 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003346 return( PSA_ERROR_BUFFER_TOO_SMALL );
3347
John Durkop0e005192020-10-31 22:06:54 -07003348#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003349 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3350 {
3351 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3352 MBEDTLS_MD_NONE );
3353 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003354 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003355 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003356 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003357 md_alg,
3358 (unsigned int) hash_length,
3359 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003360 signature );
3361 }
3362 else
John Durkop0e005192020-10-31 22:06:54 -07003363#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3364#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003365 if( PSA_ALG_IS_RSA_PSS( alg ) )
3366 {
3367 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3368 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003369 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003370 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003371 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003372 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003373 (unsigned int) hash_length,
3374 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003375 signature );
3376 }
3377 else
John Durkop0e005192020-10-31 22:06:54 -07003378#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003379 {
3380 return( PSA_ERROR_INVALID_ARGUMENT );
3381 }
3382
3383 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003384 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003385 return( mbedtls_to_psa_error( ret ) );
3386}
3387
3388static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3389 psa_algorithm_t alg,
3390 const uint8_t *hash,
3391 size_t hash_length,
3392 const uint8_t *signature,
3393 size_t signature_length )
3394{
3395 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003397 mbedtls_md_type_t md_alg;
3398
3399 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3400 if( status != PSA_SUCCESS )
3401 return( status );
3402
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003403 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3404 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003405
John Durkop0e005192020-10-31 22:06:54 -07003406#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003407 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3408 {
3409 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3410 MBEDTLS_MD_NONE );
3411 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003412 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003413 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003414 MBEDTLS_RSA_PUBLIC,
3415 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003416 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003417 hash,
3418 signature );
3419 }
3420 else
John Durkop0e005192020-10-31 22:06:54 -07003421#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3422#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003423 if( PSA_ALG_IS_RSA_PSS( alg ) )
3424 {
3425 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3426 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003427 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003428 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003429 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003430 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003431 (unsigned int) hash_length,
3432 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003433 signature );
3434 }
3435 else
John Durkop0e005192020-10-31 22:06:54 -07003436#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003437 {
3438 return( PSA_ERROR_INVALID_ARGUMENT );
3439 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003440
3441 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3442 * the rest of the signature is invalid". This has little use in
3443 * practice and PSA doesn't report this distinction. */
3444 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3445 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003446 return( mbedtls_to_psa_error( ret ) );
3447}
John Durkop6ba40d12020-11-10 08:50:04 -08003448#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3449 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003450
John Durkop6ba40d12020-11-10 08:50:04 -08003451#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3452 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003453/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3454 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3455 * (even though these functions don't modify it). */
3456static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3457 psa_algorithm_t alg,
3458 const uint8_t *hash,
3459 size_t hash_length,
3460 uint8_t *signature,
3461 size_t signature_size,
3462 size_t *signature_length )
3463{
Janos Follath24eed8d2019-11-22 13:21:35 +00003464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003465 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003466 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003467 mbedtls_mpi_init( &r );
3468 mbedtls_mpi_init( &s );
3469
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003470 if( signature_size < 2 * curve_bytes )
3471 {
3472 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3473 goto cleanup;
3474 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003475
John Durkop0ea39e02020-10-13 19:58:20 -07003476#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003477 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3478 {
3479 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3480 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3481 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003482 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3483 &ecp->d, hash,
3484 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003485 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003486 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003487 }
3488 else
John Durkop0ea39e02020-10-13 19:58:20 -07003489#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003490 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003491 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003492 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3493 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003494 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003495 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003496 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003497
3498 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3499 signature,
3500 curve_bytes ) );
3501 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3502 signature + curve_bytes,
3503 curve_bytes ) );
3504
3505cleanup:
3506 mbedtls_mpi_free( &r );
3507 mbedtls_mpi_free( &s );
3508 if( ret == 0 )
3509 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003510 return( mbedtls_to_psa_error( ret ) );
3511}
3512
3513static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3514 const uint8_t *hash,
3515 size_t hash_length,
3516 const uint8_t *signature,
3517 size_t signature_length )
3518{
Janos Follath24eed8d2019-11-22 13:21:35 +00003519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003520 mbedtls_mpi r, s;
3521 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3522 mbedtls_mpi_init( &r );
3523 mbedtls_mpi_init( &s );
3524
3525 if( signature_length != 2 * curve_bytes )
3526 return( PSA_ERROR_INVALID_SIGNATURE );
3527
3528 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3529 signature,
3530 curve_bytes ) );
3531 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3532 signature + curve_bytes,
3533 curve_bytes ) );
3534
Steven Cooremanacda8342020-07-24 23:09:52 +02003535 /* Check whether the public part is loaded. If not, load it. */
3536 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3537 {
3538 MBEDTLS_MPI_CHK(
3539 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003540 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003541 }
3542
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003543 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3544 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003545
3546cleanup:
3547 mbedtls_mpi_free( &r );
3548 mbedtls_mpi_free( &s );
3549 return( mbedtls_to_psa_error( ret ) );
3550}
John Durkop6ba40d12020-11-10 08:50:04 -08003551#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3552 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003553
Ronald Croncf56a0a2020-08-04 09:51:30 +02003554psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003555 psa_algorithm_t alg,
3556 const uint8_t *hash,
3557 size_t hash_length,
3558 uint8_t *signature,
3559 size_t signature_size,
3560 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003561{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003563 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003564 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003565
3566 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003567 /* Immediately reject a zero-length signature buffer. This guarantees
3568 * that signature must be a valid pointer. (On the other hand, the hash
3569 * buffer can in principle be empty since it doesn't actually have
3570 * to be a hash.) */
3571 if( signature_size == 0 )
3572 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003573
Ronald Cron5c522922020-11-14 16:35:34 +01003574 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3575 PSA_KEY_USAGE_SIGN_HASH,
3576 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003577 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003578 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003579 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003580 {
3581 status = PSA_ERROR_INVALID_ARGUMENT;
3582 goto exit;
3583 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003584
Steven Cooremancd84cb42020-07-16 20:28:36 +02003585 /* Try any of the available accelerators first */
3586 status = psa_driver_wrapper_sign_hash( slot,
3587 alg,
3588 hash,
3589 hash_length,
3590 signature,
3591 signature_size,
3592 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003593 if( status != PSA_ERROR_NOT_SUPPORTED ||
3594 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003595 goto exit;
3596
Steven Cooreman7a250572020-07-17 16:43:05 +02003597 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003598#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3599 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003600 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003601 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003602 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003603
Ronald Cron90857082020-11-25 14:58:33 +01003604 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3605 slot->key.data,
3606 slot->key.bytes,
3607 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003608 if( status != PSA_SUCCESS )
3609 goto exit;
3610
Steven Cooremana2371e52020-07-28 14:30:39 +02003611 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003612 alg,
3613 hash, hash_length,
3614 signature, signature_size,
3615 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003616
Steven Cooremana2371e52020-07-28 14:30:39 +02003617 mbedtls_rsa_free( rsa );
3618 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003619 }
3620 else
John Durkop6ba40d12020-11-10 08:50:04 -08003621#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3622 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003623 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003624 {
John Durkop9814fa22020-11-04 12:28:15 -08003625#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3626 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003627 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003628#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003629 PSA_ALG_IS_ECDSA( alg )
3630#else
3631 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3632#endif
3633 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003634 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003635 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003636 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3637 slot->key.data,
3638 slot->key.bytes,
3639 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003640 if( status != PSA_SUCCESS )
3641 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003642 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003643 alg,
3644 hash, hash_length,
3645 signature, signature_size,
3646 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003647 mbedtls_ecp_keypair_free( ecp );
3648 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003649 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003650 else
John Durkop9814fa22020-11-04 12:28:15 -08003651#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3652 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003653 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003654 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003655 }
itayzafrir5c753392018-05-08 11:18:38 +03003656 }
3657 else
itayzafrir5c753392018-05-08 11:18:38 +03003658 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003659 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003660 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003661
3662exit:
3663 /* Fill the unused part of the output buffer (the whole buffer on error,
3664 * the trailing part on success) with something that isn't a valid mac
3665 * (barring an attack on the mac and deliberately-crafted input),
3666 * in case the caller doesn't check the return status properly. */
3667 if( status == PSA_SUCCESS )
3668 memset( signature + *signature_length, '!',
3669 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003670 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003671 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003672 /* If signature_size is 0 then we have nothing to do. We must not call
3673 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003674
Ronald Cron5c522922020-11-14 16:35:34 +01003675 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003676
Ronald Cron5c522922020-11-14 16:35:34 +01003677 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003678}
3679
Ronald Croncf56a0a2020-08-04 09:51:30 +02003680psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003681 psa_algorithm_t alg,
3682 const uint8_t *hash,
3683 size_t hash_length,
3684 const uint8_t *signature,
3685 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003686{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003687 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003688 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003689 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003690
Ronald Cron5c522922020-11-14 16:35:34 +01003691 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3692 PSA_KEY_USAGE_VERIFY_HASH,
3693 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003694 if( status != PSA_SUCCESS )
3695 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003696
Steven Cooreman55ae2172020-07-17 19:46:15 +02003697 /* Try any of the available accelerators first */
3698 status = psa_driver_wrapper_verify_hash( slot,
3699 alg,
3700 hash,
3701 hash_length,
3702 signature,
3703 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003704 if( status != PSA_ERROR_NOT_SUPPORTED ||
3705 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003706 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003707
John Durkop6ba40d12020-11-10 08:50:04 -08003708#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3709 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003710 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003711 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003712 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003713
Ronald Cron90857082020-11-25 14:58:33 +01003714 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3715 slot->key.data,
3716 slot->key.bytes,
3717 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003718 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003719 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003720
Steven Cooremana2371e52020-07-28 14:30:39 +02003721 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003722 alg,
3723 hash, hash_length,
3724 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003725 mbedtls_rsa_free( rsa );
3726 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003727 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728 }
3729 else
John Durkop6ba40d12020-11-10 08:50:04 -08003730#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3731 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003732 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003733 {
John Durkop9814fa22020-11-04 12:28:15 -08003734#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3735 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003736 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003737 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003738 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003739 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3740 slot->key.data,
3741 slot->key.bytes,
3742 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003743 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003744 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003745 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003746 hash, hash_length,
3747 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003748 mbedtls_ecp_keypair_free( ecp );
3749 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003750 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003751 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003752 else
John Durkop9814fa22020-11-04 12:28:15 -08003753#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3754 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003755 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003756 status = PSA_ERROR_INVALID_ARGUMENT;
3757 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003758 }
itayzafrir5c753392018-05-08 11:18:38 +03003759 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003760 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003761 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003762 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003763 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003764
3765exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003766 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003767
Ronald Cron5c522922020-11-14 16:35:34 +01003768 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003769}
3770
John Durkop0e005192020-10-31 22:06:54 -07003771#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003772static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3773 mbedtls_rsa_context *rsa )
3774{
3775 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3776 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3777 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3778 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3779}
John Durkop0e005192020-10-31 22:06:54 -07003780#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003781
Ronald Croncf56a0a2020-08-04 09:51:30 +02003782psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003783 psa_algorithm_t alg,
3784 const uint8_t *input,
3785 size_t input_length,
3786 const uint8_t *salt,
3787 size_t salt_length,
3788 uint8_t *output,
3789 size_t output_size,
3790 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003791{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003793 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003794 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003795
Darryl Green5cc689a2018-07-24 15:34:10 +01003796 (void) input;
3797 (void) input_length;
3798 (void) salt;
3799 (void) output;
3800 (void) output_size;
3801
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003802 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003803
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003804 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3805 return( PSA_ERROR_INVALID_ARGUMENT );
3806
Ronald Cron5c522922020-11-14 16:35:34 +01003807 status = psa_get_and_lock_transparent_key_slot_with_policy(
3808 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003809 if( status != PSA_SUCCESS )
3810 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003811 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3812 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003813 {
3814 status = PSA_ERROR_INVALID_ARGUMENT;
3815 goto exit;
3816 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003817
John Durkop6ba40d12020-11-10 08:50:04 -08003818#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3819 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003820 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003821 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003822 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003823 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3824 slot->key.data,
3825 slot->key.bytes,
3826 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003827 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003828 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003829
3830 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003831 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003832 status = PSA_ERROR_BUFFER_TOO_SMALL;
3833 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003834 }
John Durkop0e005192020-10-31 22:06:54 -07003835#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003836 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003837 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003838 status = mbedtls_to_psa_error(
3839 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003840 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003841 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003842 MBEDTLS_RSA_PUBLIC,
3843 input_length,
3844 input,
3845 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003846 }
3847 else
John Durkop0e005192020-10-31 22:06:54 -07003848#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3849#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003850 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003851 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003852 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003853 status = mbedtls_to_psa_error(
3854 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003855 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003856 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003857 MBEDTLS_RSA_PUBLIC,
3858 salt, salt_length,
3859 input_length,
3860 input,
3861 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003862 }
3863 else
John Durkop0e005192020-10-31 22:06:54 -07003864#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003866 status = PSA_ERROR_INVALID_ARGUMENT;
3867 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003869rsa_exit:
3870 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003871 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003872
Steven Cooremana2371e52020-07-28 14:30:39 +02003873 mbedtls_rsa_free( rsa );
3874 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876 else
John Durkop9814fa22020-11-04 12:28:15 -08003877#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003878 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003879 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003880 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003882
3883exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003884 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003885
Ronald Cron5c522922020-11-14 16:35:34 +01003886 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003887}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003888
Ronald Croncf56a0a2020-08-04 09:51:30 +02003889psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003890 psa_algorithm_t alg,
3891 const uint8_t *input,
3892 size_t input_length,
3893 const uint8_t *salt,
3894 size_t salt_length,
3895 uint8_t *output,
3896 size_t output_size,
3897 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003899 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003900 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003901 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003902
Darryl Green5cc689a2018-07-24 15:34:10 +01003903 (void) input;
3904 (void) input_length;
3905 (void) salt;
3906 (void) output;
3907 (void) output_size;
3908
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003909 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003910
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003911 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3912 return( PSA_ERROR_INVALID_ARGUMENT );
3913
Ronald Cron5c522922020-11-14 16:35:34 +01003914 status = psa_get_and_lock_transparent_key_slot_with_policy(
3915 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003916 if( status != PSA_SUCCESS )
3917 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003918 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003919 {
3920 status = PSA_ERROR_INVALID_ARGUMENT;
3921 goto exit;
3922 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003923
John Durkop6ba40d12020-11-10 08:50:04 -08003924#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3925 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003926 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003927 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003928 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003929 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3930 slot->key.data,
3931 slot->key.bytes,
3932 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003933 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003934 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935
Steven Cooremana2371e52020-07-28 14:30:39 +02003936 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003937 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003938 status = PSA_ERROR_INVALID_ARGUMENT;
3939 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003940 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941
John Durkop0e005192020-10-31 22:06:54 -07003942#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003943 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003945 status = mbedtls_to_psa_error(
3946 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003947 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003948 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003949 MBEDTLS_RSA_PRIVATE,
3950 output_length,
3951 input,
3952 output,
3953 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954 }
3955 else
John Durkop0e005192020-10-31 22:06:54 -07003956#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3957#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003958 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003959 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003960 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003961 status = mbedtls_to_psa_error(
3962 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003963 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003964 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003965 MBEDTLS_RSA_PRIVATE,
3966 salt, salt_length,
3967 output_length,
3968 input,
3969 output,
3970 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003971 }
3972 else
John Durkop0e005192020-10-31 22:06:54 -07003973#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003974 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003975 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003976 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003977
Steven Cooreman4fed4552020-08-03 14:46:03 +02003978rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003979 mbedtls_rsa_free( rsa );
3980 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003981 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003982 else
John Durkop6ba40d12020-11-10 08:50:04 -08003983#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3984 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003985 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003986 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003987 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003988
3989exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003990 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003991
Ronald Cron5c522922020-11-14 16:35:34 +01003992 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003993}
Gilles Peskine20035e32018-02-03 22:44:14 +01003994
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003995
3996
mohammad1603503973b2018-03-12 15:59:30 +02003997/****************************************************************/
3998/* Symmetric cryptography */
3999/****************************************************************/
4000
Gilles Peskinee553c652018-06-04 16:22:46 +02004001static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004002 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004003 psa_algorithm_t alg,
4004 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004005{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004006 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004007 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004008 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004009 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004010 size_t key_bits;
4011 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004012 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4013 PSA_KEY_USAGE_ENCRYPT :
4014 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004015
Steven Cooremand3feccd2020-09-01 15:56:14 +02004016 /* A context must be freshly initialized before it can be set up. */
4017 if( operation->alg != 0 )
4018 return( PSA_ERROR_BAD_STATE );
4019
Steven Cooremana07b9972020-09-10 14:54:14 +02004020 /* The requested algorithm must be one that can be processed by cipher. */
4021 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004022 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004023
Steven Cooremanef8575e2020-09-11 11:44:50 +02004024 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004025 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004026 if( status != PSA_SUCCESS )
4027 goto exit;
4028
4029 /* Initialize the operation struct members, except for alg. The alg member
4030 * is used to indicate to psa_cipher_abort that there are resources to free,
4031 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004032 operation->key_set = 0;
4033 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004034 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004035 operation->iv_size = 0;
4036 operation->block_size = 0;
4037 if( alg == PSA_ALG_ECB_NO_PADDING )
4038 operation->iv_required = 0;
4039 else
4040 operation->iv_required = 1;
4041
Steven Cooremana07b9972020-09-10 14:54:14 +02004042 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004043 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004044 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004045 slot,
4046 alg );
4047 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004048 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004049 slot,
4050 alg );
4051
Steven Cooremanef8575e2020-09-11 11:44:50 +02004052 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004053 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004054 /* Once the driver context is initialised, it needs to be freed using
4055 * psa_cipher_abort. Indicate this through setting alg. */
4056 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004057 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004058
Steven Cooremand3feccd2020-09-01 15:56:14 +02004059 if( status != PSA_ERROR_NOT_SUPPORTED ||
4060 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4061 goto exit;
4062
Steven Cooremana07b9972020-09-10 14:54:14 +02004063 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004064 * available for the given algorithm & key. */
4065 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004066
Steven Cooremana07b9972020-09-10 14:54:14 +02004067 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004068 * psa_cipher_abort. Indicate there is something to be freed through setting
4069 * alg, and indicate the operation is being done using mbedtls crypto through
4070 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004071 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004072 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004073
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004074 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004075 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004076 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004077 {
4078 status = PSA_ERROR_NOT_SUPPORTED;
4079 goto exit;
4080 }
mohammad1603503973b2018-03-12 15:59:30 +02004081
mohammad1603503973b2018-03-12 15:59:30 +02004082 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004083 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004084 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004085
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004086#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004087 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004088 {
4089 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004090 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004091 memcpy( keys, slot->key.data, 16 );
4092 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004093 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4094 keys,
4095 192, cipher_operation );
4096 }
4097 else
4098#endif
4099 {
4100 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004101 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004102 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004103 }
Moran Peker41deec42018-04-04 15:43:05 +03004104 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004105 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004106
mohammad16038481e742018-03-18 13:57:31 +02004107#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004108 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004109 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004110 case PSA_ALG_CBC_NO_PADDING:
4111 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4112 MBEDTLS_PADDING_NONE );
4113 break;
4114 case PSA_ALG_CBC_PKCS7:
4115 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4116 MBEDTLS_PADDING_PKCS7 );
4117 break;
4118 default:
4119 /* The algorithm doesn't involve padding. */
4120 ret = 0;
4121 break;
4122 }
4123 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004124 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004125#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4126
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004127 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004128 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004129 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4130 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004131 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004132 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004133 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004134#if defined(MBEDTLS_CHACHA20_C)
4135 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004136 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004137 operation->iv_size = 12;
4138#endif
mohammad1603503973b2018-03-12 15:59:30 +02004139
Steven Cooreman7df02922020-09-09 15:28:49 +02004140 status = PSA_SUCCESS;
4141
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004142exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004143 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004144 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004145 if( status == PSA_SUCCESS )
4146 {
4147 /* Update operation flags for both driver and software implementations */
4148 operation->key_set = 1;
4149 }
4150 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004151 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004152
Ronald Cron5c522922020-11-14 16:35:34 +01004153 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004154
Ronald Cron5c522922020-11-14 16:35:34 +01004155 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004156}
4157
Gilles Peskinefe119512018-07-08 21:39:34 +02004158psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004159 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004160 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004161{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004162 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004163}
4164
Gilles Peskinefe119512018-07-08 21:39:34 +02004165psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004166 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004167 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004168{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004169 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004170}
4171
Gilles Peskinefe119512018-07-08 21:39:34 +02004172psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004173 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004174 size_t iv_size,
4175 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004176{
itayzafrir534bd7c2018-08-02 13:56:32 +03004177 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004179 if( operation->iv_set || ! operation->iv_required )
4180 {
4181 return( PSA_ERROR_BAD_STATE );
4182 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004183
Steven Cooremanef8575e2020-09-11 11:44:50 +02004184 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004185 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004186 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004187 iv,
4188 iv_size,
4189 iv_length );
4190 goto exit;
4191 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004192
Moran Peker41deec42018-04-04 15:43:05 +03004193 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004194 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004195 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004196 goto exit;
4197 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004198 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004199 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004200 if( ret != 0 )
4201 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004202 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004203 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004204 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004205
mohammad16038481e742018-03-18 13:57:31 +02004206 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004207 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004208
Moran Peker395db872018-05-31 14:07:14 +03004209exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004210 if( status == PSA_SUCCESS )
4211 operation->iv_set = 1;
4212 else
Moran Peker395db872018-05-31 14:07:14 +03004213 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004214 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004215}
4216
Gilles Peskinefe119512018-07-08 21:39:34 +02004217psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004218 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004219 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004220{
itayzafrir534bd7c2018-08-02 13:56:32 +03004221 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004223 if( operation->iv_set || ! operation->iv_required )
4224 {
4225 return( PSA_ERROR_BAD_STATE );
4226 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004227
Steven Cooremanef8575e2020-09-11 11:44:50 +02004228 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004229 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004230 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004231 iv,
4232 iv_length );
4233 goto exit;
4234 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004235
Moran Pekera28258c2018-05-29 16:25:04 +03004236 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004237 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004238 status = PSA_ERROR_INVALID_ARGUMENT;
4239 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004240 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004241 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4242 status = mbedtls_to_psa_error( ret );
4243exit:
4244 if( status == PSA_SUCCESS )
4245 operation->iv_set = 1;
4246 else
mohammad1603503973b2018-03-12 15:59:30 +02004247 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004248 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004249}
4250
Steven Cooreman177deba2020-09-07 17:14:14 +02004251/* Process input for which the algorithm is set to ECB mode. This requires
4252 * manual processing, since the PSA API is defined as being able to process
4253 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4254 * underlying mbedtls_cipher_update only takes full blocks. */
4255static psa_status_t psa_cipher_update_ecb_internal(
4256 mbedtls_cipher_context_t *ctx,
4257 const uint8_t *input,
4258 size_t input_length,
4259 uint8_t *output,
4260 size_t output_size,
4261 size_t *output_length )
4262{
4263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4264 size_t block_size = ctx->cipher_info->block_size;
4265 size_t internal_output_length = 0;
4266 *output_length = 0;
4267
4268 if( input_length == 0 )
4269 {
4270 status = PSA_SUCCESS;
4271 goto exit;
4272 }
4273
4274 if( ctx->unprocessed_len > 0 )
4275 {
4276 /* Fill up to block size, and run the block if there's a full one. */
4277 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4278
4279 if( input_length < bytes_to_copy )
4280 bytes_to_copy = input_length;
4281
4282 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4283 input, bytes_to_copy );
4284 input_length -= bytes_to_copy;
4285 input += bytes_to_copy;
4286 ctx->unprocessed_len += bytes_to_copy;
4287
4288 if( ctx->unprocessed_len == block_size )
4289 {
4290 status = mbedtls_to_psa_error(
4291 mbedtls_cipher_update( ctx,
4292 ctx->unprocessed_data,
4293 block_size,
4294 output, &internal_output_length ) );
4295
4296 if( status != PSA_SUCCESS )
4297 goto exit;
4298
4299 output += internal_output_length;
4300 output_size -= internal_output_length;
4301 *output_length += internal_output_length;
4302 ctx->unprocessed_len = 0;
4303 }
4304 }
4305
4306 while( input_length >= block_size )
4307 {
4308 /* Run all full blocks we have, one by one */
4309 status = mbedtls_to_psa_error(
4310 mbedtls_cipher_update( ctx, input,
4311 block_size,
4312 output, &internal_output_length ) );
4313
4314 if( status != PSA_SUCCESS )
4315 goto exit;
4316
4317 input_length -= block_size;
4318 input += block_size;
4319
4320 output += internal_output_length;
4321 output_size -= internal_output_length;
4322 *output_length += internal_output_length;
4323 }
4324
4325 if( input_length > 0 )
4326 {
4327 /* Save unprocessed bytes for later processing */
4328 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4329 input, input_length );
4330 ctx->unprocessed_len += input_length;
4331 }
4332
4333 status = PSA_SUCCESS;
4334
4335exit:
4336 return( status );
4337}
4338
Gilles Peskinee553c652018-06-04 16:22:46 +02004339psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4340 const uint8_t *input,
4341 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004342 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004343 size_t output_size,
4344 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004345{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004347 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004348 if( operation->alg == 0 )
4349 {
4350 return( PSA_ERROR_BAD_STATE );
4351 }
4352 if( operation->iv_required && ! operation->iv_set )
4353 {
4354 return( PSA_ERROR_BAD_STATE );
4355 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004356
Steven Cooremanef8575e2020-09-11 11:44:50 +02004357 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004358 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004359 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004360 input,
4361 input_length,
4362 output,
4363 output_size,
4364 output_length );
4365 goto exit;
4366 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004367
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004368 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004369 {
4370 /* Take the unprocessed partial block left over from previous
4371 * update calls, if any, plus the input to this call. Remove
4372 * the last partial block, if any. You get the data that will be
4373 * output in this call. */
4374 expected_output_size =
4375 ( operation->ctx.cipher.unprocessed_len + input_length )
4376 / operation->block_size * operation->block_size;
4377 }
4378 else
4379 {
4380 expected_output_size = input_length;
4381 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004382
Gilles Peskine89d789c2018-06-04 17:17:16 +02004383 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004384 {
4385 status = PSA_ERROR_BUFFER_TOO_SMALL;
4386 goto exit;
4387 }
mohammad160382759612018-03-12 18:16:40 +02004388
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004389 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4390 {
4391 /* mbedtls_cipher_update has an API inconsistency: it will only
4392 * process a single block at a time in ECB mode. Abstract away that
4393 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004394 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4395 input,
4396 input_length,
4397 output,
4398 output_size,
4399 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004400 }
4401 else
4402 {
4403 status = mbedtls_to_psa_error(
4404 mbedtls_cipher_update( &operation->ctx.cipher, input,
4405 input_length, output, output_length ) );
4406 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004407exit:
4408 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004409 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004410 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004411}
4412
Gilles Peskinee553c652018-06-04 16:22:46 +02004413psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4414 uint8_t *output,
4415 size_t output_size,
4416 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004417{
David Saadab4ecc272019-02-14 13:48:10 +02004418 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004419 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004420 if( operation->alg == 0 )
4421 {
4422 return( PSA_ERROR_BAD_STATE );
4423 }
4424 if( operation->iv_required && ! operation->iv_set )
4425 {
4426 return( PSA_ERROR_BAD_STATE );
4427 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004428
Steven Cooremanef8575e2020-09-11 11:44:50 +02004429 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004430 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004431 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004432 output,
4433 output_size,
4434 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004435 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004436 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004437
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004438 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004439 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004440 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004441 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004442 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004443 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004444 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004445 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004446 }
4447
Steven Cooremanef8575e2020-09-11 11:44:50 +02004448 status = mbedtls_to_psa_error(
4449 mbedtls_cipher_finish( &operation->ctx.cipher,
4450 temp_output_buffer,
4451 output_length ) );
4452 if( status != PSA_SUCCESS )
4453 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004454
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004455 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004456 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004457 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004458 memcpy( output, temp_output_buffer, *output_length );
4459 else
Janos Follath315b51c2018-07-09 16:04:51 +01004460 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004461
Steven Cooremanef8575e2020-09-11 11:44:50 +02004462exit:
4463 if( operation->mbedtls_in_use == 1 )
4464 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004465
Steven Cooremanef8575e2020-09-11 11:44:50 +02004466 if( status == PSA_SUCCESS )
4467 return( psa_cipher_abort( operation ) );
4468 else
4469 {
4470 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004471 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004472
Steven Cooremanef8575e2020-09-11 11:44:50 +02004473 return( status );
4474 }
mohammad1603503973b2018-03-12 15:59:30 +02004475}
4476
Gilles Peskinee553c652018-06-04 16:22:46 +02004477psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4478{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004479 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004480 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004481 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004482 * in use. It's ok to call abort on such an object, and there's
4483 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004484 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004485 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004486
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004487 /* Sanity check (shouldn't happen: operation->alg should
4488 * always have been initialized to a valid value). */
4489 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4490 return( PSA_ERROR_BAD_STATE );
4491
Steven Cooremanef8575e2020-09-11 11:44:50 +02004492 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004493 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004494 else
4495 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004496
Moran Peker41deec42018-04-04 15:43:05 +03004497 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004498 operation->key_set = 0;
4499 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004500 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004501 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004502 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004503 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004504
Moran Peker395db872018-05-31 14:07:14 +03004505 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004506}
4507
Gilles Peskinea0655c32018-04-30 17:06:50 +02004508
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004509
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004510
mohammad16035955c982018-04-26 00:53:03 +03004511/****************************************************************/
4512/* AEAD */
4513/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004514
Gilles Peskineedf9a652018-08-17 18:11:56 +02004515typedef struct
4516{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004517 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004518 const mbedtls_cipher_info_t *cipher_info;
4519 union
4520 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004521 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004522#if defined(MBEDTLS_CCM_C)
4523 mbedtls_ccm_context ccm;
4524#endif /* MBEDTLS_CCM_C */
4525#if defined(MBEDTLS_GCM_C)
4526 mbedtls_gcm_context gcm;
4527#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004528#if defined(MBEDTLS_CHACHAPOLY_C)
4529 mbedtls_chachapoly_context chachapoly;
4530#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004531 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004532 psa_algorithm_t core_alg;
4533 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004534 uint8_t tag_length;
4535} aead_operation_t;
4536
Ronald Cronf95a2b12020-10-22 15:24:49 +02004537#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4538
Gilles Peskine30a9e412019-01-14 18:36:12 +01004539static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004540{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004541 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004542 {
4543#if defined(MBEDTLS_CCM_C)
4544 case PSA_ALG_CCM:
4545 mbedtls_ccm_free( &operation->ctx.ccm );
4546 break;
4547#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004548#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004549 case PSA_ALG_GCM:
4550 mbedtls_gcm_free( &operation->ctx.gcm );
4551 break;
4552#endif /* MBEDTLS_GCM_C */
4553 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004554
Ronald Cron5c522922020-11-14 16:35:34 +01004555 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004556}
4557
4558static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004559 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004560 psa_key_usage_t usage,
4561 psa_algorithm_t alg )
4562{
4563 psa_status_t status;
4564 size_t key_bits;
4565 mbedtls_cipher_id_t cipher_id;
4566
Ronald Cron5c522922020-11-14 16:35:34 +01004567 status = psa_get_and_lock_transparent_key_slot_with_policy(
4568 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004569 if( status != PSA_SUCCESS )
4570 return( status );
4571
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004572 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004573
4574 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004575 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004576 &cipher_id );
4577 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004578 {
4579 status = PSA_ERROR_NOT_SUPPORTED;
4580 goto cleanup;
4581 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004582
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004583 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004584 {
4585#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004586 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4587 operation->core_alg = PSA_ALG_CCM;
4588 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004589 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4590 * The call to mbedtls_ccm_encrypt_and_tag or
4591 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004592 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004593 {
4594 status = PSA_ERROR_INVALID_ARGUMENT;
4595 goto cleanup;
4596 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004597 mbedtls_ccm_init( &operation->ctx.ccm );
4598 status = mbedtls_to_psa_error(
4599 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004600 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004601 (unsigned int) key_bits ) );
4602 if( status != 0 )
4603 goto cleanup;
4604 break;
4605#endif /* MBEDTLS_CCM_C */
4606
4607#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004608 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4609 operation->core_alg = PSA_ALG_GCM;
4610 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004611 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4612 * The call to mbedtls_gcm_crypt_and_tag or
4613 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004614 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004615 {
4616 status = PSA_ERROR_INVALID_ARGUMENT;
4617 goto cleanup;
4618 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004619 mbedtls_gcm_init( &operation->ctx.gcm );
4620 status = mbedtls_to_psa_error(
4621 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004622 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004623 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004624 if( status != 0 )
4625 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004626 break;
4627#endif /* MBEDTLS_GCM_C */
4628
Gilles Peskine26869f22019-05-06 15:25:00 +02004629#if defined(MBEDTLS_CHACHAPOLY_C)
4630 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4631 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4632 operation->full_tag_length = 16;
4633 /* We only support the default tag length. */
4634 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004635 {
4636 status = PSA_ERROR_NOT_SUPPORTED;
4637 goto cleanup;
4638 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004639 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4640 status = mbedtls_to_psa_error(
4641 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004642 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004643 if( status != 0 )
4644 goto cleanup;
4645 break;
4646#endif /* MBEDTLS_CHACHAPOLY_C */
4647
Gilles Peskineedf9a652018-08-17 18:11:56 +02004648 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004649 status = PSA_ERROR_NOT_SUPPORTED;
4650 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004651 }
4652
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004653 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4654 {
4655 status = PSA_ERROR_INVALID_ARGUMENT;
4656 goto cleanup;
4657 }
4658 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004659
Gilles Peskineedf9a652018-08-17 18:11:56 +02004660 return( PSA_SUCCESS );
4661
4662cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004663 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004664 return( status );
4665}
4666
Ronald Croncf56a0a2020-08-04 09:51:30 +02004667psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004668 psa_algorithm_t alg,
4669 const uint8_t *nonce,
4670 size_t nonce_length,
4671 const uint8_t *additional_data,
4672 size_t additional_data_length,
4673 const uint8_t *plaintext,
4674 size_t plaintext_length,
4675 uint8_t *ciphertext,
4676 size_t ciphertext_size,
4677 size_t *ciphertext_length )
4678{
mohammad16035955c982018-04-26 00:53:03 +03004679 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004680 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004681 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004682
mohammad1603f08a5502018-06-03 15:05:47 +03004683 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004684
Ronald Croncf56a0a2020-08-04 09:51:30 +02004685 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004686 if( status != PSA_SUCCESS )
4687 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004688
Gilles Peskineedf9a652018-08-17 18:11:56 +02004689 /* For all currently supported modes, the tag is at the end of the
4690 * ciphertext. */
4691 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4692 {
4693 status = PSA_ERROR_BUFFER_TOO_SMALL;
4694 goto exit;
4695 }
4696 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004697
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004698#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004699 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004700 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004701 status = mbedtls_to_psa_error(
4702 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4703 MBEDTLS_GCM_ENCRYPT,
4704 plaintext_length,
4705 nonce, nonce_length,
4706 additional_data, additional_data_length,
4707 plaintext, ciphertext,
4708 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004709 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004710 else
4711#endif /* MBEDTLS_GCM_C */
4712#if defined(MBEDTLS_CCM_C)
4713 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004714 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004715 status = mbedtls_to_psa_error(
4716 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4717 plaintext_length,
4718 nonce, nonce_length,
4719 additional_data,
4720 additional_data_length,
4721 plaintext, ciphertext,
4722 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004723 }
mohammad16035c8845f2018-05-09 05:40:09 -07004724 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004725#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004726#if defined(MBEDTLS_CHACHAPOLY_C)
4727 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4728 {
4729 if( nonce_length != 12 || operation.tag_length != 16 )
4730 {
4731 status = PSA_ERROR_NOT_SUPPORTED;
4732 goto exit;
4733 }
4734 status = mbedtls_to_psa_error(
4735 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4736 plaintext_length,
4737 nonce,
4738 additional_data,
4739 additional_data_length,
4740 plaintext,
4741 ciphertext,
4742 tag ) );
4743 }
4744 else
4745#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004746 {
mohammad1603554faad2018-06-03 15:07:38 +03004747 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004748 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004749
Gilles Peskineedf9a652018-08-17 18:11:56 +02004750 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4751 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004752
Gilles Peskineedf9a652018-08-17 18:11:56 +02004753exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004754 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004755 if( status == PSA_SUCCESS )
4756 *ciphertext_length = plaintext_length + operation.tag_length;
4757 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004758}
4759
Gilles Peskineee652a32018-06-01 19:23:52 +02004760/* Locate the tag in a ciphertext buffer containing the encrypted data
4761 * followed by the tag. Return the length of the part preceding the tag in
4762 * *plaintext_length. This is the size of the plaintext in modes where
4763 * the encrypted data has the same size as the plaintext, such as
4764 * CCM and GCM. */
4765static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4766 const uint8_t *ciphertext,
4767 size_t ciphertext_length,
4768 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004769 const uint8_t **p_tag )
4770{
4771 size_t payload_length;
4772 if( tag_length > ciphertext_length )
4773 return( PSA_ERROR_INVALID_ARGUMENT );
4774 payload_length = ciphertext_length - tag_length;
4775 if( payload_length > plaintext_size )
4776 return( PSA_ERROR_BUFFER_TOO_SMALL );
4777 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004778 return( PSA_SUCCESS );
4779}
4780
Ronald Croncf56a0a2020-08-04 09:51:30 +02004781psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004782 psa_algorithm_t alg,
4783 const uint8_t *nonce,
4784 size_t nonce_length,
4785 const uint8_t *additional_data,
4786 size_t additional_data_length,
4787 const uint8_t *ciphertext,
4788 size_t ciphertext_length,
4789 uint8_t *plaintext,
4790 size_t plaintext_size,
4791 size_t *plaintext_length )
4792{
mohammad16035955c982018-04-26 00:53:03 +03004793 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004794 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004795 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004796
Gilles Peskineee652a32018-06-01 19:23:52 +02004797 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004798
Ronald Croncf56a0a2020-08-04 09:51:30 +02004799 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004800 if( status != PSA_SUCCESS )
4801 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004802
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004803 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4804 ciphertext, ciphertext_length,
4805 plaintext_size, &tag );
4806 if( status != PSA_SUCCESS )
4807 goto exit;
4808
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004809#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004810 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004811 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004812 status = mbedtls_to_psa_error(
4813 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4814 ciphertext_length - operation.tag_length,
4815 nonce, nonce_length,
4816 additional_data,
4817 additional_data_length,
4818 tag, operation.tag_length,
4819 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004820 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004821 else
4822#endif /* MBEDTLS_GCM_C */
4823#if defined(MBEDTLS_CCM_C)
4824 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004825 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004826 status = mbedtls_to_psa_error(
4827 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4828 ciphertext_length - operation.tag_length,
4829 nonce, nonce_length,
4830 additional_data,
4831 additional_data_length,
4832 ciphertext, plaintext,
4833 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004834 }
mohammad160339574652018-06-01 04:39:53 -07004835 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004836#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004837#if defined(MBEDTLS_CHACHAPOLY_C)
4838 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4839 {
4840 if( nonce_length != 12 || operation.tag_length != 16 )
4841 {
4842 status = PSA_ERROR_NOT_SUPPORTED;
4843 goto exit;
4844 }
4845 status = mbedtls_to_psa_error(
4846 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4847 ciphertext_length - operation.tag_length,
4848 nonce,
4849 additional_data,
4850 additional_data_length,
4851 tag,
4852 ciphertext,
4853 plaintext ) );
4854 }
4855 else
4856#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004857 {
mohammad1603554faad2018-06-03 15:07:38 +03004858 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004859 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004860
Gilles Peskineedf9a652018-08-17 18:11:56 +02004861 if( status != PSA_SUCCESS && plaintext_size != 0 )
4862 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004863
Gilles Peskineedf9a652018-08-17 18:11:56 +02004864exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004865 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004866 if( status == PSA_SUCCESS )
4867 *plaintext_length = ciphertext_length - operation.tag_length;
4868 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004869}
4870
Gilles Peskinea0655c32018-04-30 17:06:50 +02004871
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004872
Gilles Peskine20035e32018-02-03 22:44:14 +01004873/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004874/* Generators */
4875/****************************************************************/
4876
John Durkop07cc04a2020-11-16 22:08:34 -08004877#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4878 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4879 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4880#define AT_LEAST_ONE_BUILTIN_KDF
4881#endif
4882
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004883#define HKDF_STATE_INIT 0 /* no input yet */
4884#define HKDF_STATE_STARTED 1 /* got salt */
4885#define HKDF_STATE_KEYED 2 /* got key */
4886#define HKDF_STATE_OUTPUT 3 /* output started */
4887
Gilles Peskinecbe66502019-05-16 16:59:18 +02004888static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004889 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004891 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4892 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004893 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004895}
4896
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004898{
4899 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004900 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004901 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004902 {
4903 /* The object has (apparently) been initialized but it is not
4904 * in use. It's ok to call abort on such an object, and there's
4905 * nothing to do. */
4906 }
4907 else
John Durkopd0321952020-10-29 21:37:36 -07004908#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004909 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004910 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 mbedtls_free( operation->ctx.hkdf.info );
4912 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004913 }
John Durkop07cc04a2020-11-16 22:08:34 -08004914 else
4915#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4916#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4917 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4918 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004920 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004921 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004922 if( operation->ctx.tls12_prf.seed != NULL )
4923 {
4924 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4925 operation->ctx.tls12_prf.seed_length );
4926 mbedtls_free( operation->ctx.tls12_prf.seed );
4927 }
4928
4929 if( operation->ctx.tls12_prf.label != NULL )
4930 {
4931 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4932 operation->ctx.tls12_prf.label_length );
4933 mbedtls_free( operation->ctx.tls12_prf.label );
4934 }
4935
4936 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4937
4938 /* We leave the fields Ai and output_block to be erased safely by the
4939 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004940 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004941 else
John Durkop07cc04a2020-11-16 22:08:34 -08004942#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4943 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004944 {
4945 status = PSA_ERROR_BAD_STATE;
4946 }
Janos Follath083036a2019-06-11 10:22:26 +01004947 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004948 return( status );
4949}
4950
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004951psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004952 size_t *capacity)
4953{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004955 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004957 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004958 }
4959
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004961 return( PSA_SUCCESS );
4962}
4963
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004965 size_t capacity )
4966{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004968 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004969 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004970 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004972 return( PSA_SUCCESS );
4973}
4974
John Durkopd0321952020-10-29 21:37:36 -07004975#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004977 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004978static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004979 psa_algorithm_t hash_alg,
4980 uint8_t *output,
4981 size_t output_length )
4982{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004983 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004984 psa_status_t status;
4985
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004986 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4987 return( PSA_ERROR_BAD_STATE );
4988 hkdf->state = HKDF_STATE_OUTPUT;
4989
Gilles Peskinebef7f142018-07-12 17:22:21 +02004990 while( output_length != 0 )
4991 {
4992 /* Copy what remains of the current block */
4993 uint8_t n = hash_length - hkdf->offset_in_block;
4994 if( n > output_length )
4995 n = (uint8_t) output_length;
4996 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4997 output += n;
4998 output_length -= n;
4999 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005000 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005001 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005002 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005003 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005004 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005005 * object was corrupted or if this function is called directly
5006 * inside the library. */
5007 if( hkdf->block_number == 0xff )
5008 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005009
5010 /* We need a new block */
5011 ++hkdf->block_number;
5012 hkdf->offset_in_block = 0;
5013 status = psa_hmac_setup_internal( &hkdf->hmac,
5014 hkdf->prk, hash_length,
5015 hash_alg );
5016 if( status != PSA_SUCCESS )
5017 return( status );
5018 if( hkdf->block_number != 1 )
5019 {
5020 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5021 hkdf->output_block,
5022 hash_length );
5023 if( status != PSA_SUCCESS )
5024 return( status );
5025 }
5026 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5027 hkdf->info,
5028 hkdf->info_length );
5029 if( status != PSA_SUCCESS )
5030 return( status );
5031 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5032 &hkdf->block_number, 1 );
5033 if( status != PSA_SUCCESS )
5034 return( status );
5035 status = psa_hmac_finish_internal( &hkdf->hmac,
5036 hkdf->output_block,
5037 sizeof( hkdf->output_block ) );
5038 if( status != PSA_SUCCESS )
5039 return( status );
5040 }
5041
5042 return( PSA_SUCCESS );
5043}
John Durkop07cc04a2020-11-16 22:08:34 -08005044#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005045
John Durkop07cc04a2020-11-16 22:08:34 -08005046#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5047 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005048static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5049 psa_tls12_prf_key_derivation_t *tls12_prf,
5050 psa_algorithm_t alg )
5051{
5052 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005053 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005054 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5055 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005056
Janos Follath7742fee2019-06-17 12:58:10 +01005057 /* We can't be wanting more output after block 0xff, otherwise
5058 * the capacity check in psa_key_derivation_output_bytes() would have
5059 * prevented this call. It could happen only if the operation
5060 * object was corrupted or if this function is called directly
5061 * inside the library. */
5062 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005063 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005064
5065 /* We need a new block */
5066 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005067 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005068
5069 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5070 *
5071 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5072 *
5073 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5074 * HMAC_hash(secret, A(2) + seed) +
5075 * HMAC_hash(secret, A(3) + seed) + ...
5076 *
5077 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005078 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005079 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005080 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005081 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005082 * is currently extracted as `output_block` and where i is
5083 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005084 */
5085
Janos Follathea29bfb2019-06-19 12:21:20 +01005086 /* Save the hash context before using it, to preserve the hash state with
5087 * only the inner padding in it. We need this, because inner padding depends
5088 * on the key (secret in the RFC's terminology). */
5089 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5090 if( status != PSA_SUCCESS )
5091 goto cleanup;
5092
5093 /* Calculate A(i) where i = tls12_prf->block_number. */
5094 if( tls12_prf->block_number == 1 )
5095 {
5096 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5097 * the variable seed and in this instance means it in the context of the
5098 * P_hash function, where seed = label + seed.) */
5099 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5100 tls12_prf->label, tls12_prf->label_length );
5101 if( status != PSA_SUCCESS )
5102 goto cleanup;
5103 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5104 tls12_prf->seed, tls12_prf->seed_length );
5105 if( status != PSA_SUCCESS )
5106 goto cleanup;
5107 }
5108 else
5109 {
5110 /* A(i) = HMAC_hash(secret, A(i-1)) */
5111 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5112 tls12_prf->Ai, hash_length );
5113 if( status != PSA_SUCCESS )
5114 goto cleanup;
5115 }
5116
5117 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5118 tls12_prf->Ai, hash_length );
5119 if( status != PSA_SUCCESS )
5120 goto cleanup;
5121 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5122 if( status != PSA_SUCCESS )
5123 goto cleanup;
5124
5125 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5126 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5127 tls12_prf->Ai, hash_length );
5128 if( status != PSA_SUCCESS )
5129 goto cleanup;
5130 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5131 tls12_prf->label, tls12_prf->label_length );
5132 if( status != PSA_SUCCESS )
5133 goto cleanup;
5134 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5135 tls12_prf->seed, tls12_prf->seed_length );
5136 if( status != PSA_SUCCESS )
5137 goto cleanup;
5138 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5139 tls12_prf->output_block, hash_length );
5140 if( status != PSA_SUCCESS )
5141 goto cleanup;
5142 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5143 if( status != PSA_SUCCESS )
5144 goto cleanup;
5145
Janos Follath7742fee2019-06-17 12:58:10 +01005146
5147cleanup:
5148
Janos Follathea29bfb2019-06-19 12:21:20 +01005149 cleanup_status = psa_hash_abort( &backup );
5150 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5151 status = cleanup_status;
5152
5153 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005154}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005155
Janos Follath844eb0e2019-06-19 12:10:49 +01005156static psa_status_t psa_key_derivation_tls12_prf_read(
5157 psa_tls12_prf_key_derivation_t *tls12_prf,
5158 psa_algorithm_t alg,
5159 uint8_t *output,
5160 size_t output_length )
5161{
5162 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005163 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005164 psa_status_t status;
5165 uint8_t offset, length;
5166
5167 while( output_length != 0 )
5168 {
5169 /* Check if we have fully processed the current block. */
5170 if( tls12_prf->left_in_block == 0 )
5171 {
5172 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5173 alg );
5174 if( status != PSA_SUCCESS )
5175 return( status );
5176
5177 continue;
5178 }
5179
5180 if( tls12_prf->left_in_block > output_length )
5181 length = (uint8_t) output_length;
5182 else
5183 length = tls12_prf->left_in_block;
5184
5185 offset = hash_length - tls12_prf->left_in_block;
5186 memcpy( output, tls12_prf->output_block + offset, length );
5187 output += length;
5188 output_length -= length;
5189 tls12_prf->left_in_block -= length;
5190 }
5191
5192 return( PSA_SUCCESS );
5193}
John Durkop07cc04a2020-11-16 22:08:34 -08005194#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5195 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005196
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005197psa_status_t psa_key_derivation_output_bytes(
5198 psa_key_derivation_operation_t *operation,
5199 uint8_t *output,
5200 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005201{
5202 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005204
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005205 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005206 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005208 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005209 }
5210
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005212 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005214 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005216 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005217 goto exit;
5218 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005220 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005222 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005223 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5224 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005226 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005227 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005228 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005229 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005230
John Durkopd0321952020-10-29 21:37:36 -07005231#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005232 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005233 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005234 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005235 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005236 output, output_length );
5237 }
Janos Follathadbec812019-06-14 11:05:39 +01005238 else
John Durkop07cc04a2020-11-16 22:08:34 -08005239#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5240#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5241 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005242 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005243 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005244 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005246 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005247 output_length );
5248 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005249 else
John Durkop07cc04a2020-11-16 22:08:34 -08005250#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5251 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005252 {
5253 return( PSA_ERROR_BAD_STATE );
5254 }
5255
5256exit:
5257 if( status != PSA_SUCCESS )
5258 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005259 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 * This allows us to differentiate between exhausted operations and
5261 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5262 * operations. */
5263 psa_algorithm_t alg = operation->alg;
5264 psa_key_derivation_abort( operation );
5265 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005266 memset( output, '!', output_length );
5267 }
5268 return( status );
5269}
5270
Gilles Peskine08542d82018-07-19 17:05:42 +02005271#if defined(MBEDTLS_DES_C)
5272static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5273{
5274 if( data_size >= 8 )
5275 mbedtls_des_key_set_parity( data );
5276 if( data_size >= 16 )
5277 mbedtls_des_key_set_parity( data + 8 );
5278 if( data_size >= 24 )
5279 mbedtls_des_key_set_parity( data + 16 );
5280}
5281#endif /* MBEDTLS_DES_C */
5282
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005283static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005284 psa_key_slot_t *slot,
5285 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005287{
5288 uint8_t *data = NULL;
5289 size_t bytes = PSA_BITS_TO_BYTES( bits );
5290 psa_status_t status;
5291
Gilles Peskine8e338702019-07-30 20:06:31 +02005292 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005293 return( PSA_ERROR_INVALID_ARGUMENT );
5294 if( bits % 8 != 0 )
5295 return( PSA_ERROR_INVALID_ARGUMENT );
5296 data = mbedtls_calloc( 1, bytes );
5297 if( data == NULL )
5298 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005301 if( status != PSA_SUCCESS )
5302 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005303#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005304 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005305 psa_des_set_key_parity( data, bytes );
5306#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005307 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005308
5309exit:
5310 mbedtls_free( data );
5311 return( status );
5312}
5313
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005314psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005315 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005316 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005317{
5318 psa_status_t status;
5319 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005320 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005321
Ronald Cron81709fc2020-11-14 12:10:32 +01005322 *key = MBEDTLS_SVC_KEY_ID_INIT;
5323
Gilles Peskine0f84d622019-09-12 19:03:13 +02005324 /* Reject any attempt to create a zero-length key so that we don't
5325 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5326 if( psa_get_key_bits( attributes ) == 0 )
5327 return( PSA_ERROR_INVALID_ARGUMENT );
5328
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005329 if( ! operation->can_output_key )
5330 return( PSA_ERROR_NOT_PERMITTED );
5331
Ronald Cron81709fc2020-11-14 12:10:32 +01005332 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5333 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005334#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5335 if( driver != NULL )
5336 {
5337 /* Deriving a key in a secure element is not implemented yet. */
5338 status = PSA_ERROR_NOT_SUPPORTED;
5339 }
5340#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005341 if( status == PSA_SUCCESS )
5342 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005343 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005344 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005346 }
5347 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005348 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005349 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005350 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005351
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005352 return( status );
5353}
5354
Gilles Peskineeab56e42018-07-12 17:12:33 +02005355
5356
5357/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005358/* Key derivation */
5359/****************************************************************/
5360
John Durkop07cc04a2020-11-16 22:08:34 -08005361#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005362static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005363 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005364 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005365{
John Durkop07cc04a2020-11-16 22:08:34 -08005366 int is_kdf_alg_supported;
5367
Janos Follath5fe19732019-06-20 15:09:30 +01005368 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5369 * initialisers for this union leave some bytes unspecified.) */
5370 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5371
Gilles Peskine969c5d62019-01-16 15:53:06 +01005372 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005373#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005374 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5375 is_kdf_alg_supported = 1;
5376 else
5377#endif
5378#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5379 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5380 is_kdf_alg_supported = 1;
5381 else
5382#endif
5383#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5384 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5385 is_kdf_alg_supported = 1;
5386 else
5387#endif
5388 is_kdf_alg_supported = 0;
5389
5390 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005391 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005392 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005393 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005394 if( hash_size == 0 )
5395 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005396 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5397 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005398 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005399 {
5400 return( PSA_ERROR_NOT_SUPPORTED );
5401 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005402 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005403 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005404 }
John Durkop07cc04a2020-11-16 22:08:34 -08005405
5406 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005407}
John Durkop07cc04a2020-11-16 22:08:34 -08005408#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005409
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005410psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005411 psa_algorithm_t alg )
5412{
5413 psa_status_t status;
5414
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005415 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005416 return( PSA_ERROR_BAD_STATE );
5417
Gilles Peskine6843c292019-01-18 16:44:49 +01005418 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5419 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005420#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005421 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005422 {
5423 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005424 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005425 }
5426 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5427 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005428 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005429 }
John Durkop07cc04a2020-11-16 22:08:34 -08005430#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005431 else
5432 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005433
5434 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005435 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005436 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005437}
5438
John Durkopd0321952020-10-29 21:37:36 -07005439#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005440static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005441 psa_algorithm_t hash_alg,
5442 psa_key_derivation_step_t step,
5443 const uint8_t *data,
5444 size_t data_length )
5445{
5446 psa_status_t status;
5447 switch( step )
5448 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005449 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005450 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005451 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005452 status = psa_hmac_setup_internal( &hkdf->hmac,
5453 data, data_length,
5454 hash_alg );
5455 if( status != PSA_SUCCESS )
5456 return( status );
5457 hkdf->state = HKDF_STATE_STARTED;
5458 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005459 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005460 /* If no salt was provided, use an empty salt. */
5461 if( hkdf->state == HKDF_STATE_INIT )
5462 {
5463 status = psa_hmac_setup_internal( &hkdf->hmac,
5464 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005465 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005466 if( status != PSA_SUCCESS )
5467 return( status );
5468 hkdf->state = HKDF_STATE_STARTED;
5469 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005470 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005471 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005472 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5473 data, data_length );
5474 if( status != PSA_SUCCESS )
5475 return( status );
5476 status = psa_hmac_finish_internal( &hkdf->hmac,
5477 hkdf->prk,
5478 sizeof( hkdf->prk ) );
5479 if( status != PSA_SUCCESS )
5480 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005481 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005482 hkdf->block_number = 0;
5483 hkdf->state = HKDF_STATE_KEYED;
5484 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005485 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005486 if( hkdf->state == HKDF_STATE_OUTPUT )
5487 return( PSA_ERROR_BAD_STATE );
5488 if( hkdf->info_set )
5489 return( PSA_ERROR_BAD_STATE );
5490 hkdf->info_length = data_length;
5491 if( data_length != 0 )
5492 {
5493 hkdf->info = mbedtls_calloc( 1, data_length );
5494 if( hkdf->info == NULL )
5495 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5496 memcpy( hkdf->info, data, data_length );
5497 }
5498 hkdf->info_set = 1;
5499 return( PSA_SUCCESS );
5500 default:
5501 return( PSA_ERROR_INVALID_ARGUMENT );
5502 }
5503}
John Durkop07cc04a2020-11-16 22:08:34 -08005504#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005505
John Durkop07cc04a2020-11-16 22:08:34 -08005506#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5507 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005508static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5509 const uint8_t *data,
5510 size_t data_length )
5511{
5512 if( prf->state != TLS12_PRF_STATE_INIT )
5513 return( PSA_ERROR_BAD_STATE );
5514
Janos Follathd6dce9f2019-07-04 09:11:38 +01005515 if( data_length != 0 )
5516 {
5517 prf->seed = mbedtls_calloc( 1, data_length );
5518 if( prf->seed == NULL )
5519 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005520
Janos Follathd6dce9f2019-07-04 09:11:38 +01005521 memcpy( prf->seed, data, data_length );
5522 prf->seed_length = data_length;
5523 }
Janos Follathf08e2652019-06-13 09:05:41 +01005524
5525 prf->state = TLS12_PRF_STATE_SEED_SET;
5526
5527 return( PSA_SUCCESS );
5528}
5529
Janos Follath81550542019-06-13 14:26:34 +01005530static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5531 psa_algorithm_t hash_alg,
5532 const uint8_t *data,
5533 size_t data_length )
5534{
5535 psa_status_t status;
5536 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5537 return( PSA_ERROR_BAD_STATE );
5538
5539 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5540 if( status != PSA_SUCCESS )
5541 return( status );
5542
5543 prf->state = TLS12_PRF_STATE_KEY_SET;
5544
5545 return( PSA_SUCCESS );
5546}
5547
Janos Follath63028dd2019-06-13 09:15:47 +01005548static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5549 const uint8_t *data,
5550 size_t data_length )
5551{
5552 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5553 return( PSA_ERROR_BAD_STATE );
5554
Janos Follathd6dce9f2019-07-04 09:11:38 +01005555 if( data_length != 0 )
5556 {
5557 prf->label = mbedtls_calloc( 1, data_length );
5558 if( prf->label == NULL )
5559 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005560
Janos Follathd6dce9f2019-07-04 09:11:38 +01005561 memcpy( prf->label, data, data_length );
5562 prf->label_length = data_length;
5563 }
Janos Follath63028dd2019-06-13 09:15:47 +01005564
5565 prf->state = TLS12_PRF_STATE_LABEL_SET;
5566
5567 return( PSA_SUCCESS );
5568}
5569
Janos Follathb03233e2019-06-11 15:30:30 +01005570static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5571 psa_algorithm_t hash_alg,
5572 psa_key_derivation_step_t step,
5573 const uint8_t *data,
5574 size_t data_length )
5575{
Janos Follathb03233e2019-06-11 15:30:30 +01005576 switch( step )
5577 {
Janos Follathf08e2652019-06-13 09:05:41 +01005578 case PSA_KEY_DERIVATION_INPUT_SEED:
5579 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005580 case PSA_KEY_DERIVATION_INPUT_SECRET:
5581 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005582 case PSA_KEY_DERIVATION_INPUT_LABEL:
5583 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005584 default:
5585 return( PSA_ERROR_INVALID_ARGUMENT );
5586 }
5587}
John Durkop07cc04a2020-11-16 22:08:34 -08005588#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5589 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5590
5591#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5592static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5593 psa_tls12_prf_key_derivation_t *prf,
5594 psa_algorithm_t hash_alg,
5595 const uint8_t *data,
5596 size_t data_length )
5597{
5598 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005599 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005600 uint8_t *cur = pms;
5601
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005602 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005603 return( PSA_ERROR_INVALID_ARGUMENT );
5604
5605 /* Quoting RFC 4279, Section 2:
5606 *
5607 * The premaster secret is formed as follows: if the PSK is N octets
5608 * long, concatenate a uint16 with the value N, N zero octets, a second
5609 * uint16 with the value N, and the PSK itself.
5610 */
5611
5612 *cur++ = ( data_length >> 8 ) & 0xff;
5613 *cur++ = ( data_length >> 0 ) & 0xff;
5614 memset( cur, 0, data_length );
5615 cur += data_length;
5616 *cur++ = pms[0];
5617 *cur++ = pms[1];
5618 memcpy( cur, data, data_length );
5619 cur += data_length;
5620
5621 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5622
5623 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5624 return( status );
5625}
Janos Follath6660f0e2019-06-17 08:44:03 +01005626
5627static psa_status_t psa_tls12_prf_psk_to_ms_input(
5628 psa_tls12_prf_key_derivation_t *prf,
5629 psa_algorithm_t hash_alg,
5630 psa_key_derivation_step_t step,
5631 const uint8_t *data,
5632 size_t data_length )
5633{
5634 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005635 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005636 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5637 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005638 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005639
5640 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5641}
John Durkop07cc04a2020-11-16 22:08:34 -08005642#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005643
Gilles Peskineb8965192019-09-24 16:21:10 +02005644/** Check whether the given key type is acceptable for the given
5645 * input step of a key derivation.
5646 *
5647 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5648 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5649 * Both secret and non-secret inputs can alternatively have the type
5650 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5651 * that the input was passed as a buffer rather than via a key object.
5652 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005653static int psa_key_derivation_check_input_type(
5654 psa_key_derivation_step_t step,
5655 psa_key_type_t key_type )
5656{
5657 switch( step )
5658 {
5659 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005660 if( key_type == PSA_KEY_TYPE_DERIVE )
5661 return( PSA_SUCCESS );
5662 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005663 return( PSA_SUCCESS );
5664 break;
5665 case PSA_KEY_DERIVATION_INPUT_LABEL:
5666 case PSA_KEY_DERIVATION_INPUT_SALT:
5667 case PSA_KEY_DERIVATION_INPUT_INFO:
5668 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005669 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5670 return( PSA_SUCCESS );
5671 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005672 return( PSA_SUCCESS );
5673 break;
5674 }
5675 return( PSA_ERROR_INVALID_ARGUMENT );
5676}
5677
Janos Follathb80a94e2019-06-12 15:54:46 +01005678static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005679 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005680 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005681 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005682 const uint8_t *data,
5683 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005684{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005685 psa_status_t status;
5686 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5687
5688 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005689 if( status != PSA_SUCCESS )
5690 goto exit;
5691
John Durkopd0321952020-10-29 21:37:36 -07005692#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005693 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005694 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005695 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005696 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005697 step, data, data_length );
5698 }
John Durkop07cc04a2020-11-16 22:08:34 -08005699 else
5700#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5701#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5702 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005703 {
Janos Follathb03233e2019-06-11 15:30:30 +01005704 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5705 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5706 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005707 }
John Durkop07cc04a2020-11-16 22:08:34 -08005708 else
5709#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5710#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5711 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005712 {
5713 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5714 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5715 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005716 }
5717 else
John Durkop07cc04a2020-11-16 22:08:34 -08005718#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005719 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005721 return( PSA_ERROR_BAD_STATE );
5722 }
5723
Gilles Peskine224b0d62019-09-23 18:13:17 +02005724exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005725 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005726 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005727 return( status );
5728}
5729
Janos Follath51f4a0f2019-06-14 11:35:55 +01005730psa_status_t psa_key_derivation_input_bytes(
5731 psa_key_derivation_operation_t *operation,
5732 psa_key_derivation_step_t step,
5733 const uint8_t *data,
5734 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005735{
Gilles Peskineb8965192019-09-24 16:21:10 +02005736 return( psa_key_derivation_input_internal( operation, step,
5737 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005738 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005739}
5740
Janos Follath51f4a0f2019-06-14 11:35:55 +01005741psa_status_t psa_key_derivation_input_key(
5742 psa_key_derivation_operation_t *operation,
5743 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005744 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005745{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005746 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005747 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005748 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005749
Ronald Cron5c522922020-11-14 16:35:34 +01005750 status = psa_get_and_lock_transparent_key_slot_with_policy(
5751 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005752 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005753 {
5754 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005755 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005756 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005757
5758 /* Passing a key object as a SECRET input unlocks the permission
5759 * to output to a key object. */
5760 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5761 operation->can_output_key = 1;
5762
Ronald Cronf95a2b12020-10-22 15:24:49 +02005763 status = psa_key_derivation_input_internal( operation,
5764 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005765 slot->key.data,
5766 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005767
Ronald Cron5c522922020-11-14 16:35:34 +01005768 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005769
Ronald Cron5c522922020-11-14 16:35:34 +01005770 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005771}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005772
5773
5774
5775/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005776/* Key agreement */
5777/****************************************************************/
5778
John Durkop6ba40d12020-11-10 08:50:04 -08005779#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005780static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5781 size_t peer_key_length,
5782 const mbedtls_ecp_keypair *our_key,
5783 uint8_t *shared_secret,
5784 size_t shared_secret_size,
5785 size_t *shared_secret_length )
5786{
Steven Cooremand4867872020-08-05 16:31:39 +02005787 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005788 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005789 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005790 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005791 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005792 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005793
Ronald Cron90857082020-11-25 14:58:33 +01005794 status = mbedtls_psa_ecp_load_representation(
5795 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5796 peer_key,
5797 peer_key_length,
5798 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005799 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005800 goto exit;
5801
Jaeden Amero97271b32019-01-10 19:38:51 +00005802 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005803 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005804 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005805 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005806 status = mbedtls_to_psa_error(
5807 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5808 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005809 goto exit;
5810
Jaeden Amero97271b32019-01-10 19:38:51 +00005811 status = mbedtls_to_psa_error(
5812 mbedtls_ecdh_calc_secret( &ecdh,
5813 shared_secret_length,
5814 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005815 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005816 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005817 if( status != PSA_SUCCESS )
5818 goto exit;
5819 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5820 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005821
5822exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005823 if( status != PSA_SUCCESS )
5824 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005825 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005826 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005827 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005828
Jaeden Amero97271b32019-01-10 19:38:51 +00005829 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005830}
John Durkop6ba40d12020-11-10 08:50:04 -08005831#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005832
Gilles Peskine01d718c2018-09-18 12:01:02 +02005833#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5834
Gilles Peskine0216fe12019-04-11 21:23:21 +02005835static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5836 psa_key_slot_t *private_key,
5837 const uint8_t *peer_key,
5838 size_t peer_key_length,
5839 uint8_t *shared_secret,
5840 size_t shared_secret_size,
5841 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005842{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005843 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005844 {
John Durkop6ba40d12020-11-10 08:50:04 -08005845#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005846 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005847 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005848 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005849 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005850 psa_status_t status = mbedtls_psa_ecp_load_representation(
5851 private_key->attr.type,
5852 private_key->key.data,
5853 private_key->key.bytes,
5854 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005855 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005856 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005857 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005858 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005859 shared_secret, shared_secret_size,
5860 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005861 mbedtls_ecp_keypair_free( ecp );
5862 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005863 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005864#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005865 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005866 (void) private_key;
5867 (void) peer_key;
5868 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005869 (void) shared_secret;
5870 (void) shared_secret_size;
5871 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005872 return( PSA_ERROR_NOT_SUPPORTED );
5873 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005874}
5875
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005876/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005877 * to potentially free embedded data structures and wipe confidential data.
5878 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005879static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005880 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005881 psa_key_slot_t *private_key,
5882 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005883 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005884{
5885 psa_status_t status;
5886 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5887 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005888 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005889
5890 /* Step 1: run the secret agreement algorithm to generate the shared
5891 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005892 status = psa_key_agreement_raw_internal( ka_alg,
5893 private_key,
5894 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005895 shared_secret,
5896 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005897 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005898 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005899 goto exit;
5900
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005901 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005902 * the shared secret. A shared secret is permitted wherever a key
5903 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005904 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005905 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005906 shared_secret,
5907 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005908exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005909 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005910 return( status );
5911}
5912
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005913psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005914 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005915 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005916 const uint8_t *peer_key,
5917 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005918{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005919 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005920 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005921 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005922
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005923 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005924 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005925 status = psa_get_and_lock_transparent_key_slot_with_policy(
5926 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005927 if( status != PSA_SUCCESS )
5928 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005929 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005930 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005931 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005932 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005933 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005934 else
5935 {
5936 /* If a private key has been added as SECRET, we allow the derived
5937 * key material to be used as a key in PSA Crypto. */
5938 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5939 operation->can_output_key = 1;
5940 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005941
Ronald Cron5c522922020-11-14 16:35:34 +01005942 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005943
Ronald Cron5c522922020-11-14 16:35:34 +01005944 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005945}
5946
Gilles Peskinebe697d82019-05-16 18:00:41 +02005947psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005948 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005949 const uint8_t *peer_key,
5950 size_t peer_key_length,
5951 uint8_t *output,
5952 size_t output_size,
5953 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005954{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005955 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005956 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005957 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005958
5959 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5960 {
5961 status = PSA_ERROR_INVALID_ARGUMENT;
5962 goto exit;
5963 }
Ronald Cron5c522922020-11-14 16:35:34 +01005964 status = psa_get_and_lock_transparent_key_slot_with_policy(
5965 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005966 if( status != PSA_SUCCESS )
5967 goto exit;
5968
5969 status = psa_key_agreement_raw_internal( alg, slot,
5970 peer_key, peer_key_length,
5971 output, output_size,
5972 output_length );
5973
5974exit:
5975 if( status != PSA_SUCCESS )
5976 {
5977 /* If an error happens and is not handled properly, the output
5978 * may be used as a key to protect sensitive data. Arrange for such
5979 * a key to be random, which is likely to result in decryption or
5980 * verification errors. This is better than filling the buffer with
5981 * some constant data such as zeros, which would result in the data
5982 * being protected with a reproducible, easily knowable key.
5983 */
5984 psa_generate_random( output, output_size );
5985 *output_length = output_size;
5986 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005987
Ronald Cron5c522922020-11-14 16:35:34 +01005988 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005989
Ronald Cron5c522922020-11-14 16:35:34 +01005990 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005991}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005992
5993
Gilles Peskine30524eb2020-11-13 17:02:26 +01005994
Gilles Peskine86a440b2018-11-12 18:39:40 +01005995/****************************************************************/
5996/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005997/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005998
Gilles Peskine30524eb2020-11-13 17:02:26 +01005999/** Initialize the PSA random generator.
6000 */
6001static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6002{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006003#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6004 memset( rng, 0, sizeof( *rng ) );
6005#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6006
Gilles Peskine30524eb2020-11-13 17:02:26 +01006007 /* Set default configuration if
6008 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6009 if( rng->entropy_init == NULL )
6010 rng->entropy_init = mbedtls_entropy_init;
6011 if( rng->entropy_free == NULL )
6012 rng->entropy_free = mbedtls_entropy_free;
6013
6014 rng->entropy_init( &rng->entropy );
6015#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6016 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6017 /* The PSA entropy injection feature depends on using NV seed as an entropy
6018 * source. Add NV seed as an entropy source for PSA entropy injection. */
6019 mbedtls_entropy_add_source( &rng->entropy,
6020 mbedtls_nv_seed_poll, NULL,
6021 MBEDTLS_ENTROPY_BLOCK_SIZE,
6022 MBEDTLS_ENTROPY_SOURCE_STRONG );
6023#endif
6024
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006025 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006026#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006027}
6028
6029/** Deinitialize the PSA random generator.
6030 */
6031static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6032{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006033#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6034 memset( rng, 0, sizeof( *rng ) );
6035#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006036 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006037 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006038#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006039}
6040
6041/** Seed the PSA random generator.
6042 */
6043static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6044{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006045#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6046 /* Do nothing: the external RNG seeds itself. */
6047 (void) rng;
6048 return( PSA_SUCCESS );
6049#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006050 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006051 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6052 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006053 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006054#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006055}
6056
Gilles Peskinee59236f2018-01-27 23:32:46 +01006057psa_status_t psa_generate_random( uint8_t *output,
6058 size_t output_size )
6059{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006060 GUARD_MODULE_INITIALIZED;
6061
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006062#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6063
6064 size_t output_length = 0;
6065 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6066 output, output_size,
6067 &output_length );
6068 if( status != PSA_SUCCESS )
6069 return( status );
6070 /* Breaking up a request into smaller chunks is currently not supported
6071 * for the extrernal RNG interface. */
6072 if( output_length != output_size )
6073 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6074 return( PSA_SUCCESS );
6075
6076#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6077
Gilles Peskine71ddab92021-01-04 21:01:07 +01006078 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006079 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006080 size_t request_size =
6081 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6082 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6083 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006084 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6085 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006086 if( ret != 0 )
6087 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006088 output_size -= request_size;
6089 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006090 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006091 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006092#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006093}
6094
Gilles Peskine8814fc42020-12-14 15:33:44 +01006095/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006096 *
6097 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6098 * `psa_generate_random(...)`. The state parameter is ignored since the
6099 * PSA API doesn't support passing an explicit state.
6100 *
6101 * In the non-external case, psa_generate_random() calls an
6102 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6103 * and semantics as mbedtls_psa_get_random(). As an optimization,
6104 * instead of doing this back-and-forth between the PSA API and the
6105 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6106 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006107 */
6108#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6109int mbedtls_psa_get_random( void *p_rng,
6110 unsigned char *output,
6111 size_t output_size )
6112{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006113 /* This function takes a pointer to the RNG state because that's what
6114 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6115 * its own state internally and doesn't let the caller access that state.
6116 * So we just ignore the state parameter, and in practice we'll pass
6117 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006118 (void) p_rng;
6119 psa_status_t status = psa_generate_random( output, output_size );
6120 if( status == PSA_SUCCESS )
6121 return( 0 );
6122 else
6123 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6124}
6125#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6126
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006127#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6128#include "mbedtls/entropy_poll.h"
6129
Gilles Peskine7228da22019-07-15 11:06:15 +02006130psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006131 size_t seed_size )
6132{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006133 if( global_data.initialized )
6134 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006135
6136 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6137 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6138 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6139 return( PSA_ERROR_INVALID_ARGUMENT );
6140
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006141 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006142}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006143#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006144
John Durkop07cc04a2020-11-16 22:08:34 -08006145#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006146static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6147 size_t domain_parameters_size,
6148 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006149{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006150 size_t i;
6151 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006152
Gilles Peskinee56e8782019-04-26 17:34:02 +02006153 if( domain_parameters_size == 0 )
6154 {
6155 *exponent = 65537;
6156 return( PSA_SUCCESS );
6157 }
6158
6159 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6160 * support values that fit in a 32-bit integer, which is larger than
6161 * int on just about every platform anyway. */
6162 if( domain_parameters_size > sizeof( acc ) )
6163 return( PSA_ERROR_NOT_SUPPORTED );
6164 for( i = 0; i < domain_parameters_size; i++ )
6165 acc = ( acc << 8 ) | domain_parameters[i];
6166 if( acc > INT_MAX )
6167 return( PSA_ERROR_NOT_SUPPORTED );
6168 *exponent = acc;
6169 return( PSA_SUCCESS );
6170}
John Durkop07cc04a2020-11-16 22:08:34 -08006171#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006172
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006173static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006174 psa_key_slot_t *slot, size_t bits,
6175 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006176{
Gilles Peskine8e338702019-07-30 20:06:31 +02006177 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006178
Gilles Peskinee56e8782019-04-26 17:34:02 +02006179 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006180 return( PSA_ERROR_INVALID_ARGUMENT );
6181
Gilles Peskinee59236f2018-01-27 23:32:46 +01006182 if( key_type_is_raw_bytes( type ) )
6183 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006184 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006185
6186 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006187 if( status != PSA_SUCCESS )
6188 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006189
6190 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006191 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6192 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006193 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006194
Ronald Cronea0f8a62020-11-25 17:52:23 +01006195 status = psa_generate_random( slot->key.data,
6196 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006197 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006198 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006199
6200 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006201#if defined(MBEDTLS_DES_C)
6202 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006203 psa_des_set_key_parity( slot->key.data,
6204 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006205#endif /* MBEDTLS_DES_C */
6206 }
6207 else
6208
John Durkop07cc04a2020-11-16 22:08:34 -08006209#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006210 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006211 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006212 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006214 int exponent;
6215 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006216 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6217 return( PSA_ERROR_NOT_SUPPORTED );
6218 /* Accept only byte-aligned keys, for the same reasons as
Ronald Cronb6420e32020-11-22 16:15:38 +01006219 * in mbedtls_psa_rsa_import_key(). */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006220 if( bits % 8 != 0 )
6221 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006222 status = psa_read_rsa_exponent( domain_parameters,
6223 domain_parameters_size,
6224 &exponent );
6225 if( status != PSA_SUCCESS )
6226 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006227 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6228 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006229 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006230 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006231 (unsigned int) bits,
6232 exponent );
6233 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006234 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006235
6236 /* Make sure to always have an export representation available */
6237 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6238
Steven Cooreman75b74362020-07-28 14:30:13 +02006239 status = psa_allocate_buffer_to_slot( slot, bytes );
6240 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006241 {
6242 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006243 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006244 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006245
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01006246 status = mbedtls_psa_rsa_export_key( type,
6247 &rsa,
6248 slot->key.data,
6249 bytes,
6250 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006251 mbedtls_rsa_free( &rsa );
6252 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006253 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006254 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006255 }
6256 else
John Durkop07cc04a2020-11-16 22:08:34 -08006257#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006258
John Durkop9814fa22020-11-04 12:28:15 -08006259#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006260 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006261 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006262 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006263 mbedtls_ecp_group_id grp_id =
6264 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006265 const mbedtls_ecp_curve_info *curve_info =
6266 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006267 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006269 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006270 return( PSA_ERROR_NOT_SUPPORTED );
6271 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6272 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006273 mbedtls_ecp_keypair_init( &ecp );
6274 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006275 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006276 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006277 if( ret != 0 )
6278 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006279 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006280 return( mbedtls_to_psa_error( ret ) );
6281 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006282
6283
6284 /* Make sure to always have an export representation available */
6285 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006286 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6287 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006288 {
6289 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006290 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006291 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006292
6293 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006294 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006295
6296 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006297 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006298 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006299 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006300 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006301 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006302 }
6303 else
John Durkop9814fa22020-11-04 12:28:15 -08006304#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006305 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006306 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006307 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006308
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006309 return( PSA_SUCCESS );
6310}
Darryl Green0c6575a2018-11-07 16:05:30 +00006311
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006312psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006313 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006314{
6315 psa_status_t status;
6316 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006317 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006318
Ronald Cron81709fc2020-11-14 12:10:32 +01006319 *key = MBEDTLS_SVC_KEY_ID_INIT;
6320
Gilles Peskine0f84d622019-09-12 19:03:13 +02006321 /* Reject any attempt to create a zero-length key so that we don't
6322 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6323 if( psa_get_key_bits( attributes ) == 0 )
6324 return( PSA_ERROR_INVALID_ARGUMENT );
6325
Ronald Cron81709fc2020-11-14 12:10:32 +01006326 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6327 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006328 if( status != PSA_SUCCESS )
6329 goto exit;
6330
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006331 status = psa_driver_wrapper_generate_key( attributes,
6332 slot );
6333 if( status != PSA_ERROR_NOT_SUPPORTED ||
6334 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6335 goto exit;
6336
6337 status = psa_generate_key_internal(
6338 slot, attributes->core.bits,
6339 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006340
6341exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006342 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006343 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006344 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006345 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006346
Darryl Green0c6575a2018-11-07 16:05:30 +00006347 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006348}
6349
6350
Gilles Peskinee59236f2018-01-27 23:32:46 +01006351
6352/****************************************************************/
6353/* Module setup */
6354/****************************************************************/
6355
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006356#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006357psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6358 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6359 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6360{
6361 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6362 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006363 global_data.rng.entropy_init = entropy_init;
6364 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006365 return( PSA_SUCCESS );
6366}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006367#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006368
Gilles Peskinee59236f2018-01-27 23:32:46 +01006369void mbedtls_psa_crypto_free( void )
6370{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006371 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006372 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6373 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006374 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006375 }
6376 /* Wipe all remaining data, including configuration.
6377 * In particular, this sets all state indicator to the value
6378 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006379 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006380#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006381 /* Unregister all secure element drivers, so that we restart from
6382 * a pristine state. */
6383 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006384#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006385}
6386
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006387#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6388/** Recover a transaction that was interrupted by a power failure.
6389 *
6390 * This function is called during initialization, before psa_crypto_init()
6391 * returns. If this function returns a failure status, the initialization
6392 * fails.
6393 */
6394static psa_status_t psa_crypto_recover_transaction(
6395 const psa_crypto_transaction_t *transaction )
6396{
6397 switch( transaction->unknown.type )
6398 {
6399 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6400 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006401 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006402 * is implemented.
6403 * https://github.com/ARMmbed/mbed-crypto/issues/218
6404 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006405 default:
6406 /* We found an unsupported transaction in the storage.
6407 * We don't know what state the storage is in. Give up. */
6408 return( PSA_ERROR_STORAGE_FAILURE );
6409 }
6410}
6411#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6412
Gilles Peskinee59236f2018-01-27 23:32:46 +01006413psa_status_t psa_crypto_init( void )
6414{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006415 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006416
Gilles Peskinec6b69072018-11-20 21:42:52 +01006417 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006418 if( global_data.initialized != 0 )
6419 return( PSA_SUCCESS );
6420
Gilles Peskine30524eb2020-11-13 17:02:26 +01006421 /* Initialize and seed the random generator. */
6422 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006423 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006424 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006425 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006426 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006427 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006428
Gilles Peskine66fb1262018-12-10 16:29:04 +01006429 status = psa_initialize_key_slots( );
6430 if( status != PSA_SUCCESS )
6431 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006432
Gilles Peskined9348f22019-10-01 15:22:29 +02006433#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6434 status = psa_init_all_se_drivers( );
6435 if( status != PSA_SUCCESS )
6436 goto exit;
6437#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6438
Gilles Peskine4b734222019-07-24 15:56:31 +02006439#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006440 status = psa_crypto_load_transaction( );
6441 if( status == PSA_SUCCESS )
6442 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006443 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6444 if( status != PSA_SUCCESS )
6445 goto exit;
6446 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006447 }
6448 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6449 {
6450 /* There's no transaction to complete. It's all good. */
6451 status = PSA_SUCCESS;
6452 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006453#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006454
Gilles Peskinec6b69072018-11-20 21:42:52 +01006455 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006456 global_data.initialized = 1;
6457
6458exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006459 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006460 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006461 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006462}
6463
6464#endif /* MBEDTLS_PSA_CRYPTO_C */