blob: e9ea81abf07d581250260575cade17c68c07af77 [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"
Ronald Cron7023db52020-11-20 18:17:42 +010037#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020038#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020039#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020040#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010041#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010042/* Include internal declarations that are useful for implementing persistently
43 * stored keys. */
44#include "psa_crypto_storage.h"
45
Gilles Peskineb2b64d32020-12-14 16:43:58 +010046#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010047
Gilles Peskineb46bef22019-07-30 21:32:04 +020048#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010049#include <stdlib.h>
50#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020052#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020059#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000060#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020061#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020064#include "mbedtls/chacha20.h"
65#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/cipher.h"
67#include "mbedtls/ccm.h"
68#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020070#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/error.h"
74#include "mbedtls/gcm.h"
75#include "mbedtls/md2.h"
76#include "mbedtls/md4.h"
77#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010078#include "mbedtls/md.h"
79#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010080#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010082#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000083#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/sha1.h"
87#include "mbedtls/sha256.h"
88#include "mbedtls/sha512.h"
89#include "mbedtls/xtea.h"
90
Gilles Peskine996deb12018-08-01 15:45:45 +020091#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111static int key_type_is_raw_bytes( psa_key_type_t type )
112{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200113 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200114}
115
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100116/* Values for psa_global_data_t::rng_state */
117#define RNG_NOT_INITIALIZED 0
118#define RNG_INITIALIZED 1
119#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120
Gilles Peskine2d277862018-06-18 15:41:12 +0200121typedef struct
122{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100123 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100124 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100125 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126} psa_global_data_t;
127
128static psa_global_data_t global_data;
129
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100130#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
131mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
132 &global_data.rng.drbg;
133#endif
134
itayzafrir0adf0fc2018-09-06 16:24:41 +0300135#define GUARD_MODULE_INITIALIZED \
136 if( global_data.initialized == 0 ) \
137 return( PSA_ERROR_BAD_STATE );
138
Steven Cooreman01164162020-07-20 15:31:37 +0200139psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100140{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100141 /* Mbed TLS error codes can combine a high-level error code and a
142 * low-level error code. The low-level error usually reflects the
143 * root cause better, so dispatch on that preferably. */
144 int low_level_ret = - ( -ret & 0x007f );
145 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100146 {
147 case 0:
148 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100149
150 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
151 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
152 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
153 return( PSA_ERROR_NOT_SUPPORTED );
154 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
155 return( PSA_ERROR_HARDWARE_FAILURE );
156
Gilles Peskine9a944802018-06-21 09:35:35 +0200157 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
158 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
159 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
160 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
161 case MBEDTLS_ERR_ASN1_INVALID_DATA:
162 return( PSA_ERROR_INVALID_ARGUMENT );
163 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
164 return( PSA_ERROR_INSUFFICIENT_MEMORY );
165 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
166 return( PSA_ERROR_BUFFER_TOO_SMALL );
167
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000169 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
171 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
172#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100173 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
Jaeden Amero93e21112019-02-20 13:57:28 +0000178#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000179 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
181 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
182#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_CCM_BAD_INPUT:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190 case MBEDTLS_ERR_CCM_AUTH_FAILED:
191 return( PSA_ERROR_INVALID_SIGNATURE );
192 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
193 return( PSA_ERROR_HARDWARE_FAILURE );
194
Gilles Peskine26869f22019-05-06 15:25:00 +0200195 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197
198 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
199 return( PSA_ERROR_BAD_STATE );
200 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
201 return( PSA_ERROR_INVALID_SIGNATURE );
202
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
204 return( PSA_ERROR_NOT_SUPPORTED );
205 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
206 return( PSA_ERROR_INVALID_ARGUMENT );
207 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
208 return( PSA_ERROR_INSUFFICIENT_MEMORY );
209 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
210 return( PSA_ERROR_INVALID_PADDING );
211 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200212 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100213 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
214 return( PSA_ERROR_INVALID_SIGNATURE );
215 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200216 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
218 return( PSA_ERROR_HARDWARE_FAILURE );
219
220 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
Gilles Peskine82e57d12020-11-13 21:31:17 +0100223#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
224 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100225 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
226 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100227 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
229 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
230 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
231 return( PSA_ERROR_NOT_SUPPORTED );
232 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100234#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100235
236 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
237 return( PSA_ERROR_NOT_SUPPORTED );
238 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
Gilles Peskinee59236f2018-01-27 23:32:46 +0100241 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
242 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
243 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
244 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100245
246 case MBEDTLS_ERR_GCM_AUTH_FAILED:
247 return( PSA_ERROR_INVALID_SIGNATURE );
248 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200249 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100250 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
251 return( PSA_ERROR_HARDWARE_FAILURE );
252
Gilles Peskine82e57d12020-11-13 21:31:17 +0100253#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
254 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100255 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
256 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
258 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
259 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
260 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
261 return( PSA_ERROR_NOT_SUPPORTED );
262 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
263 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
264#endif
265
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
267 return( PSA_ERROR_NOT_SUPPORTED );
268 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MD_ALLOC_FAILED:
271 return( PSA_ERROR_INSUFFICIENT_MEMORY );
272 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
273 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100274
Gilles Peskinef76aa772018-10-29 19:24:33 +0100275 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
276 return( PSA_ERROR_STORAGE_FAILURE );
277 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
282 return( PSA_ERROR_BUFFER_TOO_SMALL );
283 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
290 return( PSA_ERROR_INSUFFICIENT_MEMORY );
291
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 case MBEDTLS_ERR_PK_ALLOC_FAILED:
293 return( PSA_ERROR_INSUFFICIENT_MEMORY );
294 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
295 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
300 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
303 return( PSA_ERROR_NOT_SUPPORTED );
304 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
305 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
306 return( PSA_ERROR_NOT_PERMITTED );
307 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_PK_INVALID_ALG:
310 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
311 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
312 return( PSA_ERROR_NOT_SUPPORTED );
313 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
314 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
316 return( PSA_ERROR_HARDWARE_FAILURE );
317
Gilles Peskineff2d2002019-05-06 15:26:23 +0200318 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
319 return( PSA_ERROR_HARDWARE_FAILURE );
320 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
321 return( PSA_ERROR_NOT_SUPPORTED );
322
Gilles Peskinea5905292018-02-07 20:59:33 +0100323 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
324 return( PSA_ERROR_INVALID_ARGUMENT );
325 case MBEDTLS_ERR_RSA_INVALID_PADDING:
326 return( PSA_ERROR_INVALID_PADDING );
327 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
330 return( PSA_ERROR_INVALID_ARGUMENT );
331 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
332 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200333 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
335 return( PSA_ERROR_INVALID_SIGNATURE );
336 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
337 return( PSA_ERROR_BUFFER_TOO_SMALL );
338 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100339 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100340 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
341 return( PSA_ERROR_NOT_SUPPORTED );
342 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
343 return( PSA_ERROR_HARDWARE_FAILURE );
344
Gilles Peskinea5905292018-02-07 20:59:33 +0100345 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
346 return( PSA_ERROR_INVALID_ARGUMENT );
347 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
348 return( PSA_ERROR_HARDWARE_FAILURE );
349
itayzafrir5c753392018-05-08 11:18:38 +0300350 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300351 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300352 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300353 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
354 return( PSA_ERROR_BUFFER_TOO_SMALL );
355 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
356 return( PSA_ERROR_NOT_SUPPORTED );
357 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
358 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
359 return( PSA_ERROR_INVALID_SIGNATURE );
360 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
361 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100362 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
363 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300364 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
365 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100366
Janos Follatha13b9052019-11-22 12:48:59 +0000367 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
368 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300369
Gilles Peskinee59236f2018-01-27 23:32:46 +0100370 default:
David Saadab4ecc272019-02-14 13:48:10 +0200371 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100372 }
373}
374
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200375
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200376
377
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378/****************************************************************/
379/* Key management */
380/****************************************************************/
381
Gilles Peskine73167e12019-07-12 23:44:37 +0200382#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
383static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
384{
Gilles Peskine8e338702019-07-30 20:06:31 +0200385 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200386}
387#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
388
John Durkop07cc04a2020-11-16 22:08:34 -0800389/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
390 * current test driver in key_management.c is using this function
391 * when accelerators are used for ECC key pair and public key.
392 * Once that dependency is resolved these guards can be removed.
393 */
John Durkop9814fa22020-11-04 12:28:15 -0800394#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800395 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
396 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
397 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100398mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100399 size_t bits,
400 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200401{
402 switch( curve )
403 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100404 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100405 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100406 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100407 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100408 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100409 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100410 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100411 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100412 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100413 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100414 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100415 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100416 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 case 528:
418 if( bits_is_sloppy )
419 return( MBEDTLS_ECP_DP_SECP521R1 );
420 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100421 }
422 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100423
Paul Elliott8ff510a2020-06-02 17:19:28 +0100424 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100428 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100430 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100433 }
434 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100435
Paul Elliott8ff510a2020-06-02 17:19:28 +0100436 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100437 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100438 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100439 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100440 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100441 case 256:
442 if( bits_is_sloppy )
443 return( MBEDTLS_ECP_DP_CURVE25519 );
444 break;
445 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100446 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100447 }
448 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100449
Paul Elliott8ff510a2020-06-02 17:19:28 +0100450 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100451 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100452 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100453 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100454 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100456 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100459 }
460 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200461 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100462
463 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200464}
John Durkop9814fa22020-11-04 12:28:15 -0800465#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800466 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
467 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
468 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200469
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200470static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
471 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200472{
473 /* Check that the bit size is acceptable for the key type */
474 switch( type )
475 {
476 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200478 case PSA_KEY_TYPE_DERIVE:
479 break;
David Browne04acc22021-01-26 11:39:16 -0700480#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481 case PSA_KEY_TYPE_AES:
482 if( bits != 128 && bits != 192 && bits != 256 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
484 break;
485#endif
David Browne04acc22021-01-26 11:39:16 -0700486#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 case PSA_KEY_TYPE_CAMELLIA:
488 if( bits != 128 && bits != 192 && bits != 256 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
490 break;
491#endif
David Browne04acc22021-01-26 11:39:16 -0700492#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200493 case PSA_KEY_TYPE_DES:
494 if( bits != 64 && bits != 128 && bits != 192 )
495 return( PSA_ERROR_INVALID_ARGUMENT );
496 break;
497#endif
David Browne04acc22021-01-26 11:39:16 -0700498#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 case PSA_KEY_TYPE_ARC4:
500 if( bits < 8 || bits > 2048 )
501 return( PSA_ERROR_INVALID_ARGUMENT );
502 break;
503#endif
David Brown1bfe4d72021-02-16 12:54:35 -0700504#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200505 case PSA_KEY_TYPE_CHACHA20:
506 if( bits != 256 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
508 break;
509#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200510 default:
511 return( PSA_ERROR_NOT_SUPPORTED );
512 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200513 if( bits % 8 != 0 )
514 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200516 return( PSA_SUCCESS );
517}
518
Gilles Peskineb46bef22019-07-30 21:32:04 +0200519/** Return the size of the key in the given slot, in bits.
520 *
521 * \param[in] slot A key slot.
522 *
523 * \return The key size in bits, read from the metadata in the slot.
524 */
525static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
526{
527 return( slot->attr.bits );
528}
529
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100530/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100531 *
Steven Cooremancd640932021-03-08 12:00:27 +0100532 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
533 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100534 *
535 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100536 * \param[in] key_type The key type of the key to be used with the
537 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100538 *
539 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100540 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100541 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100542 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100543 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100544MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100545 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100546 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100547{
Steven Cooremancd640932021-03-08 12:00:27 +0100548 if( PSA_ALG_IS_HMAC( algorithm ) )
549 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100550 if( key_type == PSA_KEY_TYPE_HMAC )
551 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100552 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100553
554 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100555 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100556 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
557 * key. */
558 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
559 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
560 {
561 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
562 * the block length (larger than 1) for block ciphers. */
563 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
564 return( PSA_SUCCESS );
565 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100566 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100567
568 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100569}
570
Steven Cooreman75b74362020-07-28 14:30:13 +0200571/** Try to allocate a buffer to an empty key slot.
572 *
573 * \param[in,out] slot Key slot to attach buffer to.
574 * \param[in] buffer_length Requested size of the buffer.
575 *
576 * \retval #PSA_SUCCESS
577 * The buffer has been successfully allocated.
578 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
579 * Not enough memory was available for allocation.
580 * \retval #PSA_ERROR_ALREADY_EXISTS
581 * Trying to allocate a buffer to a non-empty key slot.
582 */
583static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
584 size_t buffer_length )
585{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100586 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200587 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200588
Ronald Cronea0f8a62020-11-25 17:52:23 +0100589 slot->key.data = mbedtls_calloc( 1, buffer_length );
590 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200591 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200592
Ronald Cronea0f8a62020-11-25 17:52:23 +0100593 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200594 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200595}
596
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200597psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
598 const uint8_t* data,
599 size_t data_length )
600{
601 psa_status_t status = psa_allocate_buffer_to_slot( slot,
602 data_length );
603 if( status != PSA_SUCCESS )
604 return( status );
605
Ronald Cronea0f8a62020-11-25 17:52:23 +0100606 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200607 return( PSA_SUCCESS );
608}
609
Ronald Crona0fe59f2020-11-29 11:14:53 +0100610psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100611 const psa_key_attributes_t *attributes,
612 const uint8_t *data, size_t data_length,
613 uint8_t *key_buffer, size_t key_buffer_size,
614 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100615{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100616 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
617 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200619 /* zero-length keys are never supported. */
620 if( data_length == 0 )
621 return( PSA_ERROR_NOT_SUPPORTED );
622
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100623 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100625 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200626
Steven Cooreman75b74362020-07-28 14:30:13 +0200627 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
628 if( data_length > SIZE_MAX / 8 )
629 return( PSA_ERROR_NOT_SUPPORTED );
630
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200631 /* Enforce a size limit, and in particular ensure that the bit
632 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100633 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200634 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200635
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100636 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200637 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200638 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200639
Ronald Crondd04d422020-11-28 15:14:42 +0100640 /* Copy the key material. */
641 memcpy( key_buffer, data, data_length );
642 *key_buffer_length = data_length;
643 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200644
Steven Cooreman40120f62020-10-29 11:42:22 +0100645 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100647 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200648 {
John Durkop9814fa22020-11-04 12:28:15 -0800649#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
650 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100651 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200652 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100653 return( mbedtls_psa_ecp_import_key( attributes,
654 data, data_length,
655 key_buffer, key_buffer_size,
656 key_buffer_length,
657 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100658 }
John Durkop9814fa22020-11-04 12:28:15 -0800659#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
660 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700661#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
662 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100663 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200664 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100665 return( mbedtls_psa_rsa_import_key( attributes,
666 data, data_length,
667 key_buffer, key_buffer_size,
668 key_buffer_length,
669 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200670 }
John Durkop9814fa22020-11-04 12:28:15 -0800671#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
672 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100673 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100674
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100675 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100676}
677
Gilles Peskinef603c712019-01-19 13:40:11 +0100678/** Calculate the intersection of two algorithm usage policies.
679 *
680 * Return 0 (which allows no operation) on incompatibility.
681 */
682static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100683 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100684 psa_algorithm_t alg1,
685 psa_algorithm_t alg2 )
686{
Gilles Peskine549ea862019-05-22 11:45:59 +0200687 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100688 if( alg1 == alg2 )
689 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100690 /* If the policies are from the same hash-and-sign family, check
691 * if one is a wildcard. If so the other has the specific algorithm. */
692 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
693 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
694 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100695 {
Steven Cooreman03488022021-02-18 12:07:20 +0100696 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
697 return( alg2 );
698 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
699 return( alg1 );
700 }
701 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100702 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100703 * restrictive tag length. */
704 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
705 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
706 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
707 {
708 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
709 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100710 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100711
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100712 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100713 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
714 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100715 {
Steven Cooremancd640932021-03-08 12:00:27 +0100716 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
717 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100718 }
719 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100720 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100721 ( alg1_len <= alg2_len ) )
722 {
723 return( alg2 );
724 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100725 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100726 ( alg2_len <= alg1_len ) )
727 {
728 return( alg1 );
729 }
730 }
731 /* If the policies are from the same MAC family, check whether one
732 * of them is a minimum-MAC-length policy. Calculate the most
733 * restrictive tag length. */
734 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
735 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
736 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
737 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100738 /* Validate the combination of key type and algorithm. Since the base
739 * algorithm of alg1 and alg2 are the same, we only need this once. */
740 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100741 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100742
Steven Cooreman31a876d2021-03-03 20:47:40 +0100743 /* Get the (exact or at-least) output lengths for both sides of the
744 * requested intersection. None of the currently supported algorithms
745 * have an output length dependent on the actual key size, so setting it
746 * to a bogus value of 0 is currently OK.
747 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100748 * Note that for at-least-this-length wildcard algorithms, the output
749 * length is set to the shortest allowed length, which allows us to
750 * calculate the most restrictive tag length for the intersection. */
751 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
752 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100753 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100754
Steven Cooremana1d83222021-02-25 10:20:29 +0100755 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100756 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
757 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100758 {
Steven Cooremancd640932021-03-08 12:00:27 +0100759 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100760 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100761
762 /* If only one is an at-least-this-length policy, the intersection would
763 * be the other (fixed-length) policy as long as said fixed length is
764 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100765 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100766 {
Steven Cooremancd640932021-03-08 12:00:27 +0100767 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100768 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100769 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100770 {
Steven Cooremancd640932021-03-08 12:00:27 +0100771 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100772 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100773
Steven Cooremancd640932021-03-08 12:00:27 +0100774 /* If none of them are wildcards, check whether they define the same tag
775 * length. This is still possible here when one is default-length and
776 * the other specific-length. Ensure to always return the
777 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100778 if( alg1_len == alg2_len )
779 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100780 }
781 /* If the policies are incompatible, allow nothing. */
782 return( 0 );
783}
784
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100785static int psa_key_algorithm_permits( psa_key_type_t key_type,
786 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200787 psa_algorithm_t requested_alg )
788{
Gilles Peskine549ea862019-05-22 11:45:59 +0200789 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200790 if( requested_alg == policy_alg )
791 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100792 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
793 * and requested_alg is the same hash-and-sign family with any hash,
794 * then requested_alg is compliant with policy_alg. */
795 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
796 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200797 {
Steven Cooreman03488022021-02-18 12:07:20 +0100798 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
799 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
800 }
801 /* If policy_alg is a wildcard AEAD algorithm of the same base as
802 * the requested algorithm, check the requested tag length to be
803 * equal-length or longer than the wildcard-specified length. */
804 if( PSA_ALG_IS_AEAD( policy_alg ) &&
805 PSA_ALG_IS_AEAD( requested_alg ) &&
806 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
807 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100808 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100809 {
810 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
811 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
812 }
Steven Cooremancd640932021-03-08 12:00:27 +0100813 /* If policy_alg is a MAC algorithm of the same base as the requested
814 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100815 if( PSA_ALG_IS_MAC( policy_alg ) &&
816 PSA_ALG_IS_MAC( requested_alg ) &&
817 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100818 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100819 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100820 /* Validate the combination of key type and algorithm. Since the policy
821 * and requested algorithms are the same, we only need this once. */
822 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100823 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100824
Steven Cooreman31a876d2021-03-03 20:47:40 +0100825 /* Get both the requested output length for the algorithm which is to be
826 * verified, and the default output length for the base algorithm.
827 * Note that none of the currently supported algorithms have an output
828 * length dependent on actual key size, so setting it to a bogus value
829 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100830 size_t requested_output_length = PSA_MAC_LENGTH(
831 key_type, 0, requested_alg );
832 size_t default_output_length = PSA_MAC_LENGTH(
833 key_type, 0,
834 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100835
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100836 /* If the policy is default-length, only allow an algorithm with
837 * a declared exact-length matching the default. */
838 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100839 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100840
841 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100842 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100843 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
844 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
845 {
846 return( 1 );
847 }
848
Steven Cooremancd640932021-03-08 12:00:27 +0100849 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
850 * check for the requested MAC length to be equal to or longer than the
851 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100852 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
853 {
854 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100855 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100856 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200857 }
Steven Cooremance48e852020-10-05 16:02:45 +0200858 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200859 * a key derivation with that key agreement should also be allowed. This
860 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200861 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
862 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
863 {
864 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
865 policy_alg );
866 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100867 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200868 return( 0 );
869}
870
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100871/** Test whether a policy permits an algorithm.
872 *
873 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100874 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100875 * \note This function requires providing the key type for which the policy is
876 * being validated, since some algorithm policy definitions (e.g. MAC)
877 * have different properties depending on what kind of cipher it is
878 * combined with.
879 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100880 * \retval PSA_SUCCESS When \p alg is a specific algorithm
881 * allowed by the \p policy.
882 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
883 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
884 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100885 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100886static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100887 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100888 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100889{
Steven Cooremand788fab2021-02-25 11:29:17 +0100890 /* '0' is not a valid algorithm */
891 if( alg == 0 )
892 return( PSA_ERROR_INVALID_ARGUMENT );
893
Steven Cooreman7e39f052021-02-23 14:18:32 +0100894 /* A requested algorithm cannot be a wildcard. */
895 if( PSA_ALG_IS_WILDCARD( alg ) )
896 return( PSA_ERROR_INVALID_ARGUMENT );
897
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100898 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
899 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100900 return( PSA_SUCCESS );
901 else
902 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100903}
904
Gilles Peskinef603c712019-01-19 13:40:11 +0100905/** Restrict a key policy based on a constraint.
906 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100907 * \note This function requires providing the key type for which the policy is
908 * being restricted, since some algorithm policy definitions (e.g. MAC)
909 * have different properties depending on what kind of cipher it is
910 * combined with.
911 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100912 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100913 * \param[in,out] policy The policy to restrict.
914 * \param[in] constraint The policy constraint to apply.
915 *
916 * \retval #PSA_SUCCESS
917 * \c *policy contains the intersection of the original value of
918 * \c *policy and \c *constraint.
919 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100920 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100921 * \c *policy is unchanged.
922 */
923static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100924 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100925 psa_key_policy_t *policy,
926 const psa_key_policy_t *constraint )
927{
928 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100929 psa_key_policy_algorithm_intersection( key_type, policy->alg,
930 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200931 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100932 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
933 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100934 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
935 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200936 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
937 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100938 policy->usage &= constraint->usage;
939 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200940 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100941 return( PSA_SUCCESS );
942}
943
Ronald Cron5c522922020-11-14 16:35:34 +0100944/** Get the description of a key given its identifier and policy constraints
945 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200946 *
Ronald Cron5c522922020-11-14 16:35:34 +0100947 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100948 * nonzero, the key must allow operations with this algorithm. If \p alg is
949 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100950 *
Ronald Cron5c522922020-11-14 16:35:34 +0100951 * In case of a persistent key, the function loads the description of the key
952 * into a key slot if not already done.
953 *
954 * On success, the returned key slot is locked. It is the responsibility of
955 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200956 */
Ronald Cron5c522922020-11-14 16:35:34 +0100957static psa_status_t psa_get_and_lock_key_slot_with_policy(
958 mbedtls_svc_key_id_t key,
959 psa_key_slot_t **p_slot,
960 psa_key_usage_t usage,
961 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100962{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200963 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
964 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100965
Ronald Cron5c522922020-11-14 16:35:34 +0100966 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100967 if( status != PSA_SUCCESS )
968 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200969 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100970
971 /* Enforce that usage policy for the key slot contains all the flags
972 * required by the usage parameter. There is one exception: public
973 * keys can always be exported, so we treat public key objects as
974 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200975 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100976 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200977
Gilles Peskine8e338702019-07-30 20:06:31 +0200978 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100979 {
980 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200981 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100982 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100983
984 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100985 if( alg != 0 )
986 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100987 status = psa_key_policy_permits( &slot->attr.policy,
988 slot->attr.type,
989 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +0100990 if( status != PSA_SUCCESS )
991 goto error;
992 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100993
Darryl Green06fd18d2018-07-16 11:21:11 +0100994 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200995
996error:
997 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100998 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200999
1000 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001001}
Darryl Green940d72c2018-07-13 13:18:51 +01001002
Ronald Cron5c522922020-11-14 16:35:34 +01001003/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001004 *
1005 * A transparent key is a key for which the key material is directly
1006 * available, as opposed to a key in a secure element.
1007 *
Ronald Cron5c522922020-11-14 16:35:34 +01001008 * This is a temporary function to use instead of
1009 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1010 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001011 *
Ronald Cron5c522922020-11-14 16:35:34 +01001012 * On success, the returned key slot is locked. It is the responsibility of the
1013 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001014 */
1015#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001016static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1017 mbedtls_svc_key_id_t key,
1018 psa_key_slot_t **p_slot,
1019 psa_key_usage_t usage,
1020 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001021{
Ronald Cron5c522922020-11-14 16:35:34 +01001022 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1023 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001024 if( status != PSA_SUCCESS )
1025 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001026
Gilles Peskineadad8132019-07-25 11:31:23 +02001027 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001028 {
Ronald Cron5c522922020-11-14 16:35:34 +01001029 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001030 *p_slot = NULL;
1031 return( PSA_ERROR_NOT_SUPPORTED );
1032 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001033
Gilles Peskine28f8f302019-07-24 13:30:31 +02001034 return( PSA_SUCCESS );
1035}
1036#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1037/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001038#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1039 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001040#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1041
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001042/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001043static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001044{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001045 /* Data pointer will always be either a valid pointer or NULL in an
1046 * initialized slot, so we can just free it. */
1047 if( slot->key.data != NULL )
1048 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1049
1050 mbedtls_free( slot->key.data );
1051 slot->key.data = NULL;
1052 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001053
1054 return( PSA_SUCCESS );
1055}
1056
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001057/** Completely wipe a slot in memory, including its policy.
1058 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001059psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001060{
1061 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001062
1063 /*
1064 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001065 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001066 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1067 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001068 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001069 */
Ronald Cron5c522922020-11-14 16:35:34 +01001070 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001071 {
1072#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001073 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001074#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001075 status = PSA_ERROR_CORRUPTION_DETECTED;
1076 }
1077
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001078 /* Multipart operations may still be using the key. This is safe
1079 * because all multipart operation objects are independent from
1080 * the key slot: if they need to access the key after the setup
1081 * phase, they have a copy of the key. Note that this means that
1082 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001083 /* At this point, key material and other type-specific content has
1084 * been wiped. Clear remaining metadata. We can call memset and not
1085 * zeroize because the metadata is not particularly sensitive. */
1086 memset( slot, 0, sizeof( *slot ) );
1087 return( status );
1088}
1089
Ronald Croncf56a0a2020-08-04 09:51:30 +02001090psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001092 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001093 psa_status_t status; /* status of the last operation */
1094 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001095#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1096 psa_se_drv_table_entry_t *driver;
1097#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098
Ronald Croncf56a0a2020-08-04 09:51:30 +02001099 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001100 return( PSA_SUCCESS );
1101
Ronald Cronf2911112020-10-29 17:51:10 +01001102 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001103 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001104 * key, this will load the key description from persistent memory if not
1105 * done yet. We cannot avoid this loading as without it we don't know if
1106 * the key is operated by an SE or not and this information is needed by
1107 * the current implementation.
1108 */
Ronald Cron5c522922020-11-14 16:35:34 +01001109 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001110 if( status != PSA_SUCCESS )
1111 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001112
Ronald Cronf2911112020-10-29 17:51:10 +01001113 /*
1114 * If the key slot containing the key description is under access by the
1115 * library (apart from the present access), the key cannot be destroyed
1116 * yet. For the time being, just return in error. Eventually (to be
1117 * implemented), the key should be destroyed when all accesses have
1118 * stopped.
1119 */
Ronald Cron5c522922020-11-14 16:35:34 +01001120 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001121 {
Ronald Cron5c522922020-11-14 16:35:34 +01001122 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001123 return( PSA_ERROR_GENERIC_ERROR );
1124 }
1125
Gilles Peskine354f7672019-07-12 23:46:38 +02001126#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001127 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001128 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001129 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001130 /* For a key in a secure element, we need to do three things:
1131 * remove the key file in internal storage, destroy the
1132 * key inside the secure element, and update the driver's
1133 * persistent data. Start a transaction that will encompass these
1134 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001135 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001136 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001137 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001138 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001139 status = psa_crypto_save_transaction( );
1140 if( status != PSA_SUCCESS )
1141 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001142 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001143 /* We should still try to destroy the key in the secure
1144 * element and the key metadata in storage. This is especially
1145 * important if the error is that the storage is full.
1146 * But how to do it exactly without risking an inconsistent
1147 * state after a reset?
1148 * https://github.com/ARMmbed/mbed-crypto/issues/215
1149 */
1150 overall_status = status;
1151 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001152 }
1153
Ronald Cronea0f8a62020-11-25 17:52:23 +01001154 status = psa_destroy_se_key( driver,
1155 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001156 if( overall_status == PSA_SUCCESS )
1157 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001158 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001159#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1160
Darryl Greend49a4992018-06-18 17:27:26 +01001161#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001162 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001163 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001164 status = psa_destroy_persistent_key( slot->attr.id );
1165 if( overall_status == PSA_SUCCESS )
1166 overall_status = status;
1167
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001168 /* TODO: other slots may have a copy of the same key. We should
1169 * invalidate them.
1170 * https://github.com/ARMmbed/mbed-crypto/issues/214
1171 */
Darryl Greend49a4992018-06-18 17:27:26 +01001172 }
1173#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001174
Gilles Peskinefc762652019-07-22 19:30:34 +02001175#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1176 if( driver != NULL )
1177 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001178 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001179 if( overall_status == PSA_SUCCESS )
1180 overall_status = status;
1181 status = psa_crypto_stop_transaction( );
1182 if( overall_status == PSA_SUCCESS )
1183 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001184 }
1185#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1186
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001187#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1188exit:
1189#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001190 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001191 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1192 if( overall_status == PSA_SUCCESS )
1193 overall_status = status;
1194 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001195}
1196
John Durkop07cc04a2020-11-16 22:08:34 -08001197#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001198 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001199static psa_status_t psa_get_rsa_public_exponent(
1200 const mbedtls_rsa_context *rsa,
1201 psa_key_attributes_t *attributes )
1202{
1203 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001204 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001205 uint8_t *buffer = NULL;
1206 size_t buflen;
1207 mbedtls_mpi_init( &mpi );
1208
1209 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1210 if( ret != 0 )
1211 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001212 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1213 {
1214 /* It's the default value, which is reported as an empty string,
1215 * so there's nothing to do. */
1216 goto exit;
1217 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001218
1219 buflen = mbedtls_mpi_size( &mpi );
1220 buffer = mbedtls_calloc( 1, buflen );
1221 if( buffer == NULL )
1222 {
1223 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1224 goto exit;
1225 }
1226 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1227 if( ret != 0 )
1228 goto exit;
1229 attributes->domain_parameters = buffer;
1230 attributes->domain_parameters_size = buflen;
1231
1232exit:
1233 mbedtls_mpi_free( &mpi );
1234 if( ret != 0 )
1235 mbedtls_free( buffer );
1236 return( mbedtls_to_psa_error( ret ) );
1237}
John Durkop07cc04a2020-11-16 22:08:34 -08001238#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001239 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001240
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001241/** Retrieve all the publicly-accessible attributes of a key.
1242 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001243psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001244 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001245{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001247 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001248 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001249
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001250 psa_reset_key_attributes( attributes );
1251
Ronald Cron5c522922020-11-14 16:35:34 +01001252 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001253 if( status != PSA_SUCCESS )
1254 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001255
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001256 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001257 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1258 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1259
1260#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1261 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001262 psa_set_key_slot_number( attributes,
1263 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001264#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001265
Gilles Peskine8e338702019-07-30 20:06:31 +02001266 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001267 {
John Durkop07cc04a2020-11-16 22:08:34 -08001268#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001269 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001270 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001271 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001272#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001273 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001274 * is not yet implemented.
1275 * https://github.com/ARMmbed/mbed-crypto/issues/216
1276 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001277 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001278 break;
1279#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001280 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001281 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001282
Ronald Cron90857082020-11-25 14:58:33 +01001283 status = mbedtls_psa_rsa_load_representation(
1284 slot->attr.type,
1285 slot->key.data,
1286 slot->key.bytes,
1287 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001288 if( status != PSA_SUCCESS )
1289 break;
1290
Steven Cooremana2371e52020-07-28 14:30:39 +02001291 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001292 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001293 mbedtls_rsa_free( rsa );
1294 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001295 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001296 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001297#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001298 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001299 default:
1300 /* Nothing else to do. */
1301 break;
1302 }
1303
1304 if( status != PSA_SUCCESS )
1305 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001306
Ronald Cron5c522922020-11-14 16:35:34 +01001307 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001308
Ronald Cron5c522922020-11-14 16:35:34 +01001309 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001310}
1311
Gilles Peskinec8000c02019-08-02 20:15:51 +02001312#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1313psa_status_t psa_get_key_slot_number(
1314 const psa_key_attributes_t *attributes,
1315 psa_key_slot_number_t *slot_number )
1316{
1317 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1318 {
1319 *slot_number = attributes->slot_number;
1320 return( PSA_SUCCESS );
1321 }
1322 else
1323 return( PSA_ERROR_INVALID_ARGUMENT );
1324}
1325#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1326
Ronald Crond18b5f82020-11-25 16:46:05 +01001327static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1328 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001329 uint8_t *data,
1330 size_t data_size,
1331 size_t *data_length )
1332{
Ronald Crond18b5f82020-11-25 16:46:05 +01001333 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001334 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001335 memcpy( data, key_buffer, key_buffer_size );
1336 memset( data + key_buffer_size, 0,
1337 data_size - key_buffer_size );
1338 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001339 return( PSA_SUCCESS );
1340}
1341
Ronald Cron7285cda2020-11-26 14:46:37 +01001342psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001343 const psa_key_attributes_t *attributes,
1344 const uint8_t *key_buffer, size_t key_buffer_size,
1345 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001346{
Ronald Crond18b5f82020-11-25 16:46:05 +01001347 psa_key_type_t type = attributes->core.type;
1348
Ronald Crond18b5f82020-11-25 16:46:05 +01001349 if( key_type_is_raw_bytes( type ) ||
1350 PSA_KEY_TYPE_IS_RSA( type ) ||
1351 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001352 {
1353 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001354 key_buffer, key_buffer_size,
1355 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001356 }
1357 else
1358 {
1359 /* This shouldn't happen in the reference implementation, but
1360 it is valid for a special-purpose implementation to omit
1361 support for exporting certain key types. */
1362 return( PSA_ERROR_NOT_SUPPORTED );
1363 }
1364}
1365
1366psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1367 uint8_t *data,
1368 size_t data_size,
1369 size_t *data_length )
1370{
1371 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1372 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1373 psa_key_slot_t *slot;
1374
Ronald Crone907e552021-01-18 13:22:38 +01001375 /* Reject a zero-length output buffer now, since this can never be a
1376 * valid key representation. This way we know that data must be a valid
1377 * pointer and we can do things like memset(data, ..., data_size). */
1378 if( data_size == 0 )
1379 return( PSA_ERROR_BUFFER_TOO_SMALL );
1380
Ronald Cron9486f9d2020-11-26 13:36:23 +01001381 /* Set the key to empty now, so that even when there are errors, we always
1382 * set data_length to a value between 0 and data_size. On error, setting
1383 * the key to empty is a good choice because an empty key representation is
1384 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001385 *data_length = 0;
1386
Ronald Cron9486f9d2020-11-26 13:36:23 +01001387 /* Export requires the EXPORT flag. There is an exception for public keys,
1388 * which don't require any flag, but
1389 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1390 */
1391 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1392 PSA_KEY_USAGE_EXPORT, 0 );
1393 if( status != PSA_SUCCESS )
1394 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001395
Ronald Crond18b5f82020-11-25 16:46:05 +01001396 psa_key_attributes_t attributes = {
1397 .core = slot->attr
1398 };
Ronald Cron67227982020-11-26 15:16:05 +01001399 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001400 slot->key.data, slot->key.bytes,
1401 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001402
Ronald Cron9486f9d2020-11-26 13:36:23 +01001403 unlock_status = psa_unlock_key_slot( slot );
1404
1405 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1406}
1407
Ronald Cron7285cda2020-11-26 14:46:37 +01001408psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001409 const psa_key_attributes_t *attributes,
1410 const uint8_t *key_buffer,
1411 size_t key_buffer_size,
1412 uint8_t *data,
1413 size_t data_size,
1414 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001415{
Ronald Crond18b5f82020-11-25 16:46:05 +01001416 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001417
Ronald Crond18b5f82020-11-25 16:46:05 +01001418 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001419 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001420 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001421 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001422 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001423 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001424 key_buffer, key_buffer_size,
1425 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001426 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001427
Ronald Crond18b5f82020-11-25 16:46:05 +01001428 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001429 {
John Durkop0e005192020-10-31 22:06:54 -07001430#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1431 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001432 return( mbedtls_psa_rsa_export_public_key( attributes,
1433 key_buffer,
1434 key_buffer_size,
1435 data,
1436 data_size,
1437 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001438#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001439 /* We don't know how to convert a private RSA key to public. */
1440 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001441#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1442 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001443 }
1444 else
Moran Pekera998bc62018-04-16 18:16:20 +03001445 {
John Durkop6ba40d12020-11-10 08:50:04 -08001446#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1447 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001448 return( mbedtls_psa_ecp_export_public_key( attributes,
1449 key_buffer,
1450 key_buffer_size,
1451 data,
1452 data_size,
1453 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001454#else
1455 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001456 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001457#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1458 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001459 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001460 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001461 else
1462 {
1463 /* This shouldn't happen in the reference implementation, but
1464 it is valid for a special-purpose implementation to omit
1465 support for exporting certain key types. */
1466 return( PSA_ERROR_NOT_SUPPORTED );
1467 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001468}
Ronald Crond18b5f82020-11-25 16:46:05 +01001469
Ronald Croncf56a0a2020-08-04 09:51:30 +02001470psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001471 uint8_t *data,
1472 size_t data_size,
1473 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001474{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001475 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001476 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001477 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001478
Ronald Crone907e552021-01-18 13:22:38 +01001479 /* Reject a zero-length output buffer now, since this can never be a
1480 * valid key representation. This way we know that data must be a valid
1481 * pointer and we can do things like memset(data, ..., data_size). */
1482 if( data_size == 0 )
1483 return( PSA_ERROR_BUFFER_TOO_SMALL );
1484
Darryl Greendd8fb772018-11-07 16:00:44 +00001485 /* Set the key to empty now, so that even when there are errors, we always
1486 * set data_length to a value between 0 and data_size. On error, setting
1487 * the key to empty is a good choice because an empty key representation is
1488 * unlikely to be accepted anywhere. */
1489 *data_length = 0;
1490
1491 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001492 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001493 if( status != PSA_SUCCESS )
1494 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001495
Ronald Cron9486f9d2020-11-26 13:36:23 +01001496 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1497 {
1498 status = PSA_ERROR_INVALID_ARGUMENT;
1499 goto exit;
1500 }
1501
Ronald Crond18b5f82020-11-25 16:46:05 +01001502 psa_key_attributes_t attributes = {
1503 .core = slot->attr
1504 };
Ronald Cron67227982020-11-26 15:16:05 +01001505 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001506 &attributes, slot->key.data, slot->key.bytes,
1507 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508
1509exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001510 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001511
Ronald Cron5c522922020-11-14 16:35:34 +01001512 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001513}
1514
Gilles Peskine91e8c332019-08-02 19:19:39 +02001515#if defined(static_assert)
1516static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1517 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001518static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001519 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001520static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001521 "One or more key attribute flag is listed as both internal-only and external-only" );
1522#endif
1523
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001524/** Validate that a key policy is internally well-formed.
1525 *
1526 * This function only rejects invalid policies. It does not validate the
1527 * consistency of the policy with respect to other attributes of the key
1528 * such as the key type.
1529 */
1530static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001531{
1532 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001533 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001534 PSA_KEY_USAGE_ENCRYPT |
1535 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001536 PSA_KEY_USAGE_SIGN_HASH |
1537 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001538 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1539 return( PSA_ERROR_INVALID_ARGUMENT );
1540
Gilles Peskine4747d192019-04-17 15:05:45 +02001541 return( PSA_SUCCESS );
1542}
1543
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001544/** Validate the internal consistency of key attributes.
1545 *
1546 * This function only rejects invalid attribute values. If does not
1547 * validate the consistency of the attributes with any key data that may
1548 * be involved in the creation of the key.
1549 *
1550 * Call this function early in the key creation process.
1551 *
1552 * \param[in] attributes Key attributes for the new key.
1553 * \param[out] p_drv On any return, the driver for the key, if any.
1554 * NULL for a transparent key.
1555 *
1556 */
1557static psa_status_t psa_validate_key_attributes(
1558 const psa_key_attributes_t *attributes,
1559 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001560{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001561 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001562 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001563 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001564
Ronald Cron54b90082020-10-29 15:26:43 +01001565 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001566 if( status != PSA_SUCCESS )
1567 return( status );
1568
Ronald Crond2ed4812020-07-17 16:11:30 +02001569 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001570 if( status != PSA_SUCCESS )
1571 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001572
Ronald Cron65f38a32020-10-23 17:11:13 +02001573 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1574 {
1575 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1576 return( PSA_ERROR_INVALID_ARGUMENT );
1577 }
1578 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001579 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001580 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001581 if( status != PSA_SUCCESS )
1582 return( status );
1583 }
1584
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001585 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001586 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001587 return( status );
1588
1589 /* Refuse to create overly large keys.
1590 * Note that this doesn't trigger on import if the attributes don't
1591 * explicitly specify a size (so psa_get_key_bits returns 0), so
1592 * psa_import_key() needs its own checks. */
1593 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1594 return( PSA_ERROR_NOT_SUPPORTED );
1595
Gilles Peskine91e8c332019-08-02 19:19:39 +02001596 /* Reject invalid flags. These should not be reachable through the API. */
1597 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1598 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1599 return( PSA_ERROR_INVALID_ARGUMENT );
1600
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001601 return( PSA_SUCCESS );
1602}
1603
Gilles Peskine4747d192019-04-17 15:05:45 +02001604/** Prepare a key slot to receive key material.
1605 *
1606 * This function allocates a key slot and sets its metadata.
1607 *
1608 * If this function fails, call psa_fail_key_creation().
1609 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001610 * This function is intended to be used as follows:
1611 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001612 * it with the specified attributes, and in case of a volatile key assign it
1613 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001614 * -# Populate the slot with the key material.
1615 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1616 * In case of failure at any step, stop the sequence and call
1617 * psa_fail_key_creation().
1618 *
Ronald Cron5c522922020-11-14 16:35:34 +01001619 * On success, the key slot is locked. It is the responsibility of the caller
1620 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001621 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001622 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001623 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001624 * \param[out] p_slot On success, a pointer to the prepared slot.
1625 * \param[out] p_drv On any return, the driver for the key, if any.
1626 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001627 *
1628 * \retval #PSA_SUCCESS
1629 * The key slot is ready to receive key material.
1630 * \return If this function fails, the key slot is an invalid state.
1631 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001632 */
1633static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001634 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001636 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001637 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001638{
1639 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001640 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001641 psa_key_slot_t *slot;
1642
Gilles Peskinedf179142019-07-15 22:02:14 +02001643 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001644 *p_drv = NULL;
1645
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001646 status = psa_validate_key_attributes( attributes, p_drv );
1647 if( status != PSA_SUCCESS )
1648 return( status );
1649
Ronald Cronc4d1b512020-07-31 11:26:37 +02001650 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 if( status != PSA_SUCCESS )
1652 return( status );
1653 slot = *p_slot;
1654
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001655 /* We're storing the declared bit-size of the key. It's up to each
1656 * creation mechanism to verify that this information is correct.
1657 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001658 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001659 * is optional (import, copy). In case of a volatile key, assign it the
1660 * volatile key identifier associated to the slot returned to contain its
1661 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001662
1663 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001664 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1665 {
1666#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1667 slot->attr.id = volatile_key_id;
1668#else
1669 slot->attr.id.key_id = volatile_key_id;
1670#endif
1671 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001672
Gilles Peskine91e8c332019-08-02 19:19:39 +02001673 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001674 * external-only flags, query `attributes`. Thanks to the check
1675 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001676 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001677 * may have set. */
1678 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001679
Gilles Peskinecbaff462019-07-12 23:46:04 +02001680#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001681 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001682 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001683 * create the key file in internal storage, create the
1684 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001685 * persistent data. This is done by starting a transaction that will
1686 * encompass these three actions.
1687 * For registering a volatile key, we just need to find an appropriate
1688 * slot number inside the SE. Since the key is designated volatile, creating
1689 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001690 /* The first thing to do is to find a slot number for the new key.
1691 * We save the slot number in persistent storage as part of the
1692 * transaction data. It will be needed to recover if the power
1693 * fails during the key creation process, to clean up on the secure
1694 * element side after restarting. Obtaining a slot number from the
1695 * secure element driver updates its persistent state, but we do not yet
1696 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001697 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001698 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001699 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001700 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001701 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001702 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001703 if( status != PSA_SUCCESS )
1704 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001705
1706 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001707 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001708 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1709 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001710 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001711 psa_crypto_transaction.key.id = slot->attr.id;
1712 status = psa_crypto_save_transaction( );
1713 if( status != PSA_SUCCESS )
1714 {
1715 (void) psa_crypto_stop_transaction( );
1716 return( status );
1717 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001718 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001719
1720 status = psa_copy_key_material_into_slot(
1721 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001722 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001723
1724 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1725 {
1726 /* Key registration only makes sense with a secure element. */
1727 return( PSA_ERROR_INVALID_ARGUMENT );
1728 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001729#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1730
Ronald Cronc4d1b512020-07-31 11:26:37 +02001731 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001732}
Gilles Peskine4747d192019-04-17 15:05:45 +02001733
1734/** Finalize the creation of a key once its key material has been set.
1735 *
1736 * This entails writing the key to persistent storage.
1737 *
1738 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001739 * See the documentation of psa_start_key_creation() for the intended use
1740 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001741 *
Ronald Cron5c522922020-11-14 16:35:34 +01001742 * If the finalization succeeds, the function unlocks the key slot (it was
1743 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1744 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001745 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001746 * \param[in,out] slot Pointer to the slot with key material.
1747 * \param[in] driver The secure element driver for the key,
1748 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001749 * \param[out] key On success, identifier of the key. Note that the
1750 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001751 *
1752 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001753 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001754 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1755 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1756 * \retval #PSA_ERROR_ALREADY_EXISTS
1757 * \retval #PSA_ERROR_DATA_INVALID
1758 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001759 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001760 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001761 * \return If this function fails, the key slot is an invalid state.
1762 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001764static psa_status_t psa_finish_key_creation(
1765 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001766 psa_se_drv_table_entry_t *driver,
1767 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001768{
1769 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001770 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001771 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001772
1773#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001774 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001775 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001776#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1777 if( driver != NULL )
1778 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001779 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001780 psa_key_slot_number_t slot_number =
1781 psa_key_slot_get_slot_number( slot ) ;
1782
Gilles Peskineb46bef22019-07-30 21:32:04 +02001783#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001784 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001785 sizeof( data.slot_number ),
1786 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001787#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001788 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001789 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001790 (uint8_t*) &data,
1791 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001792 }
1793 else
1794#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1795 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001796 /* Key material is saved in export representation in the slot, so
1797 * just pass the slot buffer for storage. */
1798 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001799 slot->key.data,
1800 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001801 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001802 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001803#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1804
Gilles Peskinecbaff462019-07-12 23:46:04 +02001805#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001806 /* Finish the transaction for a key creation. This does not
1807 * happen when registering an existing key. Detect this case
1808 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001809 * creation of a persistent key in a secure element requires a transaction,
1810 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001811 if( driver != NULL &&
1812 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001813 {
1814 status = psa_save_se_persistent_data( driver );
1815 if( status != PSA_SUCCESS )
1816 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001817 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001818 return( status );
1819 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001820 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001821 }
1822#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1823
Ronald Cron50972942020-11-14 11:28:25 +01001824 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001825 {
1826 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001827 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001828 if( status != PSA_SUCCESS )
1829 *key = MBEDTLS_SVC_KEY_ID_INIT;
1830 }
Ronald Cron50972942020-11-14 11:28:25 +01001831
Gilles Peskine4747d192019-04-17 15:05:45 +02001832 return( status );
1833}
1834
1835/** Abort the creation of a key.
1836 *
1837 * You may call this function after calling psa_start_key_creation(),
1838 * or after psa_finish_key_creation() fails. In other circumstances, this
1839 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001840 * See the documentation of psa_start_key_creation() for the intended use
1841 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001842 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001843 * \param[in,out] slot Pointer to the slot with key material.
1844 * \param[in] driver The secure element driver for the key,
1845 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001846 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001847static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001848 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001849{
Gilles Peskine011e4282019-06-26 18:34:38 +02001850 (void) driver;
1851
Gilles Peskine4747d192019-04-17 15:05:45 +02001852 if( slot == NULL )
1853 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001854
1855#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001856 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001857 * element, and the failure happened later (when saving metadata
1858 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001859 * element.
1860 * https://github.com/ARMmbed/mbed-crypto/issues/217
1861 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001862
Gilles Peskined7729582019-08-05 15:55:54 +02001863 /* Abort the ongoing transaction if any (there may not be one if
1864 * the creation process failed before starting one, or if the
1865 * key creation is a registration of a key in a secure element).
1866 * Earlier functions must already have done what it takes to undo any
1867 * partial creation. All that's left is to update the transaction data
1868 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001869 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001870#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1871
Gilles Peskine4747d192019-04-17 15:05:45 +02001872 psa_wipe_key_slot( slot );
1873}
1874
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001875/** Validate optional attributes during key creation.
1876 *
1877 * Some key attributes are optional during key creation. If they are
1878 * specified in the attributes structure, check that they are consistent
1879 * with the data in the slot.
1880 *
1881 * This function should be called near the end of key creation, after
1882 * the slot in memory is fully populated but before saving persistent data.
1883 */
1884static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001885 const psa_key_slot_t *slot,
1886 const psa_key_attributes_t *attributes )
1887{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001888 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001889 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001890 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001891 return( PSA_ERROR_INVALID_ARGUMENT );
1892 }
1893
1894 if( attributes->domain_parameters_size != 0 )
1895 {
John Durkop0e005192020-10-31 22:06:54 -07001896#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1897 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001898 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001900 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001901 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001903
Ronald Cron90857082020-11-25 14:58:33 +01001904 psa_status_t status = mbedtls_psa_rsa_load_representation(
1905 slot->attr.type,
1906 slot->key.data,
1907 slot->key.bytes,
1908 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001909 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001910 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001911
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001912 mbedtls_mpi_init( &actual );
1913 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001914 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001915 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001916 mbedtls_rsa_free( rsa );
1917 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001918 if( ret != 0 )
1919 goto rsa_exit;
1920 ret = mbedtls_mpi_read_binary( &required,
1921 attributes->domain_parameters,
1922 attributes->domain_parameters_size );
1923 if( ret != 0 )
1924 goto rsa_exit;
1925 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1926 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1927 rsa_exit:
1928 mbedtls_mpi_free( &actual );
1929 mbedtls_mpi_free( &required );
1930 if( ret != 0)
1931 return( mbedtls_to_psa_error( ret ) );
1932 }
1933 else
John Durkop9814fa22020-11-04 12:28:15 -08001934#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1935 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001936 {
1937 return( PSA_ERROR_INVALID_ARGUMENT );
1938 }
1939 }
1940
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001941 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001942 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001943 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001944 return( PSA_ERROR_INVALID_ARGUMENT );
1945 }
1946
1947 return( PSA_SUCCESS );
1948}
1949
Gilles Peskine4747d192019-04-17 15:05:45 +02001950psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001951 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001952 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001953 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001954{
1955 psa_status_t status;
1956 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001957 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001958 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001959
Ronald Cron81709fc2020-11-14 12:10:32 +01001960 *key = MBEDTLS_SVC_KEY_ID_INIT;
1961
Gilles Peskine0f84d622019-09-12 19:03:13 +02001962 /* Reject zero-length symmetric keys (including raw data key objects).
1963 * This also rejects any key which might be encoded as an empty string,
1964 * which is never valid. */
1965 if( data_length == 0 )
1966 return( PSA_ERROR_INVALID_ARGUMENT );
1967
Gilles Peskinedf179142019-07-15 22:02:14 +02001968 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001969 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001970 if( status != PSA_SUCCESS )
1971 goto exit;
1972
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001973 /* In the case of a transparent key or an opaque key stored in local
1974 * storage (thus not in the case of generating a key in a secure element
1975 * or cryptoprocessor with storage), we have to allocate a buffer to
1976 * hold the generated key material. */
1977 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001978 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001979 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001980 if( status != PSA_SUCCESS )
1981 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001982 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001983
1984 bits = slot->attr.bits;
1985 status = psa_driver_wrapper_import_key( attributes,
1986 data, data_length,
1987 slot->key.data,
1988 slot->key.bytes,
1989 &slot->key.bytes, &bits );
1990 if( status != PSA_SUCCESS )
1991 goto exit;
1992
1993 if( slot->attr.bits == 0 )
1994 slot->attr.bits = (psa_key_bits_t) bits;
1995 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001996 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001997 status = PSA_ERROR_INVALID_ARGUMENT;
1998 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001999 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002000
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002001 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002002 if( status != PSA_SUCCESS )
2003 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004
Ronald Cron81709fc2020-11-14 12:10:32 +01002005 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002006exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002007 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002008 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002009
Gilles Peskine4747d192019-04-17 15:05:45 +02002010 return( status );
2011}
2012
Gilles Peskined7729582019-08-05 15:55:54 +02002013#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2014psa_status_t mbedtls_psa_register_se_key(
2015 const psa_key_attributes_t *attributes )
2016{
2017 psa_status_t status;
2018 psa_key_slot_t *slot = NULL;
2019 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002020 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002021
2022 /* Leaving attributes unspecified is not currently supported.
2023 * It could make sense to query the key type and size from the
2024 * secure element, but not all secure elements support this
2025 * and the driver HAL doesn't currently support it. */
2026 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2027 return( PSA_ERROR_NOT_SUPPORTED );
2028 if( psa_get_key_bits( attributes ) == 0 )
2029 return( PSA_ERROR_NOT_SUPPORTED );
2030
2031 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002032 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002033 if( status != PSA_SUCCESS )
2034 goto exit;
2035
Ronald Cron81709fc2020-11-14 12:10:32 +01002036 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002037
2038exit:
2039 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002040 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002041
Gilles Peskined7729582019-08-05 15:55:54 +02002042 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002043 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002044 return( status );
2045}
2046#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2047
Gilles Peskinef603c712019-01-19 13:40:11 +01002048static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002049 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002050{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002051 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002052 source->key.data,
2053 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002054 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002055 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002056
Steven Cooreman398aee52020-10-13 14:35:45 +02002057 target->attr.type = source->attr.type;
2058 target->attr.bits = source->attr.bits;
2059
2060 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002061}
2062
Ronald Croncf56a0a2020-08-04 09:51:30 +02002063psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002064 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002065 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002066{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002067 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002068 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002069 psa_key_slot_t *source_slot = NULL;
2070 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002071 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002072 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002073
Ronald Cron81709fc2020-11-14 12:10:32 +01002074 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2075
Ronald Cron5c522922020-11-14 16:35:34 +01002076 status = psa_get_and_lock_transparent_key_slot_with_policy(
2077 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002078 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002079 goto exit;
2080
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002081 status = psa_validate_optional_attributes( source_slot,
2082 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002083 if( status != PSA_SUCCESS )
2084 goto exit;
2085
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002086 status = psa_restrict_key_policy( source_slot->attr.type,
2087 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002088 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002089 if( status != PSA_SUCCESS )
2090 goto exit;
2091
Ronald Cron81709fc2020-11-14 12:10:32 +01002092 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2093 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094 if( status != PSA_SUCCESS )
2095 goto exit;
2096
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002097#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2098 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002099 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002100 /* Copying to a secure element is not implemented yet. */
2101 status = PSA_ERROR_NOT_SUPPORTED;
2102 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002103 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002104#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002105
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002106 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002107 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002108 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002109
Ronald Cron81709fc2020-11-14 12:10:32 +01002110 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002111exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002112 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002113 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002114
Ronald Cron5c522922020-11-14 16:35:34 +01002115 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002116
Ronald Cron5c522922020-11-14 16:35:34 +01002117 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002118}
2119
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002120
2121
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002122/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002123/* Message digests */
2124/****************************************************************/
2125
John Durkop07cc04a2020-11-16 22:08:34 -08002126#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002127 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2128 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002129 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Ronald Cron36f641b2021-02-16 17:20:43 +01002130const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002131{
2132 switch( alg )
2133 {
John Durkopee4e6602020-11-27 08:48:46 -08002134#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002135 case PSA_ALG_MD2:
2136 return( &mbedtls_md2_info );
2137#endif
John Durkopee4e6602020-11-27 08:48:46 -08002138#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002139 case PSA_ALG_MD4:
2140 return( &mbedtls_md4_info );
2141#endif
John Durkopee4e6602020-11-27 08:48:46 -08002142#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002143 case PSA_ALG_MD5:
2144 return( &mbedtls_md5_info );
2145#endif
John Durkopee4e6602020-11-27 08:48:46 -08002146#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002147 case PSA_ALG_RIPEMD160:
2148 return( &mbedtls_ripemd160_info );
2149#endif
John Durkopee4e6602020-11-27 08:48:46 -08002150#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002151 case PSA_ALG_SHA_1:
2152 return( &mbedtls_sha1_info );
2153#endif
John Durkopee4e6602020-11-27 08:48:46 -08002154#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002155 case PSA_ALG_SHA_224:
2156 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002157#endif
2158#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002159 case PSA_ALG_SHA_256:
2160 return( &mbedtls_sha256_info );
2161#endif
John Durkopee4e6602020-11-27 08:48:46 -08002162#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002163 case PSA_ALG_SHA_384:
2164 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002165#endif
John Durkopee4e6602020-11-27 08:48:46 -08002166#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002167 case PSA_ALG_SHA_512:
2168 return( &mbedtls_sha512_info );
2169#endif
2170 default:
2171 return( NULL );
2172 }
2173}
John Durkop07cc04a2020-11-16 22:08:34 -08002174#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002175 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2176 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2177 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002178
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002179psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2180{
2181 switch( operation->alg )
2182 {
Gilles Peskine81736312018-06-26 15:04:31 +02002183 case 0:
2184 /* The object has (apparently) been initialized but it is not
2185 * in use. It's ok to call abort on such an object, and there's
2186 * nothing to do. */
2187 break;
John Durkopee4e6602020-11-27 08:48:46 -08002188#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189 case PSA_ALG_MD2:
2190 mbedtls_md2_free( &operation->ctx.md2 );
2191 break;
2192#endif
John Durkopee4e6602020-11-27 08:48:46 -08002193#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194 case PSA_ALG_MD4:
2195 mbedtls_md4_free( &operation->ctx.md4 );
2196 break;
2197#endif
John Durkopee4e6602020-11-27 08:48:46 -08002198#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002199 case PSA_ALG_MD5:
2200 mbedtls_md5_free( &operation->ctx.md5 );
2201 break;
2202#endif
John Durkopee4e6602020-11-27 08:48:46 -08002203#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002204 case PSA_ALG_RIPEMD160:
2205 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2206 break;
2207#endif
John Durkopee4e6602020-11-27 08:48:46 -08002208#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002209 case PSA_ALG_SHA_1:
2210 mbedtls_sha1_free( &operation->ctx.sha1 );
2211 break;
2212#endif
John Durkop6ca23272020-12-03 06:01:32 -08002213#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002214 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002215 mbedtls_sha256_free( &operation->ctx.sha256 );
2216 break;
2217#endif
2218#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002219 case PSA_ALG_SHA_256:
2220 mbedtls_sha256_free( &operation->ctx.sha256 );
2221 break;
2222#endif
John Durkop6ca23272020-12-03 06:01:32 -08002223#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002225 mbedtls_sha512_free( &operation->ctx.sha512 );
2226 break;
2227#endif
2228#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002229 case PSA_ALG_SHA_512:
2230 mbedtls_sha512_free( &operation->ctx.sha512 );
2231 break;
2232#endif
2233 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002234 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002235 }
2236 operation->alg = 0;
2237 return( PSA_SUCCESS );
2238}
2239
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002240psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241 psa_algorithm_t alg )
2242{
Janos Follath24eed8d2019-11-22 13:21:35 +00002243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002244
2245 /* A context must be freshly initialized before it can be set up. */
2246 if( operation->alg != 0 )
2247 {
2248 return( PSA_ERROR_BAD_STATE );
2249 }
2250
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251 switch( alg )
2252 {
John Durkopee4e6602020-11-27 08:48:46 -08002253#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254 case PSA_ALG_MD2:
2255 mbedtls_md2_init( &operation->ctx.md2 );
2256 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2257 break;
2258#endif
John Durkopee4e6602020-11-27 08:48:46 -08002259#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 case PSA_ALG_MD4:
2261 mbedtls_md4_init( &operation->ctx.md4 );
2262 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2263 break;
2264#endif
John Durkopee4e6602020-11-27 08:48:46 -08002265#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266 case PSA_ALG_MD5:
2267 mbedtls_md5_init( &operation->ctx.md5 );
2268 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2269 break;
2270#endif
John Durkopee4e6602020-11-27 08:48:46 -08002271#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272 case PSA_ALG_RIPEMD160:
2273 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2274 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2275 break;
2276#endif
John Durkopee4e6602020-11-27 08:48:46 -08002277#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278 case PSA_ALG_SHA_1:
2279 mbedtls_sha1_init( &operation->ctx.sha1 );
2280 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2281 break;
2282#endif
John Durkopee4e6602020-11-27 08:48:46 -08002283#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284 case PSA_ALG_SHA_224:
2285 mbedtls_sha256_init( &operation->ctx.sha256 );
2286 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2287 break;
John Durkopee4e6602020-11-27 08:48:46 -08002288#endif
2289#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290 case PSA_ALG_SHA_256:
2291 mbedtls_sha256_init( &operation->ctx.sha256 );
2292 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2293 break;
2294#endif
John Durkopee4e6602020-11-27 08:48:46 -08002295#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002296 case PSA_ALG_SHA_384:
2297 mbedtls_sha512_init( &operation->ctx.sha512 );
2298 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2299 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002300#endif
John Durkopee4e6602020-11-27 08:48:46 -08002301#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002302 case PSA_ALG_SHA_512:
2303 mbedtls_sha512_init( &operation->ctx.sha512 );
2304 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2305 break;
2306#endif
2307 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002308 return( PSA_ALG_IS_HASH( alg ) ?
2309 PSA_ERROR_NOT_SUPPORTED :
2310 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311 }
2312 if( ret == 0 )
2313 operation->alg = alg;
2314 else
2315 psa_hash_abort( operation );
2316 return( mbedtls_to_psa_error( ret ) );
2317}
2318
2319psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2320 const uint8_t *input,
2321 size_t input_length )
2322{
Janos Follath24eed8d2019-11-22 13:21:35 +00002323 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002324
2325 /* Don't require hash implementations to behave correctly on a
2326 * zero-length input, which may have an invalid pointer. */
2327 if( input_length == 0 )
2328 return( PSA_SUCCESS );
2329
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330 switch( operation->alg )
2331 {
John Durkopee4e6602020-11-27 08:48:46 -08002332#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333 case PSA_ALG_MD2:
2334 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2335 input, input_length );
2336 break;
2337#endif
John Durkopee4e6602020-11-27 08:48:46 -08002338#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 case PSA_ALG_MD4:
2340 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2341 input, input_length );
2342 break;
2343#endif
John Durkopee4e6602020-11-27 08:48:46 -08002344#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 case PSA_ALG_MD5:
2346 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2347 input, input_length );
2348 break;
2349#endif
John Durkopee4e6602020-11-27 08:48:46 -08002350#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351 case PSA_ALG_RIPEMD160:
2352 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2353 input, input_length );
2354 break;
2355#endif
John Durkopee4e6602020-11-27 08:48:46 -08002356#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002357 case PSA_ALG_SHA_1:
2358 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2359 input, input_length );
2360 break;
2361#endif
John Durkop6ca23272020-12-03 06:01:32 -08002362#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002363 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002364 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2365 input, input_length );
2366 break;
2367#endif
2368#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002369 case PSA_ALG_SHA_256:
2370 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2371 input, input_length );
2372 break;
2373#endif
John Durkop6ca23272020-12-03 06:01:32 -08002374#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002376 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2377 input, input_length );
2378 break;
2379#endif
2380#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381 case PSA_ALG_SHA_512:
2382 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2383 input, input_length );
2384 break;
2385#endif
2386 default:
John Durkopee4e6602020-11-27 08:48:46 -08002387 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002388 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002390
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002391 if( ret != 0 )
2392 psa_hash_abort( operation );
2393 return( mbedtls_to_psa_error( ret ) );
2394}
2395
2396psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2397 uint8_t *hash,
2398 size_t hash_size,
2399 size_t *hash_length )
2400{
itayzafrir40835d42018-08-02 13:14:17 +03002401 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002403 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002404
2405 /* Fill the output buffer with something that isn't a valid hash
2406 * (barring an attack on the hash and deliberately-crafted input),
2407 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002408 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002409 /* If hash_size is 0 then hash may be NULL and then the
2410 * call to memset would have undefined behavior. */
2411 if( hash_size != 0 )
2412 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002413
2414 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002415 {
2416 status = PSA_ERROR_BUFFER_TOO_SMALL;
2417 goto exit;
2418 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002419
2420 switch( operation->alg )
2421 {
John Durkopee4e6602020-11-27 08:48:46 -08002422#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002423 case PSA_ALG_MD2:
2424 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2425 break;
2426#endif
John Durkopee4e6602020-11-27 08:48:46 -08002427#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002428 case PSA_ALG_MD4:
2429 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2430 break;
2431#endif
John Durkopee4e6602020-11-27 08:48:46 -08002432#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002433 case PSA_ALG_MD5:
2434 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2435 break;
2436#endif
John Durkopee4e6602020-11-27 08:48:46 -08002437#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002438 case PSA_ALG_RIPEMD160:
2439 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2440 break;
2441#endif
John Durkopee4e6602020-11-27 08:48:46 -08002442#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002443 case PSA_ALG_SHA_1:
2444 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2445 break;
2446#endif
John Durkop6ca23272020-12-03 06:01:32 -08002447#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002448 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002449 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2450 break;
2451#endif
2452#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002453 case PSA_ALG_SHA_256:
2454 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2455 break;
2456#endif
John Durkop6ca23272020-12-03 06:01:32 -08002457#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002458 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002459 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2460 break;
2461#endif
2462#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002463 case PSA_ALG_SHA_512:
2464 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2465 break;
2466#endif
2467 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002468 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002469 }
itayzafrir40835d42018-08-02 13:14:17 +03002470 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002471
itayzafrir40835d42018-08-02 13:14:17 +03002472exit:
2473 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002474 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002475 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002476 return( psa_hash_abort( operation ) );
2477 }
2478 else
2479 {
2480 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002481 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002482 }
2483}
2484
Gilles Peskine2d277862018-06-18 15:41:12 +02002485psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2486 const uint8_t *hash,
2487 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488{
2489 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2490 size_t actual_hash_length;
2491 psa_status_t status = psa_hash_finish( operation,
2492 actual_hash, sizeof( actual_hash ),
2493 &actual_hash_length );
2494 if( status != PSA_SUCCESS )
2495 return( status );
2496 if( actual_hash_length != hash_length )
2497 return( PSA_ERROR_INVALID_SIGNATURE );
2498 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2499 return( PSA_ERROR_INVALID_SIGNATURE );
2500 return( PSA_SUCCESS );
2501}
2502
Gilles Peskine0a749c82019-11-28 19:33:58 +01002503psa_status_t psa_hash_compute( psa_algorithm_t alg,
2504 const uint8_t *input, size_t input_length,
2505 uint8_t *hash, size_t hash_size,
2506 size_t *hash_length )
2507{
2508 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2509 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2510
2511 *hash_length = hash_size;
2512 status = psa_hash_setup( &operation, alg );
2513 if( status != PSA_SUCCESS )
2514 goto exit;
2515 status = psa_hash_update( &operation, input, input_length );
2516 if( status != PSA_SUCCESS )
2517 goto exit;
2518 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2519 if( status != PSA_SUCCESS )
2520 goto exit;
2521
2522exit:
2523 if( status == PSA_SUCCESS )
2524 status = psa_hash_abort( &operation );
2525 else
2526 psa_hash_abort( &operation );
2527 return( status );
2528}
2529
2530psa_status_t psa_hash_compare( psa_algorithm_t alg,
2531 const uint8_t *input, size_t input_length,
2532 const uint8_t *hash, size_t hash_length )
2533{
2534 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2535 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2536
2537 status = psa_hash_setup( &operation, alg );
2538 if( status != PSA_SUCCESS )
2539 goto exit;
2540 status = psa_hash_update( &operation, input, input_length );
2541 if( status != PSA_SUCCESS )
2542 goto exit;
2543 status = psa_hash_verify( &operation, hash, hash_length );
2544 if( status != PSA_SUCCESS )
2545 goto exit;
2546
2547exit:
2548 if( status == PSA_SUCCESS )
2549 status = psa_hash_abort( &operation );
2550 else
2551 psa_hash_abort( &operation );
2552 return( status );
2553}
2554
Gilles Peskineeb35d782019-01-22 17:56:16 +01002555psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2556 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002557{
2558 if( target_operation->alg != 0 )
2559 return( PSA_ERROR_BAD_STATE );
2560
2561 switch( source_operation->alg )
2562 {
2563 case 0:
2564 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002565#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002566 case PSA_ALG_MD2:
2567 mbedtls_md2_clone( &target_operation->ctx.md2,
2568 &source_operation->ctx.md2 );
2569 break;
2570#endif
John Durkopee4e6602020-11-27 08:48:46 -08002571#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002572 case PSA_ALG_MD4:
2573 mbedtls_md4_clone( &target_operation->ctx.md4,
2574 &source_operation->ctx.md4 );
2575 break;
2576#endif
John Durkopee4e6602020-11-27 08:48:46 -08002577#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002578 case PSA_ALG_MD5:
2579 mbedtls_md5_clone( &target_operation->ctx.md5,
2580 &source_operation->ctx.md5 );
2581 break;
2582#endif
John Durkopee4e6602020-11-27 08:48:46 -08002583#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002584 case PSA_ALG_RIPEMD160:
2585 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2586 &source_operation->ctx.ripemd160 );
2587 break;
2588#endif
John Durkopee4e6602020-11-27 08:48:46 -08002589#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002590 case PSA_ALG_SHA_1:
2591 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2592 &source_operation->ctx.sha1 );
2593 break;
2594#endif
John Durkop6ca23272020-12-03 06:01:32 -08002595#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002596 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002597 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2598 &source_operation->ctx.sha256 );
2599 break;
2600#endif
2601#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002602 case PSA_ALG_SHA_256:
2603 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2604 &source_operation->ctx.sha256 );
2605 break;
2606#endif
John Durkop6ca23272020-12-03 06:01:32 -08002607#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002608 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002609 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2610 &source_operation->ctx.sha512 );
2611 break;
2612#endif
2613#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002614 case PSA_ALG_SHA_512:
2615 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2616 &source_operation->ctx.sha512 );
2617 break;
2618#endif
2619 default:
2620 return( PSA_ERROR_NOT_SUPPORTED );
2621 }
2622
2623 target_operation->alg = source_operation->alg;
2624 return( PSA_SUCCESS );
2625}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002626
2627
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002628/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629/* MAC */
2630/****************************************************************/
2631
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002632static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633 psa_algorithm_t alg,
2634 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002635 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002636 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002637{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002638 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002639 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002640
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002641 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002642 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002643
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2645 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002646 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002647 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002648 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002649 mode = MBEDTLS_MODE_STREAM;
2650 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002651 case PSA_ALG_CTR:
2652 mode = MBEDTLS_MODE_CTR;
2653 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002654 case PSA_ALG_CFB:
2655 mode = MBEDTLS_MODE_CFB;
2656 break;
2657 case PSA_ALG_OFB:
2658 mode = MBEDTLS_MODE_OFB;
2659 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002660 case PSA_ALG_ECB_NO_PADDING:
2661 mode = MBEDTLS_MODE_ECB;
2662 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002663 case PSA_ALG_CBC_NO_PADDING:
2664 mode = MBEDTLS_MODE_CBC;
2665 break;
2666 case PSA_ALG_CBC_PKCS7:
2667 mode = MBEDTLS_MODE_CBC;
2668 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002669 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670 mode = MBEDTLS_MODE_CCM;
2671 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002672 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673 mode = MBEDTLS_MODE_GCM;
2674 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002675 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002676 mode = MBEDTLS_MODE_CHACHAPOLY;
2677 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678 default:
2679 return( NULL );
2680 }
2681 }
2682 else if( alg == PSA_ALG_CMAC )
2683 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684 else
2685 return( NULL );
2686
2687 switch( key_type )
2688 {
2689 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002690 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691 break;
2692 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002693 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2694 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002696 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002698 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002699 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2700 * but two-key Triple-DES is functionally three-key Triple-DES
2701 * with K1=K3, so that's how we present it to mbedtls. */
2702 if( key_bits == 128 )
2703 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704 break;
2705 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002706 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707 break;
2708 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002709 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002711 case PSA_KEY_TYPE_CHACHA20:
2712 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2713 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002714 default:
2715 return( NULL );
2716 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002717 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002718 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719
Jaeden Amero23bbb752018-06-26 14:16:54 +01002720 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2721 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722}
2723
John Durkop6ba40d12020-11-10 08:50:04 -08002724#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002725static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002726{
Gilles Peskine2d277862018-06-18 15:41:12 +02002727 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002728 {
2729 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002730 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002731 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002732 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002733 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002734 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002735 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002736 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002737 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002738 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002739 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002740 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002741 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002742 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002743 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002744 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002745 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002746 return( 128 );
2747 default:
2748 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002749 }
2750}
John Durkop07cc04a2020-11-16 22:08:34 -08002751#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002752
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002753/* Initialize the MAC operation structure. Once this function has been
2754 * called, psa_mac_abort can run and will do the right thing. */
2755static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2756 psa_algorithm_t alg )
2757{
2758 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2759
Steven Cooreman15472f82021-03-02 16:16:22 +01002760 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002761 operation->key_set = 0;
2762 operation->iv_set = 0;
2763 operation->iv_required = 0;
2764 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002765 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002766
2767#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002768 if( operation->alg == PSA_ALG_CMAC )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002769 {
2770 operation->iv_required = 0;
2771 mbedtls_cipher_init( &operation->ctx.cmac );
2772 status = PSA_SUCCESS;
2773 }
2774 else
2775#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002776#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002777 if( PSA_ALG_IS_HMAC( operation->alg ) )
2778 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002779 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2780 operation->ctx.hmac.hash_ctx.alg = 0;
2781 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002782 }
2783 else
John Durkopd0321952020-10-29 21:37:36 -07002784#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002785 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002786 if( ! PSA_ALG_IS_MAC( alg ) )
2787 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002788 }
2789
2790 if( status != PSA_SUCCESS )
2791 memset( operation, 0, sizeof( *operation ) );
2792 return( status );
2793}
2794
John Durkop6ba40d12020-11-10 08:50:04 -08002795#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002796static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2797{
Gilles Peskine3f108122018-12-07 18:14:53 +01002798 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002799 return( psa_hash_abort( &hmac->hash_ctx ) );
2800}
John Durkop6ba40d12020-11-10 08:50:04 -08002801#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002802
Gilles Peskine8c9def32018-02-08 10:02:12 +01002803psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2804{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002805 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002807 /* The object has (apparently) been initialized but it is not
2808 * in use. It's ok to call abort on such an object, and there's
2809 * nothing to do. */
2810 return( PSA_SUCCESS );
2811 }
2812 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002813#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002814 if( operation->alg == PSA_ALG_CMAC )
2815 {
2816 mbedtls_cipher_free( &operation->ctx.cmac );
2817 }
2818 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002819#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002820#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002821 if( PSA_ALG_IS_HMAC( operation->alg ) )
2822 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002823 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002824 }
2825 else
John Durkopd0321952020-10-29 21:37:36 -07002826#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002827 {
2828 /* Sanity check (shouldn't happen: operation->alg should
2829 * always have been initialized to a valid value). */
2830 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831 }
Moran Peker41deec42018-04-04 15:43:05 +03002832
Gilles Peskine8c9def32018-02-08 10:02:12 +01002833 operation->alg = 0;
2834 operation->key_set = 0;
2835 operation->iv_set = 0;
2836 operation->iv_required = 0;
2837 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002838 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002839
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002841
2842bad_state:
2843 /* If abort is called on an uninitialized object, we can't trust
2844 * anything. Wipe the object in case it contains confidential data.
2845 * This may result in a memory leak if a pointer gets overwritten,
2846 * but it's too late to do anything about this. */
2847 memset( operation, 0, sizeof( *operation ) );
2848 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002849}
2850
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002851#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002852static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation,
2853 psa_key_slot_t *slot )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002854{
Janos Follath24eed8d2019-11-22 13:21:35 +00002855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002856 const mbedtls_cipher_info_t *cipher_info =
2857 mbedtls_cipher_info_from_psa( PSA_ALG_CMAC,
2858 slot->attr.type, slot->attr.bits,
2859 NULL );
2860 if( cipher_info == NULL )
2861 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002862
2863 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2864 if( ret != 0 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002865 goto exit;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002866
2867 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002868 slot->key.data,
Steven Cooreman15472f82021-03-02 16:16:22 +01002869 slot->attr.bits );
2870exit:
2871 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002872}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002873#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002874
John Durkop6ba40d12020-11-10 08:50:04 -08002875#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002876static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2877 const uint8_t *key,
2878 size_t key_length,
2879 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002880{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002881 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002882 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002883 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002884 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002885 psa_status_t status;
2886
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002887 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2888 * overflow below. This should never trigger if the hash algorithm
2889 * is implemented correctly. */
2890 /* The size checks against the ipad and opad buffers cannot be written
2891 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2892 * because that triggers -Wlogical-op on GCC 7.3. */
2893 if( block_size > sizeof( ipad ) )
2894 return( PSA_ERROR_NOT_SUPPORTED );
2895 if( block_size > sizeof( hmac->opad ) )
2896 return( PSA_ERROR_NOT_SUPPORTED );
2897 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002898 return( PSA_ERROR_NOT_SUPPORTED );
2899
Gilles Peskined223b522018-06-11 18:12:58 +02002900 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002901 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002902 status = psa_hash_compute( hash_alg, key, key_length,
2903 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002904 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002905 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002906 }
Gilles Peskine96889972018-07-12 17:07:03 +02002907 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2908 * but it is permitted. It is common when HMAC is used in HKDF, for
2909 * example. Don't call `memcpy` in the 0-length because `key` could be
2910 * an invalid pointer which would make the behavior undefined. */
2911 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002912 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002913
Gilles Peskined223b522018-06-11 18:12:58 +02002914 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2915 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002916 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002917 ipad[i] ^= 0x36;
2918 memset( ipad + key_length, 0x36, block_size - key_length );
2919
2920 /* Copy the key material from ipad to opad, flipping the requisite bits,
2921 * and filling the rest of opad with the requisite constant. */
2922 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002923 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2924 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002925
Gilles Peskine01126fa2018-07-12 17:04:55 +02002926 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002927 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002928 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002929
Gilles Peskine01126fa2018-07-12 17:04:55 +02002930 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002931
2932cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002933 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002934
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002935 return( status );
2936}
John Durkop6ba40d12020-11-10 08:50:04 -08002937#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002938
Gilles Peskine89167cb2018-07-08 20:12:23 +02002939static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002940 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002941 psa_algorithm_t alg,
2942 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002943{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002944 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002945 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002946 psa_key_slot_t *slot;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002947 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002948 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002949
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002950 /* A context must be freshly initialized before it can be set up. */
2951 if( operation->alg != 0 )
2952 {
2953 return( PSA_ERROR_BAD_STATE );
2954 }
2955
Steven Cooreman15472f82021-03-02 16:16:22 +01002956 status = psa_mac_init( operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002957 if( status != PSA_SUCCESS )
2958 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002959 if( is_sign )
2960 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002961
Ronald Cron5c522922020-11-14 16:35:34 +01002962 status = psa_get_and_lock_transparent_key_slot_with_policy(
2963 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002964 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002965 goto exit;
Steven Cooreman15472f82021-03-02 16:16:22 +01002966
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002967 /* Validate the combination of key type and algorithm */
2968 status = psa_mac_key_can_do( alg, slot->attr.type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002969 if( status != PSA_SUCCESS )
2970 goto exit;
2971
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002972 /* Get the output length for the algorithm and key combination. None of the
2973 * currently supported algorithms have an output length dependent on actual
2974 * key size, so setting it to a bogus value is currently OK. */
2975 operation->mac_size = PSA_MAC_LENGTH( slot->attr.type, 0, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002976
2977 if( operation->mac_size < 4 )
2978 {
2979 /* A very short MAC is too short for security since it can be
2980 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2981 * so we make this our minimum, even though 32 bits is still
2982 * too small for security. */
2983 status = PSA_ERROR_NOT_SUPPORTED;
2984 goto exit;
2985 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002986
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002987 if( operation->mac_size >
2988 PSA_MAC_LENGTH( slot->attr.type, 0, PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
2989 {
2990 /* It's impossible to "truncate" to a larger length than the full length
2991 * of the algorithm. */
2992 status = PSA_ERROR_INVALID_ARGUMENT;
2993 goto exit;
2994 }
2995
Gilles Peskine8c9def32018-02-08 10:02:12 +01002996#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002997 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002998 {
Steven Cooreman15472f82021-03-02 16:16:22 +01002999 status = psa_cmac_setup( operation, slot );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003000 }
3001 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003003#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01003004 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003005 {
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003006 /* Sanity check. This shouldn't fail on a valid configuration. */
Steven Cooreman1fb691a2021-03-08 12:01:55 +01003007 if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003008 {
3009 status = PSA_ERROR_NOT_SUPPORTED;
3010 goto exit;
3011 }
3012
Gilles Peskine8e338702019-07-30 20:06:31 +02003013 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003014 {
3015 status = PSA_ERROR_INVALID_ARGUMENT;
3016 goto exit;
3017 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003018
Gilles Peskine01126fa2018-07-12 17:04:55 +02003019 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003020 slot->key.data,
3021 slot->key.bytes,
Steven Cooreman15472f82021-03-02 16:16:22 +01003022 PSA_ALG_HMAC_GET_HASH( alg ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003023 }
3024 else
John Durkopd0321952020-10-29 21:37:36 -07003025#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003026 {
3027 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003029
Gilles Peskinefbfac682018-07-08 20:51:54 +02003030exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003031 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003033 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003035 else
3036 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003037 operation->key_set = 1;
3038 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003039
Ronald Cron5c522922020-11-14 16:35:34 +01003040 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003041
Ronald Cron5c522922020-11-14 16:35:34 +01003042 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043}
3044
Gilles Peskine89167cb2018-07-08 20:12:23 +02003045psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003046 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003047 psa_algorithm_t alg )
3048{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003049 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003050}
3051
3052psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003053 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003054 psa_algorithm_t alg )
3055{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003056 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003057}
3058
Gilles Peskine8c9def32018-02-08 10:02:12 +01003059psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3060 const uint8_t *input,
3061 size_t input_length )
3062{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003063 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003064 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003065 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003066 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003067 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068 operation->has_input = 1;
3069
Gilles Peskine8c9def32018-02-08 10:02:12 +01003070#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003071 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003072 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003073 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3074 input, input_length );
3075 status = mbedtls_to_psa_error( ret );
3076 }
3077 else
3078#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003079#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003080 if( PSA_ALG_IS_HMAC( operation->alg ) )
3081 {
3082 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3083 input_length );
3084 }
3085 else
John Durkopd0321952020-10-29 21:37:36 -07003086#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003087 {
3088 /* This shouldn't happen if `operation` was initialized by
3089 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003090 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003091 }
3092
Gilles Peskinefbfac682018-07-08 20:51:54 +02003093 if( status != PSA_SUCCESS )
3094 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003095 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003096}
3097
John Durkop6ba40d12020-11-10 08:50:04 -08003098#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003099static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3100 uint8_t *mac,
3101 size_t mac_size )
3102{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003103 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003104 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3105 size_t hash_size = 0;
3106 size_t block_size = psa_get_hash_block_size( hash_alg );
3107 psa_status_t status;
3108
Gilles Peskine01126fa2018-07-12 17:04:55 +02003109 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3110 if( status != PSA_SUCCESS )
3111 return( status );
3112 /* From here on, tmp needs to be wiped. */
3113
3114 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3115 if( status != PSA_SUCCESS )
3116 goto exit;
3117
3118 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3119 if( status != PSA_SUCCESS )
3120 goto exit;
3121
3122 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3123 if( status != PSA_SUCCESS )
3124 goto exit;
3125
Gilles Peskined911eb72018-08-14 15:18:45 +02003126 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3127 if( status != PSA_SUCCESS )
3128 goto exit;
3129
3130 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003131
3132exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003133 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003134 return( status );
3135}
John Durkop6ba40d12020-11-10 08:50:04 -08003136#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003137
mohammad16036df908f2018-04-02 08:34:15 -07003138static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003139 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003140 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003141{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003142 if( ! operation->key_set )
3143 return( PSA_ERROR_BAD_STATE );
3144 if( operation->iv_required && ! operation->iv_set )
3145 return( PSA_ERROR_BAD_STATE );
3146
Gilles Peskine8c9def32018-02-08 10:02:12 +01003147 if( mac_size < operation->mac_size )
3148 return( PSA_ERROR_BUFFER_TOO_SMALL );
3149
Gilles Peskine8c9def32018-02-08 10:02:12 +01003150#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003151 if( operation->alg == PSA_ALG_CMAC )
3152 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003153 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003154 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3155 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003156 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003157 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003158 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003159 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003160 else
3161#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003162#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003163 if( PSA_ALG_IS_HMAC( operation->alg ) )
3164 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003165 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003166 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003167 }
3168 else
John Durkopd0321952020-10-29 21:37:36 -07003169#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003170 {
3171 /* This shouldn't happen if `operation` was initialized by
3172 * a setup function. */
3173 return( PSA_ERROR_BAD_STATE );
3174 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003175}
3176
Gilles Peskineacd4be32018-07-08 19:56:25 +02003177psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3178 uint8_t *mac,
3179 size_t mac_size,
3180 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003181{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003182 psa_status_t status;
3183
Jaeden Amero252ef282019-02-15 14:05:35 +00003184 if( operation->alg == 0 )
3185 {
3186 return( PSA_ERROR_BAD_STATE );
3187 }
3188
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003189 /* Fill the output buffer with something that isn't a valid mac
3190 * (barring an attack on the mac and deliberately-crafted input),
3191 * in case the caller doesn't check the return status properly. */
3192 *mac_length = mac_size;
3193 /* If mac_size is 0 then mac may be NULL and then the
3194 * call to memset would have undefined behavior. */
3195 if( mac_size != 0 )
3196 memset( mac, '!', mac_size );
3197
Gilles Peskine89167cb2018-07-08 20:12:23 +02003198 if( ! operation->is_sign )
3199 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003200 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003201 }
mohammad16036df908f2018-04-02 08:34:15 -07003202
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003203 status = psa_mac_finish_internal( operation, mac, mac_size );
3204
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003205 if( status == PSA_SUCCESS )
3206 {
3207 status = psa_mac_abort( operation );
3208 if( status == PSA_SUCCESS )
3209 *mac_length = operation->mac_size;
3210 else
3211 memset( mac, '!', mac_size );
3212 }
3213 else
3214 psa_mac_abort( operation );
3215 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003216}
3217
Gilles Peskineacd4be32018-07-08 19:56:25 +02003218psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3219 const uint8_t *mac,
3220 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003221{
Gilles Peskine828ed142018-06-18 23:25:51 +02003222 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003223 psa_status_t status;
3224
Jaeden Amero252ef282019-02-15 14:05:35 +00003225 if( operation->alg == 0 )
3226 {
3227 return( PSA_ERROR_BAD_STATE );
3228 }
3229
Gilles Peskine89167cb2018-07-08 20:12:23 +02003230 if( operation->is_sign )
3231 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003232 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003233 }
3234 if( operation->mac_size != mac_length )
3235 {
3236 status = PSA_ERROR_INVALID_SIGNATURE;
3237 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003238 }
mohammad16036df908f2018-04-02 08:34:15 -07003239
3240 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003241 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003242 if( status != PSA_SUCCESS )
3243 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003244
3245 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3246 status = PSA_ERROR_INVALID_SIGNATURE;
3247
3248cleanup:
3249 if( status == PSA_SUCCESS )
3250 status = psa_mac_abort( operation );
3251 else
3252 psa_mac_abort( operation );
3253
Gilles Peskine3f108122018-12-07 18:14:53 +01003254 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003255
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003256 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003257}
3258
3259
Gilles Peskine20035e32018-02-03 22:44:14 +01003260
Gilles Peskine20035e32018-02-03 22:44:14 +01003261/****************************************************************/
3262/* Asymmetric cryptography */
3263/****************************************************************/
3264
Ronald Cron67b1eb32020-12-08 17:37:27 +01003265psa_status_t psa_sign_hash_internal(
Ronald Cron18659932020-12-08 16:32:23 +01003266 const psa_key_attributes_t *attributes,
3267 const uint8_t *key_buffer, size_t key_buffer_size,
3268 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
3269 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003270{
Gilles Peskine20035e32018-02-03 22:44:14 +01003271#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3272 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron99b8ed72021-02-17 10:33:32 +01003273 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003274 {
Ronald Cron7bdbca32020-12-09 13:34:54 +01003275 return( mbedtls_psa_rsa_sign_hash(
3276 attributes,
3277 key_buffer, key_buffer_size,
3278 alg, hash, hash_length,
3279 signature, signature_size, signature_length ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003280 }
3281 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003282#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3283 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine20035e32018-02-03 22:44:14 +01003284#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3285 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Ronald Cronbb9cbc72021-03-04 17:09:00 +01003286 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003287 {
Ronald Cron9103d492021-03-04 11:26:03 +01003288 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003289 {
Ronald Cron072722c2020-12-09 16:36:19 +01003290 return( mbedtls_psa_ecdsa_sign_hash(
3291 attributes,
3292 key_buffer, key_buffer_size,
3293 alg, hash, hash_length,
3294 signature, signature_size, signature_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003295 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003296 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003297 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003298 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003299 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01003300 }
3301 else
Gilles Peskinee59236f2018-01-27 23:32:46 +01003302#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3303 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
itayzafrir5c753392018-05-08 11:18:38 +03003304 {
Ronald Cronbb9cbc72021-03-04 17:09:00 +01003305 (void)attributes;
Ronald Cron8a494f32021-02-17 09:49:51 +01003306 (void)key_buffer;
3307 (void)key_buffer_size;
3308 (void)alg;
3309 (void)hash;
3310 (void)hash_length;
3311 (void)signature;
3312 (void)signature_size;
3313 (void)signature_length;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003314
Ronald Cron8a494f32021-02-17 09:49:51 +01003315 return( PSA_ERROR_NOT_SUPPORTED );
3316 }
Ronald Cron18659932020-12-08 16:32:23 +01003317}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003318
3319psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
3320 psa_algorithm_t alg,
3321 const uint8_t *hash,
3322 size_t hash_length,
3323 uint8_t *signature,
3324 size_t signature_size,
3325 size_t *signature_length )
3326{
3327 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3328 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3329 psa_key_slot_t *slot;
3330
3331 *signature_length = signature_size;
3332 /* Immediately reject a zero-length signature buffer. This guarantees
3333 * that signature must be a valid pointer. (On the other hand, the hash
3334 * buffer can in principle be empty since it doesn't actually have
3335 * to be a hash.) */
3336 if( signature_size == 0 )
3337 return( PSA_ERROR_BUFFER_TOO_SMALL );
3338
3339 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3340 PSA_KEY_USAGE_SIGN_HASH,
3341 alg );
3342 if( status != PSA_SUCCESS )
3343 goto exit;
3344 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
3345 {
3346 status = PSA_ERROR_INVALID_ARGUMENT;
3347 goto exit;
3348 }
3349
Ronald Cron9f17aa42020-12-08 17:07:25 +01003350 psa_key_attributes_t attributes = {
3351 .core = slot->attr
3352 };
Gilles Peskinee59236f2018-01-27 23:32:46 +01003353
Ronald Cron9f17aa42020-12-08 17:07:25 +01003354 status = psa_driver_wrapper_sign_hash(
3355 &attributes, slot->key.data, slot->key.bytes,
3356 alg, hash, hash_length,
3357 signature, signature_size, signature_length );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003358
3359exit:
3360 /* Fill the unused part of the output buffer (the whole buffer on error,
3361 * the trailing part on success) with something that isn't a valid mac
3362 * (barring an attack on the mac and deliberately-crafted input),
3363 * in case the caller doesn't check the return status properly. */
3364 if( status == PSA_SUCCESS )
3365 memset( signature + *signature_length, '!',
3366 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003367 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003368 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003369 /* If signature_size is 0 then we have nothing to do. We must not call
3370 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003371
Ronald Cron5c522922020-11-14 16:35:34 +01003372 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003373
Ronald Cron5c522922020-11-14 16:35:34 +01003374 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003375}
3376
Ronald Cron67b1eb32020-12-08 17:37:27 +01003377psa_status_t psa_verify_hash_internal(
Ronald Cron18659932020-12-08 16:32:23 +01003378 const psa_key_attributes_t *attributes,
3379 const uint8_t *key_buffer, size_t key_buffer_size,
3380 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
3381 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003382{
John Durkop6ba40d12020-11-10 08:50:04 -08003383#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3384 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron99b8ed72021-02-17 10:33:32 +01003385 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003386 {
Ronald Cron7bdbca32020-12-09 13:34:54 +01003387 return( mbedtls_psa_rsa_verify_hash(
3388 attributes,
3389 key_buffer, key_buffer_size,
3390 alg, hash, hash_length,
3391 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003392 }
3393 else
John Durkop6ba40d12020-11-10 08:50:04 -08003394#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3395 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Ronald Cron99b8ed72021-02-17 10:33:32 +01003396 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003397 {
John Durkop9814fa22020-11-04 12:28:15 -08003398#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3399 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003400 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003401 {
Ronald Cron072722c2020-12-09 16:36:19 +01003402 return( mbedtls_psa_ecdsa_verify_hash(
3403 attributes,
3404 key_buffer, key_buffer_size,
3405 alg, hash, hash_length,
3406 signature, signature_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003407 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003408 else
John Durkop9814fa22020-11-04 12:28:15 -08003409#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3410 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003411 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003412 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003413 }
itayzafrir5c753392018-05-08 11:18:38 +03003414 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01003415 else
Gilles Peskinee59236f2018-01-27 23:32:46 +01003416 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003417 (void)key_buffer;
3418 (void)key_buffer_size;
3419 (void)alg;
3420 (void)hash;
3421 (void)hash_length;
3422 (void)signature;
3423 (void)signature_length;
Ronald Cronf95a2b12020-10-22 15:24:49 +02003424
Ronald Cron8a494f32021-02-17 09:49:51 +01003425 return( PSA_ERROR_NOT_SUPPORTED );
3426 }
Ronald Cron18659932020-12-08 16:32:23 +01003427}
3428
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003429psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3430 psa_algorithm_t alg,
3431 const uint8_t *hash,
3432 size_t hash_length,
3433 const uint8_t *signature,
3434 size_t signature_length )
3435{
3436 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3437 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003438 psa_key_slot_t *slot;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003439
3440 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003441 PSA_KEY_USAGE_VERIFY_HASH,
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003442 alg );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003443 if( status != PSA_SUCCESS )
3444 return( status );
3445
Ronald Cron9f17aa42020-12-08 17:07:25 +01003446 psa_key_attributes_t attributes = {
3447 .core = slot->attr
3448 };
Gilles Peskinee59236f2018-01-27 23:32:46 +01003449
Ronald Cron9f17aa42020-12-08 17:07:25 +01003450 status = psa_driver_wrapper_verify_hash(
3451 &attributes, slot->key.data, slot->key.bytes,
3452 alg, hash, hash_length,
3453 signature, signature_length );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003454
Ronald Cron5c522922020-11-14 16:35:34 +01003455 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003456
Ronald Cron5c522922020-11-14 16:35:34 +01003457 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003458}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003459
John Durkop0e005192020-10-31 22:06:54 -07003460#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003461static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3462 mbedtls_rsa_context *rsa )
3463{
3464 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3465 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3466 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3467 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3468}
John Durkop0e005192020-10-31 22:06:54 -07003469#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003470
Ronald Croncf56a0a2020-08-04 09:51:30 +02003471psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003472 psa_algorithm_t alg,
3473 const uint8_t *input,
3474 size_t input_length,
3475 const uint8_t *salt,
3476 size_t salt_length,
3477 uint8_t *output,
3478 size_t output_size,
3479 size_t *output_length )
3480{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003482 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003483 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003484
Darryl Green5cc689a2018-07-24 15:34:10 +01003485 (void) input;
3486 (void) input_length;
3487 (void) salt;
3488 (void) output;
3489 (void) output_size;
3490
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003491 *output_length = 0;
3492
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003493 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3494 return( PSA_ERROR_INVALID_ARGUMENT );
3495
Ronald Cron5c522922020-11-14 16:35:34 +01003496 status = psa_get_and_lock_transparent_key_slot_with_policy(
3497 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003498 if( status != PSA_SUCCESS )
3499 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003500 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3501 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003502 {
3503 status = PSA_ERROR_INVALID_ARGUMENT;
3504 goto exit;
3505 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003506
John Durkop6ba40d12020-11-10 08:50:04 -08003507#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3508 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003509 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003510 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003511 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003512 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3513 slot->key.data,
3514 slot->key.bytes,
3515 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003516 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003517 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003518
3519 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003520 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003521 status = PSA_ERROR_BUFFER_TOO_SMALL;
3522 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003523 }
John Durkop0e005192020-10-31 22:06:54 -07003524#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003525 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
3526 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003527 status = mbedtls_to_psa_error(
3528 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003529 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003530 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003531 MBEDTLS_RSA_PUBLIC,
3532 input_length,
3533 input,
3534 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535 }
3536 else
John Durkop0e005192020-10-31 22:06:54 -07003537#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3538#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003539 if( PSA_ALG_IS_RSA_OAEP( alg ) )
3540 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003541 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003542 status = mbedtls_to_psa_error(
3543 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003544 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003545 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003546 MBEDTLS_RSA_PUBLIC,
3547 salt, salt_length,
3548 input_length,
3549 input,
3550 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003551 }
3552 else
John Durkop0e005192020-10-31 22:06:54 -07003553#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003554 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003555 status = PSA_ERROR_INVALID_ARGUMENT;
3556 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003557 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003558rsa_exit:
3559 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003560 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003561
Steven Cooremana2371e52020-07-28 14:30:39 +02003562 mbedtls_rsa_free( rsa );
3563 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003564 }
3565 else
John Durkop9814fa22020-11-04 12:28:15 -08003566#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003567 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003568 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003569 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003570 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003571
3572exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003573 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003574
Ronald Cron5c522922020-11-14 16:35:34 +01003575 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003576}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577
Ronald Croncf56a0a2020-08-04 09:51:30 +02003578psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003579 psa_algorithm_t alg,
3580 const uint8_t *input,
3581 size_t input_length,
3582 const uint8_t *salt,
3583 size_t salt_length,
3584 uint8_t *output,
3585 size_t output_size,
3586 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003587{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003589 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003590 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003591
Darryl Green5cc689a2018-07-24 15:34:10 +01003592 (void) input;
3593 (void) input_length;
3594 (void) salt;
3595 (void) output;
3596 (void) output_size;
3597
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003598 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003599
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003600 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3601 return( PSA_ERROR_INVALID_ARGUMENT );
3602
Ronald Cron5c522922020-11-14 16:35:34 +01003603 status = psa_get_and_lock_transparent_key_slot_with_policy(
3604 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003605 if( status != PSA_SUCCESS )
3606 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003607 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003608 {
3609 status = PSA_ERROR_INVALID_ARGUMENT;
3610 goto exit;
3611 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003612
John Durkop6ba40d12020-11-10 08:50:04 -08003613#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3614 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003615 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003616 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003617 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003618 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3619 slot->key.data,
3620 slot->key.bytes,
3621 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003622 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003623 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003624
Steven Cooremana2371e52020-07-28 14:30:39 +02003625 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003626 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003627 status = PSA_ERROR_INVALID_ARGUMENT;
3628 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003629 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003630
John Durkop0e005192020-10-31 22:06:54 -07003631#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003632 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003634 status = mbedtls_to_psa_error(
3635 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003636 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003637 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003638 MBEDTLS_RSA_PRIVATE,
3639 output_length,
3640 input,
3641 output,
3642 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643 }
3644 else
John Durkop0e005192020-10-31 22:06:54 -07003645#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3646#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003647 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003648 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003649 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003650 status = mbedtls_to_psa_error(
3651 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003652 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003653 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003654 MBEDTLS_RSA_PRIVATE,
3655 salt, salt_length,
3656 output_length,
3657 input,
3658 output,
3659 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003660 }
3661 else
John Durkop0e005192020-10-31 22:06:54 -07003662#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003663 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003664 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003665 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003666
Steven Cooreman4fed4552020-08-03 14:46:03 +02003667rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003668 mbedtls_rsa_free( rsa );
3669 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003671 else
John Durkop6ba40d12020-11-10 08:50:04 -08003672#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3673 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003674 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003675 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003676 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003677
3678exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003679 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003680
Ronald Cron5c522922020-11-14 16:35:34 +01003681 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003682}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003683
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003684
3685
mohammad1603503973b2018-03-12 15:59:30 +02003686/****************************************************************/
3687/* Symmetric cryptography */
3688/****************************************************************/
3689
Gilles Peskinee553c652018-06-04 16:22:46 +02003690static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003691 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003692 psa_algorithm_t alg,
3693 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003694{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003695 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003696 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003697 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003698 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003699 size_t key_bits;
3700 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003701 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3702 PSA_KEY_USAGE_ENCRYPT :
3703 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003704
Steven Cooremand3feccd2020-09-01 15:56:14 +02003705 /* A context must be freshly initialized before it can be set up. */
3706 if( operation->alg != 0 )
3707 return( PSA_ERROR_BAD_STATE );
3708
Steven Cooremana07b9972020-09-10 14:54:14 +02003709 /* The requested algorithm must be one that can be processed by cipher. */
3710 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003711 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003712
Steven Cooremanef8575e2020-09-11 11:44:50 +02003713 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003714 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003715 if( status != PSA_SUCCESS )
3716 goto exit;
3717
3718 /* Initialize the operation struct members, except for alg. The alg member
3719 * is used to indicate to psa_cipher_abort that there are resources to free,
3720 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003721 operation->key_set = 0;
3722 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003723 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003724 operation->iv_size = 0;
3725 operation->block_size = 0;
3726 if( alg == PSA_ALG_ECB_NO_PADDING )
3727 operation->iv_required = 0;
3728 else
3729 operation->iv_required = 1;
3730
Steven Cooremana07b9972020-09-10 14:54:14 +02003731 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003732 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003733 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003734 slot,
3735 alg );
3736 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003737 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003738 slot,
3739 alg );
3740
Steven Cooremanef8575e2020-09-11 11:44:50 +02003741 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003742 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003743 /* Once the driver context is initialised, it needs to be freed using
3744 * psa_cipher_abort. Indicate this through setting alg. */
3745 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003746 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02003747
Steven Cooremand3feccd2020-09-01 15:56:14 +02003748 if( status != PSA_ERROR_NOT_SUPPORTED ||
3749 psa_key_lifetime_is_external( slot->attr.lifetime ) )
3750 goto exit;
3751
Steven Cooremana07b9972020-09-10 14:54:14 +02003752 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02003753 * available for the given algorithm & key. */
3754 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02003755
Steven Cooremana07b9972020-09-10 14:54:14 +02003756 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02003757 * psa_cipher_abort. Indicate there is something to be freed through setting
3758 * alg, and indicate the operation is being done using mbedtls crypto through
3759 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003760 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003761 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02003762
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003763 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02003764 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003765 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003766 {
3767 status = PSA_ERROR_NOT_SUPPORTED;
3768 goto exit;
3769 }
mohammad1603503973b2018-03-12 15:59:30 +02003770
mohammad1603503973b2018-03-12 15:59:30 +02003771 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003772 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003773 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003774
David Brown12ca5032021-02-11 11:02:00 -07003775#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02003776 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003777 {
3778 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003779 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01003780 memcpy( keys, slot->key.data, 16 );
3781 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003782 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3783 keys,
3784 192, cipher_operation );
3785 }
3786 else
3787#endif
3788 {
3789 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003790 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003791 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003792 }
Moran Peker41deec42018-04-04 15:43:05 +03003793 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003794 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003795
David Brown63ca2602021-01-26 11:51:12 -07003796#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
3797 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003798 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003799 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003800 case PSA_ALG_CBC_NO_PADDING:
3801 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3802 MBEDTLS_PADDING_NONE );
3803 break;
3804 case PSA_ALG_CBC_PKCS7:
3805 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3806 MBEDTLS_PADDING_PKCS7 );
3807 break;
3808 default:
3809 /* The algorithm doesn't involve padding. */
3810 ret = 0;
3811 break;
3812 }
3813 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003814 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07003815#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02003816
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003817 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003818 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02003819 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
3820 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02003821 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003822 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003823 }
David Brown1bfe4d72021-02-16 12:54:35 -07003824#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02003825 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01003826 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02003827 operation->iv_size = 12;
3828#endif
mohammad1603503973b2018-03-12 15:59:30 +02003829
Steven Cooreman7df02922020-09-09 15:28:49 +02003830 status = PSA_SUCCESS;
3831
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003832exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003833 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003834 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02003835 if( status == PSA_SUCCESS )
3836 {
3837 /* Update operation flags for both driver and software implementations */
3838 operation->key_set = 1;
3839 }
3840 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003841 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003842
Ronald Cron5c522922020-11-14 16:35:34 +01003843 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003844
Ronald Cron5c522922020-11-14 16:35:34 +01003845 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003846}
3847
Gilles Peskinefe119512018-07-08 21:39:34 +02003848psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003849 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003850 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003851{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003852 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003853}
3854
Gilles Peskinefe119512018-07-08 21:39:34 +02003855psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003856 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003857 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003858{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003859 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003860}
3861
Gilles Peskinefe119512018-07-08 21:39:34 +02003862psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003863 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003864 size_t iv_size,
3865 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003866{
itayzafrir534bd7c2018-08-02 13:56:32 +03003867 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02003869 if( operation->iv_set || ! operation->iv_required )
3870 {
3871 return( PSA_ERROR_BAD_STATE );
3872 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003873
Steven Cooremanef8575e2020-09-11 11:44:50 +02003874 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02003875 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003876 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02003877 iv,
3878 iv_size,
3879 iv_length );
3880 goto exit;
3881 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003882
Moran Peker41deec42018-04-04 15:43:05 +03003883 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003884 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003885 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003886 goto exit;
3887 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003888 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003889 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003890 if( ret != 0 )
3891 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003892 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003893 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003894 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003895
mohammad16038481e742018-03-18 13:57:31 +02003896 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003897 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003898
Moran Peker395db872018-05-31 14:07:14 +03003899exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003900 if( status == PSA_SUCCESS )
3901 operation->iv_set = 1;
3902 else
Moran Peker395db872018-05-31 14:07:14 +03003903 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003904 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003905}
3906
Gilles Peskinefe119512018-07-08 21:39:34 +02003907psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003908 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003909 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003910{
itayzafrir534bd7c2018-08-02 13:56:32 +03003911 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02003913 if( operation->iv_set || ! operation->iv_required )
3914 {
3915 return( PSA_ERROR_BAD_STATE );
3916 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003917
Steven Cooremanef8575e2020-09-11 11:44:50 +02003918 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02003919 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003920 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02003921 iv,
3922 iv_length );
3923 goto exit;
3924 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003925
Moran Pekera28258c2018-05-29 16:25:04 +03003926 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003927 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003928 status = PSA_ERROR_INVALID_ARGUMENT;
3929 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003930 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003931 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3932 status = mbedtls_to_psa_error( ret );
3933exit:
3934 if( status == PSA_SUCCESS )
3935 operation->iv_set = 1;
3936 else
mohammad1603503973b2018-03-12 15:59:30 +02003937 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003938 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003939}
3940
Steven Cooreman177deba2020-09-07 17:14:14 +02003941/* Process input for which the algorithm is set to ECB mode. This requires
3942 * manual processing, since the PSA API is defined as being able to process
3943 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
3944 * underlying mbedtls_cipher_update only takes full blocks. */
3945static psa_status_t psa_cipher_update_ecb_internal(
3946 mbedtls_cipher_context_t *ctx,
3947 const uint8_t *input,
3948 size_t input_length,
3949 uint8_t *output,
3950 size_t output_size,
3951 size_t *output_length )
3952{
3953 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3954 size_t block_size = ctx->cipher_info->block_size;
3955 size_t internal_output_length = 0;
3956 *output_length = 0;
3957
3958 if( input_length == 0 )
3959 {
3960 status = PSA_SUCCESS;
3961 goto exit;
3962 }
3963
3964 if( ctx->unprocessed_len > 0 )
3965 {
3966 /* Fill up to block size, and run the block if there's a full one. */
3967 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
3968
3969 if( input_length < bytes_to_copy )
3970 bytes_to_copy = input_length;
3971
3972 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
3973 input, bytes_to_copy );
3974 input_length -= bytes_to_copy;
3975 input += bytes_to_copy;
3976 ctx->unprocessed_len += bytes_to_copy;
3977
3978 if( ctx->unprocessed_len == block_size )
3979 {
3980 status = mbedtls_to_psa_error(
3981 mbedtls_cipher_update( ctx,
3982 ctx->unprocessed_data,
3983 block_size,
3984 output, &internal_output_length ) );
3985
3986 if( status != PSA_SUCCESS )
3987 goto exit;
3988
3989 output += internal_output_length;
3990 output_size -= internal_output_length;
3991 *output_length += internal_output_length;
3992 ctx->unprocessed_len = 0;
3993 }
3994 }
3995
3996 while( input_length >= block_size )
3997 {
3998 /* Run all full blocks we have, one by one */
3999 status = mbedtls_to_psa_error(
4000 mbedtls_cipher_update( ctx, input,
4001 block_size,
4002 output, &internal_output_length ) );
4003
4004 if( status != PSA_SUCCESS )
4005 goto exit;
4006
4007 input_length -= block_size;
4008 input += block_size;
4009
4010 output += internal_output_length;
4011 output_size -= internal_output_length;
4012 *output_length += internal_output_length;
4013 }
4014
4015 if( input_length > 0 )
4016 {
4017 /* Save unprocessed bytes for later processing */
4018 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4019 input, input_length );
4020 ctx->unprocessed_len += input_length;
4021 }
4022
4023 status = PSA_SUCCESS;
4024
4025exit:
4026 return( status );
4027}
4028
Gilles Peskinee553c652018-06-04 16:22:46 +02004029psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4030 const uint8_t *input,
4031 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004032 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004033 size_t output_size,
4034 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004035{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004036 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004037 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004038 if( operation->alg == 0 )
4039 {
4040 return( PSA_ERROR_BAD_STATE );
4041 }
4042 if( operation->iv_required && ! operation->iv_set )
4043 {
4044 return( PSA_ERROR_BAD_STATE );
4045 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004046
Steven Cooremanef8575e2020-09-11 11:44:50 +02004047 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004048 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004049 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004050 input,
4051 input_length,
4052 output,
4053 output_size,
4054 output_length );
4055 goto exit;
4056 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004057
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004058 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004059 {
4060 /* Take the unprocessed partial block left over from previous
4061 * update calls, if any, plus the input to this call. Remove
4062 * the last partial block, if any. You get the data that will be
4063 * output in this call. */
4064 expected_output_size =
4065 ( operation->ctx.cipher.unprocessed_len + input_length )
4066 / operation->block_size * operation->block_size;
4067 }
4068 else
4069 {
4070 expected_output_size = input_length;
4071 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004072
Gilles Peskine89d789c2018-06-04 17:17:16 +02004073 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004074 {
4075 status = PSA_ERROR_BUFFER_TOO_SMALL;
4076 goto exit;
4077 }
mohammad160382759612018-03-12 18:16:40 +02004078
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004079 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4080 {
4081 /* mbedtls_cipher_update has an API inconsistency: it will only
4082 * process a single block at a time in ECB mode. Abstract away that
4083 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004084 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4085 input,
4086 input_length,
4087 output,
4088 output_size,
4089 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004090 }
4091 else
4092 {
4093 status = mbedtls_to_psa_error(
4094 mbedtls_cipher_update( &operation->ctx.cipher, input,
4095 input_length, output, output_length ) );
4096 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004097exit:
4098 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004099 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004100 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004101}
4102
Gilles Peskinee553c652018-06-04 16:22:46 +02004103psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4104 uint8_t *output,
4105 size_t output_size,
4106 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004107{
David Saadab4ecc272019-02-14 13:48:10 +02004108 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004109 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004110 if( operation->alg == 0 )
4111 {
4112 return( PSA_ERROR_BAD_STATE );
4113 }
4114 if( operation->iv_required && ! operation->iv_set )
4115 {
4116 return( PSA_ERROR_BAD_STATE );
4117 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004118
Steven Cooremanef8575e2020-09-11 11:44:50 +02004119 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004120 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004121 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004122 output,
4123 output_size,
4124 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004125 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004126 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004127
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004128 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004129 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004130 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004131 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004132 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004133 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004134 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004135 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004136 }
4137
Steven Cooremanef8575e2020-09-11 11:44:50 +02004138 status = mbedtls_to_psa_error(
4139 mbedtls_cipher_finish( &operation->ctx.cipher,
4140 temp_output_buffer,
4141 output_length ) );
4142 if( status != PSA_SUCCESS )
4143 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004144
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004145 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004146 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004147 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004148 memcpy( output, temp_output_buffer, *output_length );
4149 else
Janos Follath315b51c2018-07-09 16:04:51 +01004150 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004151
Steven Cooremanef8575e2020-09-11 11:44:50 +02004152exit:
4153 if( operation->mbedtls_in_use == 1 )
4154 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004155
Steven Cooremanef8575e2020-09-11 11:44:50 +02004156 if( status == PSA_SUCCESS )
4157 return( psa_cipher_abort( operation ) );
4158 else
4159 {
4160 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004161 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004162
Steven Cooremanef8575e2020-09-11 11:44:50 +02004163 return( status );
4164 }
mohammad1603503973b2018-03-12 15:59:30 +02004165}
4166
Gilles Peskinee553c652018-06-04 16:22:46 +02004167psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4168{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004169 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004170 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004171 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004172 * in use. It's ok to call abort on such an object, and there's
4173 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004174 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004175 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004176
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004177 /* Sanity check (shouldn't happen: operation->alg should
4178 * always have been initialized to a valid value). */
4179 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4180 return( PSA_ERROR_BAD_STATE );
4181
Steven Cooremanef8575e2020-09-11 11:44:50 +02004182 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004183 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004184 else
4185 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004186
Moran Peker41deec42018-04-04 15:43:05 +03004187 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004188 operation->key_set = 0;
4189 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004190 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004191 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004192 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004193 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004194
Moran Peker395db872018-05-31 14:07:14 +03004195 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004196}
4197
Gilles Peskinea0655c32018-04-30 17:06:50 +02004198
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004199
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004200
mohammad16035955c982018-04-26 00:53:03 +03004201/****************************************************************/
4202/* AEAD */
4203/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004204
Gilles Peskineedf9a652018-08-17 18:11:56 +02004205typedef struct
4206{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004207 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004208 const mbedtls_cipher_info_t *cipher_info;
4209 union
4210 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004211 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004212#if defined(MBEDTLS_CCM_C)
4213 mbedtls_ccm_context ccm;
4214#endif /* MBEDTLS_CCM_C */
4215#if defined(MBEDTLS_GCM_C)
4216 mbedtls_gcm_context gcm;
4217#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004218#if defined(MBEDTLS_CHACHAPOLY_C)
4219 mbedtls_chachapoly_context chachapoly;
4220#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004221 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004222 psa_algorithm_t core_alg;
4223 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004224 uint8_t tag_length;
4225} aead_operation_t;
4226
Ronald Cronf95a2b12020-10-22 15:24:49 +02004227#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4228
Gilles Peskine30a9e412019-01-14 18:36:12 +01004229static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004230{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004231 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004232 {
4233#if defined(MBEDTLS_CCM_C)
4234 case PSA_ALG_CCM:
4235 mbedtls_ccm_free( &operation->ctx.ccm );
4236 break;
4237#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004238#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004239 case PSA_ALG_GCM:
4240 mbedtls_gcm_free( &operation->ctx.gcm );
4241 break;
4242#endif /* MBEDTLS_GCM_C */
4243 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004244
Ronald Cron5c522922020-11-14 16:35:34 +01004245 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004246}
4247
4248static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004249 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004250 psa_key_usage_t usage,
4251 psa_algorithm_t alg )
4252{
4253 psa_status_t status;
4254 size_t key_bits;
4255 mbedtls_cipher_id_t cipher_id;
4256
Ronald Cron5c522922020-11-14 16:35:34 +01004257 status = psa_get_and_lock_transparent_key_slot_with_policy(
4258 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004259 if( status != PSA_SUCCESS )
4260 return( status );
4261
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004262 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004263
4264 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004265 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004266 &cipher_id );
4267 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004268 {
4269 status = PSA_ERROR_NOT_SUPPORTED;
4270 goto cleanup;
4271 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004272
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004273 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004274 {
4275#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004276 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004277 operation->core_alg = PSA_ALG_CCM;
4278 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004279 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4280 * The call to mbedtls_ccm_encrypt_and_tag or
4281 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004282 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004283 {
4284 status = PSA_ERROR_INVALID_ARGUMENT;
4285 goto cleanup;
4286 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004287 mbedtls_ccm_init( &operation->ctx.ccm );
4288 status = mbedtls_to_psa_error(
4289 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004290 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004291 (unsigned int) key_bits ) );
4292 if( status != 0 )
4293 goto cleanup;
4294 break;
4295#endif /* MBEDTLS_CCM_C */
4296
4297#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004298 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004299 operation->core_alg = PSA_ALG_GCM;
4300 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004301 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4302 * The call to mbedtls_gcm_crypt_and_tag or
4303 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004304 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004305 {
4306 status = PSA_ERROR_INVALID_ARGUMENT;
4307 goto cleanup;
4308 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004309 mbedtls_gcm_init( &operation->ctx.gcm );
4310 status = mbedtls_to_psa_error(
4311 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004312 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004313 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004314 if( status != 0 )
4315 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004316 break;
4317#endif /* MBEDTLS_GCM_C */
4318
Gilles Peskine26869f22019-05-06 15:25:00 +02004319#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004320 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004321 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4322 operation->full_tag_length = 16;
4323 /* We only support the default tag length. */
4324 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004325 {
4326 status = PSA_ERROR_NOT_SUPPORTED;
4327 goto cleanup;
4328 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004329 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4330 status = mbedtls_to_psa_error(
4331 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004332 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004333 if( status != 0 )
4334 goto cleanup;
4335 break;
4336#endif /* MBEDTLS_CHACHAPOLY_C */
4337
Gilles Peskineedf9a652018-08-17 18:11:56 +02004338 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004339 status = PSA_ERROR_NOT_SUPPORTED;
4340 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004341 }
4342
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004343 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4344 {
4345 status = PSA_ERROR_INVALID_ARGUMENT;
4346 goto cleanup;
4347 }
4348 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004349
Gilles Peskineedf9a652018-08-17 18:11:56 +02004350 return( PSA_SUCCESS );
4351
4352cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004353 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004354 return( status );
4355}
4356
Ronald Croncf56a0a2020-08-04 09:51:30 +02004357psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004358 psa_algorithm_t alg,
4359 const uint8_t *nonce,
4360 size_t nonce_length,
4361 const uint8_t *additional_data,
4362 size_t additional_data_length,
4363 const uint8_t *plaintext,
4364 size_t plaintext_length,
4365 uint8_t *ciphertext,
4366 size_t ciphertext_size,
4367 size_t *ciphertext_length )
4368{
mohammad16035955c982018-04-26 00:53:03 +03004369 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004370 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004371 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004372
mohammad1603f08a5502018-06-03 15:05:47 +03004373 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004374
Ronald Croncf56a0a2020-08-04 09:51:30 +02004375 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004376 if( status != PSA_SUCCESS )
4377 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004378
Gilles Peskineedf9a652018-08-17 18:11:56 +02004379 /* For all currently supported modes, the tag is at the end of the
4380 * ciphertext. */
4381 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4382 {
4383 status = PSA_ERROR_BUFFER_TOO_SMALL;
4384 goto exit;
4385 }
4386 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004387
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004388#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004389 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004390 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004391 status = mbedtls_to_psa_error(
4392 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4393 MBEDTLS_GCM_ENCRYPT,
4394 plaintext_length,
4395 nonce, nonce_length,
4396 additional_data, additional_data_length,
4397 plaintext, ciphertext,
4398 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004399 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004400 else
4401#endif /* MBEDTLS_GCM_C */
4402#if defined(MBEDTLS_CCM_C)
4403 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004404 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004405 status = mbedtls_to_psa_error(
4406 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4407 plaintext_length,
4408 nonce, nonce_length,
4409 additional_data,
4410 additional_data_length,
4411 plaintext, ciphertext,
4412 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004413 }
mohammad16035c8845f2018-05-09 05:40:09 -07004414 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004415#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004416#if defined(MBEDTLS_CHACHAPOLY_C)
4417 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4418 {
4419 if( nonce_length != 12 || operation.tag_length != 16 )
4420 {
4421 status = PSA_ERROR_NOT_SUPPORTED;
4422 goto exit;
4423 }
4424 status = mbedtls_to_psa_error(
4425 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4426 plaintext_length,
4427 nonce,
4428 additional_data,
4429 additional_data_length,
4430 plaintext,
4431 ciphertext,
4432 tag ) );
4433 }
4434 else
4435#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004436 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004437 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004438 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004439 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004440
Gilles Peskineedf9a652018-08-17 18:11:56 +02004441 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4442 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004443
Gilles Peskineedf9a652018-08-17 18:11:56 +02004444exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004445 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004446 if( status == PSA_SUCCESS )
4447 *ciphertext_length = plaintext_length + operation.tag_length;
4448 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004449}
4450
Gilles Peskineee652a32018-06-01 19:23:52 +02004451/* Locate the tag in a ciphertext buffer containing the encrypted data
4452 * followed by the tag. Return the length of the part preceding the tag in
4453 * *plaintext_length. This is the size of the plaintext in modes where
4454 * the encrypted data has the same size as the plaintext, such as
4455 * CCM and GCM. */
4456static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4457 const uint8_t *ciphertext,
4458 size_t ciphertext_length,
4459 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004460 const uint8_t **p_tag )
4461{
4462 size_t payload_length;
4463 if( tag_length > ciphertext_length )
4464 return( PSA_ERROR_INVALID_ARGUMENT );
4465 payload_length = ciphertext_length - tag_length;
4466 if( payload_length > plaintext_size )
4467 return( PSA_ERROR_BUFFER_TOO_SMALL );
4468 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004469 return( PSA_SUCCESS );
4470}
4471
Ronald Croncf56a0a2020-08-04 09:51:30 +02004472psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004473 psa_algorithm_t alg,
4474 const uint8_t *nonce,
4475 size_t nonce_length,
4476 const uint8_t *additional_data,
4477 size_t additional_data_length,
4478 const uint8_t *ciphertext,
4479 size_t ciphertext_length,
4480 uint8_t *plaintext,
4481 size_t plaintext_size,
4482 size_t *plaintext_length )
4483{
mohammad16035955c982018-04-26 00:53:03 +03004484 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004485 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004486 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004487
Gilles Peskineee652a32018-06-01 19:23:52 +02004488 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004489
Ronald Croncf56a0a2020-08-04 09:51:30 +02004490 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004491 if( status != PSA_SUCCESS )
4492 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004493
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004494 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4495 ciphertext, ciphertext_length,
4496 plaintext_size, &tag );
4497 if( status != PSA_SUCCESS )
4498 goto exit;
4499
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004500#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004501 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004502 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004503 status = mbedtls_to_psa_error(
4504 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4505 ciphertext_length - operation.tag_length,
4506 nonce, nonce_length,
4507 additional_data,
4508 additional_data_length,
4509 tag, operation.tag_length,
4510 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004511 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004512 else
4513#endif /* MBEDTLS_GCM_C */
4514#if defined(MBEDTLS_CCM_C)
4515 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004516 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004517 status = mbedtls_to_psa_error(
4518 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4519 ciphertext_length - operation.tag_length,
4520 nonce, nonce_length,
4521 additional_data,
4522 additional_data_length,
4523 ciphertext, plaintext,
4524 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004525 }
mohammad160339574652018-06-01 04:39:53 -07004526 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004527#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004528#if defined(MBEDTLS_CHACHAPOLY_C)
4529 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4530 {
4531 if( nonce_length != 12 || operation.tag_length != 16 )
4532 {
4533 status = PSA_ERROR_NOT_SUPPORTED;
4534 goto exit;
4535 }
4536 status = mbedtls_to_psa_error(
4537 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4538 ciphertext_length - operation.tag_length,
4539 nonce,
4540 additional_data,
4541 additional_data_length,
4542 tag,
4543 ciphertext,
4544 plaintext ) );
4545 }
4546 else
4547#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004548 {
mohammad1603554faad2018-06-03 15:07:38 +03004549 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004550 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004551
Gilles Peskineedf9a652018-08-17 18:11:56 +02004552 if( status != PSA_SUCCESS && plaintext_size != 0 )
4553 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004554
Gilles Peskineedf9a652018-08-17 18:11:56 +02004555exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004556 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004557 if( status == PSA_SUCCESS )
4558 *plaintext_length = ciphertext_length - operation.tag_length;
4559 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004560}
4561
Gilles Peskinea0655c32018-04-30 17:06:50 +02004562
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004563
Gilles Peskinee59236f2018-01-27 23:32:46 +01004564/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004565/* Generators */
4566/****************************************************************/
4567
John Durkop07cc04a2020-11-16 22:08:34 -08004568#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4569 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4570 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4571#define AT_LEAST_ONE_BUILTIN_KDF
4572#endif
4573
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004574#define HKDF_STATE_INIT 0 /* no input yet */
4575#define HKDF_STATE_STARTED 1 /* got salt */
4576#define HKDF_STATE_KEYED 2 /* got key */
4577#define HKDF_STATE_OUTPUT 3 /* output started */
4578
Gilles Peskinecbe66502019-05-16 16:59:18 +02004579static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004580 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004581{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004582 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4583 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004584 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004585 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004586}
4587
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004588psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004589{
4590 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004591 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004592 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004593 {
4594 /* The object has (apparently) been initialized but it is not
4595 * in use. It's ok to call abort on such an object, and there's
4596 * nothing to do. */
4597 }
4598 else
John Durkopd0321952020-10-29 21:37:36 -07004599#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004600 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004601 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 mbedtls_free( operation->ctx.hkdf.info );
4603 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004604 }
John Durkop07cc04a2020-11-16 22:08:34 -08004605 else
4606#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4607#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4608 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4609 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004610 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004611 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004612 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004613 if( operation->ctx.tls12_prf.seed != NULL )
4614 {
4615 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4616 operation->ctx.tls12_prf.seed_length );
4617 mbedtls_free( operation->ctx.tls12_prf.seed );
4618 }
4619
4620 if( operation->ctx.tls12_prf.label != NULL )
4621 {
4622 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4623 operation->ctx.tls12_prf.label_length );
4624 mbedtls_free( operation->ctx.tls12_prf.label );
4625 }
4626
4627 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4628
4629 /* We leave the fields Ai and output_block to be erased safely by the
4630 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004631 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004632 else
John Durkop07cc04a2020-11-16 22:08:34 -08004633#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4634 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004635 {
4636 status = PSA_ERROR_BAD_STATE;
4637 }
Janos Follath083036a2019-06-11 10:22:26 +01004638 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004639 return( status );
4640}
4641
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004642psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004643 size_t *capacity)
4644{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004645 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004646 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004647 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004648 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004649 }
4650
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004651 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004652 return( PSA_SUCCESS );
4653}
4654
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004655psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004656 size_t capacity )
4657{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004658 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004659 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004660 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004661 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004663 return( PSA_SUCCESS );
4664}
4665
John Durkopd0321952020-10-29 21:37:36 -07004666#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004668 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004669static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004670 psa_algorithm_t hash_alg,
4671 uint8_t *output,
4672 size_t output_length )
4673{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004674 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004675 psa_status_t status;
4676
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004677 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4678 return( PSA_ERROR_BAD_STATE );
4679 hkdf->state = HKDF_STATE_OUTPUT;
4680
Gilles Peskinebef7f142018-07-12 17:22:21 +02004681 while( output_length != 0 )
4682 {
4683 /* Copy what remains of the current block */
4684 uint8_t n = hash_length - hkdf->offset_in_block;
4685 if( n > output_length )
4686 n = (uint8_t) output_length;
4687 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4688 output += n;
4689 output_length -= n;
4690 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004691 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004692 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004693 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004694 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004695 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004696 * object was corrupted or if this function is called directly
4697 * inside the library. */
4698 if( hkdf->block_number == 0xff )
4699 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004700
4701 /* We need a new block */
4702 ++hkdf->block_number;
4703 hkdf->offset_in_block = 0;
4704 status = psa_hmac_setup_internal( &hkdf->hmac,
4705 hkdf->prk, hash_length,
4706 hash_alg );
4707 if( status != PSA_SUCCESS )
4708 return( status );
4709 if( hkdf->block_number != 1 )
4710 {
4711 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4712 hkdf->output_block,
4713 hash_length );
4714 if( status != PSA_SUCCESS )
4715 return( status );
4716 }
4717 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4718 hkdf->info,
4719 hkdf->info_length );
4720 if( status != PSA_SUCCESS )
4721 return( status );
4722 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4723 &hkdf->block_number, 1 );
4724 if( status != PSA_SUCCESS )
4725 return( status );
4726 status = psa_hmac_finish_internal( &hkdf->hmac,
4727 hkdf->output_block,
4728 sizeof( hkdf->output_block ) );
4729 if( status != PSA_SUCCESS )
4730 return( status );
4731 }
4732
4733 return( PSA_SUCCESS );
4734}
John Durkop07cc04a2020-11-16 22:08:34 -08004735#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004736
John Durkop07cc04a2020-11-16 22:08:34 -08004737#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4738 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004739static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4740 psa_tls12_prf_key_derivation_t *tls12_prf,
4741 psa_algorithm_t alg )
4742{
4743 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004744 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004745 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4746 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004747
Janos Follath7742fee2019-06-17 12:58:10 +01004748 /* We can't be wanting more output after block 0xff, otherwise
4749 * the capacity check in psa_key_derivation_output_bytes() would have
4750 * prevented this call. It could happen only if the operation
4751 * object was corrupted or if this function is called directly
4752 * inside the library. */
4753 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004754 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004755
4756 /* We need a new block */
4757 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004758 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004759
4760 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4761 *
4762 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4763 *
4764 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4765 * HMAC_hash(secret, A(2) + seed) +
4766 * HMAC_hash(secret, A(3) + seed) + ...
4767 *
4768 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004769 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004770 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004771 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004772 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004773 * is currently extracted as `output_block` and where i is
4774 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004775 */
4776
Janos Follathea29bfb2019-06-19 12:21:20 +01004777 /* Save the hash context before using it, to preserve the hash state with
4778 * only the inner padding in it. We need this, because inner padding depends
4779 * on the key (secret in the RFC's terminology). */
4780 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4781 if( status != PSA_SUCCESS )
4782 goto cleanup;
4783
4784 /* Calculate A(i) where i = tls12_prf->block_number. */
4785 if( tls12_prf->block_number == 1 )
4786 {
4787 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4788 * the variable seed and in this instance means it in the context of the
4789 * P_hash function, where seed = label + seed.) */
4790 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4791 tls12_prf->label, tls12_prf->label_length );
4792 if( status != PSA_SUCCESS )
4793 goto cleanup;
4794 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4795 tls12_prf->seed, tls12_prf->seed_length );
4796 if( status != PSA_SUCCESS )
4797 goto cleanup;
4798 }
4799 else
4800 {
4801 /* A(i) = HMAC_hash(secret, A(i-1)) */
4802 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4803 tls12_prf->Ai, hash_length );
4804 if( status != PSA_SUCCESS )
4805 goto cleanup;
4806 }
4807
4808 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4809 tls12_prf->Ai, hash_length );
4810 if( status != PSA_SUCCESS )
4811 goto cleanup;
4812 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4813 if( status != PSA_SUCCESS )
4814 goto cleanup;
4815
4816 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4817 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4818 tls12_prf->Ai, hash_length );
4819 if( status != PSA_SUCCESS )
4820 goto cleanup;
4821 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4822 tls12_prf->label, tls12_prf->label_length );
4823 if( status != PSA_SUCCESS )
4824 goto cleanup;
4825 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4826 tls12_prf->seed, tls12_prf->seed_length );
4827 if( status != PSA_SUCCESS )
4828 goto cleanup;
4829 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4830 tls12_prf->output_block, hash_length );
4831 if( status != PSA_SUCCESS )
4832 goto cleanup;
4833 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4834 if( status != PSA_SUCCESS )
4835 goto cleanup;
4836
Janos Follath7742fee2019-06-17 12:58:10 +01004837
4838cleanup:
4839
Janos Follathea29bfb2019-06-19 12:21:20 +01004840 cleanup_status = psa_hash_abort( &backup );
4841 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4842 status = cleanup_status;
4843
4844 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004845}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004846
Janos Follath844eb0e2019-06-19 12:10:49 +01004847static psa_status_t psa_key_derivation_tls12_prf_read(
4848 psa_tls12_prf_key_derivation_t *tls12_prf,
4849 psa_algorithm_t alg,
4850 uint8_t *output,
4851 size_t output_length )
4852{
4853 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004854 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004855 psa_status_t status;
4856 uint8_t offset, length;
4857
4858 while( output_length != 0 )
4859 {
4860 /* Check if we have fully processed the current block. */
4861 if( tls12_prf->left_in_block == 0 )
4862 {
4863 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4864 alg );
4865 if( status != PSA_SUCCESS )
4866 return( status );
4867
4868 continue;
4869 }
4870
4871 if( tls12_prf->left_in_block > output_length )
4872 length = (uint8_t) output_length;
4873 else
4874 length = tls12_prf->left_in_block;
4875
4876 offset = hash_length - tls12_prf->left_in_block;
4877 memcpy( output, tls12_prf->output_block + offset, length );
4878 output += length;
4879 output_length -= length;
4880 tls12_prf->left_in_block -= length;
4881 }
4882
4883 return( PSA_SUCCESS );
4884}
John Durkop07cc04a2020-11-16 22:08:34 -08004885#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4886 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004887
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004888psa_status_t psa_key_derivation_output_bytes(
4889 psa_key_derivation_operation_t *operation,
4890 uint8_t *output,
4891 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004892{
4893 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004894 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004895
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004897 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004899 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004900 }
4901
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004902 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004903 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004905 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004907 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004908 goto exit;
4909 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004911 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004913 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004914 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4915 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004917 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004918 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004919 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004921
John Durkopd0321952020-10-29 21:37:36 -07004922#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004923 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004924 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004925 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004926 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004927 output, output_length );
4928 }
Janos Follathadbec812019-06-14 11:05:39 +01004929 else
John Durkop07cc04a2020-11-16 22:08:34 -08004930#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4931#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4932 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004933 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004934 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004935 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004937 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004938 output_length );
4939 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004940 else
John Durkop07cc04a2020-11-16 22:08:34 -08004941#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4942 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004943 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004944 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004945 return( PSA_ERROR_BAD_STATE );
4946 }
4947
4948exit:
4949 if( status != PSA_SUCCESS )
4950 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004951 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004952 * This allows us to differentiate between exhausted operations and
4953 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4954 * operations. */
4955 psa_algorithm_t alg = operation->alg;
4956 psa_key_derivation_abort( operation );
4957 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004958 memset( output, '!', output_length );
4959 }
4960 return( status );
4961}
4962
David Brown7807bf72021-02-09 16:28:23 -07004963#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004964static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4965{
4966 if( data_size >= 8 )
4967 mbedtls_des_key_set_parity( data );
4968 if( data_size >= 16 )
4969 mbedtls_des_key_set_parity( data + 8 );
4970 if( data_size >= 24 )
4971 mbedtls_des_key_set_parity( data + 16 );
4972}
David Brown7807bf72021-02-09 16:28:23 -07004973#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004974
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004975static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004976 psa_key_slot_t *slot,
4977 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004978 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004979{
4980 uint8_t *data = NULL;
4981 size_t bytes = PSA_BITS_TO_BYTES( bits );
4982 psa_status_t status;
4983
Gilles Peskine8e338702019-07-30 20:06:31 +02004984 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004985 return( PSA_ERROR_INVALID_ARGUMENT );
4986 if( bits % 8 != 0 )
4987 return( PSA_ERROR_INVALID_ARGUMENT );
4988 data = mbedtls_calloc( 1, bytes );
4989 if( data == NULL )
4990 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4991
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004992 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004993 if( status != PSA_SUCCESS )
4994 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004995#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004996 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004997 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07004998#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004999
5000 status = psa_allocate_buffer_to_slot( slot, bytes );
5001 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005002 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005003
Ronald Cronbf33c932020-11-28 18:06:53 +01005004 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005005 psa_key_attributes_t attributes = {
5006 .core = slot->attr
5007 };
5008
Ronald Cronbf33c932020-11-28 18:06:53 +01005009 status = psa_driver_wrapper_import_key( &attributes,
5010 data, bytes,
5011 slot->key.data,
5012 slot->key.bytes,
5013 &slot->key.bytes, &bits );
5014 if( bits != slot->attr.bits )
5015 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005016
5017exit:
5018 mbedtls_free( data );
5019 return( status );
5020}
5021
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005022psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005024 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005025{
5026 psa_status_t status;
5027 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005028 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005029
Ronald Cron81709fc2020-11-14 12:10:32 +01005030 *key = MBEDTLS_SVC_KEY_ID_INIT;
5031
Gilles Peskine0f84d622019-09-12 19:03:13 +02005032 /* Reject any attempt to create a zero-length key so that we don't
5033 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5034 if( psa_get_key_bits( attributes ) == 0 )
5035 return( PSA_ERROR_INVALID_ARGUMENT );
5036
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005037 if( ! operation->can_output_key )
5038 return( PSA_ERROR_NOT_PERMITTED );
5039
Ronald Cron81709fc2020-11-14 12:10:32 +01005040 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5041 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005042#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5043 if( driver != NULL )
5044 {
5045 /* Deriving a key in a secure element is not implemented yet. */
5046 status = PSA_ERROR_NOT_SUPPORTED;
5047 }
5048#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005049 if( status == PSA_SUCCESS )
5050 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005051 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005052 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005054 }
5055 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005056 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005057 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005058 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005059
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005060 return( status );
5061}
5062
Gilles Peskineeab56e42018-07-12 17:12:33 +02005063
5064
5065/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005066/* Key derivation */
5067/****************************************************************/
5068
Steven Cooreman932ffb72021-02-15 12:14:32 +01005069#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005070static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005071 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005072 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005073{
John Durkop07cc04a2020-11-16 22:08:34 -08005074 int is_kdf_alg_supported;
5075
Janos Follath5fe19732019-06-20 15:09:30 +01005076 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5077 * initialisers for this union leave some bytes unspecified.) */
5078 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5079
Gilles Peskine969c5d62019-01-16 15:53:06 +01005080 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005081#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005082 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5083 is_kdf_alg_supported = 1;
5084 else
5085#endif
5086#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5087 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5088 is_kdf_alg_supported = 1;
5089 else
5090#endif
5091#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5092 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5093 is_kdf_alg_supported = 1;
5094 else
5095#endif
5096 is_kdf_alg_supported = 0;
5097
5098 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005099 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005100 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005101 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005102 if( hash_size == 0 )
5103 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005104 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5105 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005106 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005107 {
5108 return( PSA_ERROR_NOT_SUPPORTED );
5109 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005111 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112 }
John Durkop07cc04a2020-11-16 22:08:34 -08005113
5114 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005115}
John Durkop07cc04a2020-11-16 22:08:34 -08005116#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005117
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005118psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005119 psa_algorithm_t alg )
5120{
5121 psa_status_t status;
5122
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005124 return( PSA_ERROR_BAD_STATE );
5125
Gilles Peskine6843c292019-01-18 16:44:49 +01005126 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5127 return( PSA_ERROR_INVALID_ARGUMENT );
5128 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005129 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005130#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005131 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005133#else
5134 return( PSA_ERROR_NOT_SUPPORTED );
5135#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005136 }
5137 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5138 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005139#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005141#else
5142 return( PSA_ERROR_NOT_SUPPORTED );
5143#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005144 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005145 else
5146 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005147
5148 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005149 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005150 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005151}
5152
John Durkopd0321952020-10-29 21:37:36 -07005153#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005154static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005155 psa_algorithm_t hash_alg,
5156 psa_key_derivation_step_t step,
5157 const uint8_t *data,
5158 size_t data_length )
5159{
5160 psa_status_t status;
5161 switch( step )
5162 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005163 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005164 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005165 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005166 status = psa_hmac_setup_internal( &hkdf->hmac,
5167 data, data_length,
5168 hash_alg );
5169 if( status != PSA_SUCCESS )
5170 return( status );
5171 hkdf->state = HKDF_STATE_STARTED;
5172 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005173 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005174 /* If no salt was provided, use an empty salt. */
5175 if( hkdf->state == HKDF_STATE_INIT )
5176 {
5177 status = psa_hmac_setup_internal( &hkdf->hmac,
5178 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005179 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180 if( status != PSA_SUCCESS )
5181 return( status );
5182 hkdf->state = HKDF_STATE_STARTED;
5183 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005184 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005186 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5187 data, data_length );
5188 if( status != PSA_SUCCESS )
5189 return( status );
5190 status = psa_hmac_finish_internal( &hkdf->hmac,
5191 hkdf->prk,
5192 sizeof( hkdf->prk ) );
5193 if( status != PSA_SUCCESS )
5194 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005195 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005196 hkdf->block_number = 0;
5197 hkdf->state = HKDF_STATE_KEYED;
5198 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005199 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005200 if( hkdf->state == HKDF_STATE_OUTPUT )
5201 return( PSA_ERROR_BAD_STATE );
5202 if( hkdf->info_set )
5203 return( PSA_ERROR_BAD_STATE );
5204 hkdf->info_length = data_length;
5205 if( data_length != 0 )
5206 {
5207 hkdf->info = mbedtls_calloc( 1, data_length );
5208 if( hkdf->info == NULL )
5209 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5210 memcpy( hkdf->info, data, data_length );
5211 }
5212 hkdf->info_set = 1;
5213 return( PSA_SUCCESS );
5214 default:
5215 return( PSA_ERROR_INVALID_ARGUMENT );
5216 }
5217}
John Durkop07cc04a2020-11-16 22:08:34 -08005218#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005219
John Durkop07cc04a2020-11-16 22:08:34 -08005220#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5221 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005222static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5223 const uint8_t *data,
5224 size_t data_length )
5225{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005226 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005227 return( PSA_ERROR_BAD_STATE );
5228
Janos Follathd6dce9f2019-07-04 09:11:38 +01005229 if( data_length != 0 )
5230 {
5231 prf->seed = mbedtls_calloc( 1, data_length );
5232 if( prf->seed == NULL )
5233 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005234
Janos Follathd6dce9f2019-07-04 09:11:38 +01005235 memcpy( prf->seed, data, data_length );
5236 prf->seed_length = data_length;
5237 }
Janos Follathf08e2652019-06-13 09:05:41 +01005238
Gilles Peskine43f958b2020-12-13 14:55:14 +01005239 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005240
5241 return( PSA_SUCCESS );
5242}
5243
Janos Follath81550542019-06-13 14:26:34 +01005244static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5245 psa_algorithm_t hash_alg,
5246 const uint8_t *data,
5247 size_t data_length )
5248{
5249 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005250 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005251 return( PSA_ERROR_BAD_STATE );
5252
5253 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5254 if( status != PSA_SUCCESS )
5255 return( status );
5256
Gilles Peskine43f958b2020-12-13 14:55:14 +01005257 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005258
5259 return( PSA_SUCCESS );
5260}
5261
Janos Follath63028dd2019-06-13 09:15:47 +01005262static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5263 const uint8_t *data,
5264 size_t data_length )
5265{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005266 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005267 return( PSA_ERROR_BAD_STATE );
5268
Janos Follathd6dce9f2019-07-04 09:11:38 +01005269 if( data_length != 0 )
5270 {
5271 prf->label = mbedtls_calloc( 1, data_length );
5272 if( prf->label == NULL )
5273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005274
Janos Follathd6dce9f2019-07-04 09:11:38 +01005275 memcpy( prf->label, data, data_length );
5276 prf->label_length = data_length;
5277 }
Janos Follath63028dd2019-06-13 09:15:47 +01005278
Gilles Peskine43f958b2020-12-13 14:55:14 +01005279 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005280
5281 return( PSA_SUCCESS );
5282}
5283
Janos Follathb03233e2019-06-11 15:30:30 +01005284static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5285 psa_algorithm_t hash_alg,
5286 psa_key_derivation_step_t step,
5287 const uint8_t *data,
5288 size_t data_length )
5289{
Janos Follathb03233e2019-06-11 15:30:30 +01005290 switch( step )
5291 {
Janos Follathf08e2652019-06-13 09:05:41 +01005292 case PSA_KEY_DERIVATION_INPUT_SEED:
5293 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005294 case PSA_KEY_DERIVATION_INPUT_SECRET:
5295 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005296 case PSA_KEY_DERIVATION_INPUT_LABEL:
5297 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005298 default:
5299 return( PSA_ERROR_INVALID_ARGUMENT );
5300 }
5301}
John Durkop07cc04a2020-11-16 22:08:34 -08005302#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5303 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5304
5305#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5306static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5307 psa_tls12_prf_key_derivation_t *prf,
5308 psa_algorithm_t hash_alg,
5309 const uint8_t *data,
5310 size_t data_length )
5311{
5312 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005313 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005314 uint8_t *cur = pms;
5315
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005316 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005317 return( PSA_ERROR_INVALID_ARGUMENT );
5318
5319 /* Quoting RFC 4279, Section 2:
5320 *
5321 * The premaster secret is formed as follows: if the PSK is N octets
5322 * long, concatenate a uint16 with the value N, N zero octets, a second
5323 * uint16 with the value N, and the PSK itself.
5324 */
5325
5326 *cur++ = ( data_length >> 8 ) & 0xff;
5327 *cur++ = ( data_length >> 0 ) & 0xff;
5328 memset( cur, 0, data_length );
5329 cur += data_length;
5330 *cur++ = pms[0];
5331 *cur++ = pms[1];
5332 memcpy( cur, data, data_length );
5333 cur += data_length;
5334
5335 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5336
5337 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5338 return( status );
5339}
Janos Follath6660f0e2019-06-17 08:44:03 +01005340
5341static psa_status_t psa_tls12_prf_psk_to_ms_input(
5342 psa_tls12_prf_key_derivation_t *prf,
5343 psa_algorithm_t hash_alg,
5344 psa_key_derivation_step_t step,
5345 const uint8_t *data,
5346 size_t data_length )
5347{
5348 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005349 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005350 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5351 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005352 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005353
5354 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5355}
John Durkop07cc04a2020-11-16 22:08:34 -08005356#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005357
Gilles Peskineb8965192019-09-24 16:21:10 +02005358/** Check whether the given key type is acceptable for the given
5359 * input step of a key derivation.
5360 *
5361 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5362 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5363 * Both secret and non-secret inputs can alternatively have the type
5364 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5365 * that the input was passed as a buffer rather than via a key object.
5366 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005367static int psa_key_derivation_check_input_type(
5368 psa_key_derivation_step_t step,
5369 psa_key_type_t key_type )
5370{
5371 switch( step )
5372 {
5373 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005374 if( key_type == PSA_KEY_TYPE_DERIVE )
5375 return( PSA_SUCCESS );
5376 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005377 return( PSA_SUCCESS );
5378 break;
5379 case PSA_KEY_DERIVATION_INPUT_LABEL:
5380 case PSA_KEY_DERIVATION_INPUT_SALT:
5381 case PSA_KEY_DERIVATION_INPUT_INFO:
5382 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005383 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5384 return( PSA_SUCCESS );
5385 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005386 return( PSA_SUCCESS );
5387 break;
5388 }
5389 return( PSA_ERROR_INVALID_ARGUMENT );
5390}
5391
Janos Follathb80a94e2019-06-12 15:54:46 +01005392static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005394 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005395 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005396 const uint8_t *data,
5397 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005398{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005399 psa_status_t status;
5400 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5401
5402 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005403 if( status != PSA_SUCCESS )
5404 goto exit;
5405
John Durkopd0321952020-10-29 21:37:36 -07005406#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005407 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005408 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005410 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005411 step, data, data_length );
5412 }
John Durkop07cc04a2020-11-16 22:08:34 -08005413 else
5414#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5415#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5416 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005417 {
Janos Follathb03233e2019-06-11 15:30:30 +01005418 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5419 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5420 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005421 }
John Durkop07cc04a2020-11-16 22:08:34 -08005422 else
5423#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5424#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5425 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005426 {
5427 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5428 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5429 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005430 }
5431 else
John Durkop07cc04a2020-11-16 22:08:34 -08005432#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005433 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005434 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005435 (void) data;
5436 (void) data_length;
5437 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005438 return( PSA_ERROR_BAD_STATE );
5439 }
5440
Gilles Peskine224b0d62019-09-23 18:13:17 +02005441exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005442 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005443 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005444 return( status );
5445}
5446
Janos Follath51f4a0f2019-06-14 11:35:55 +01005447psa_status_t psa_key_derivation_input_bytes(
5448 psa_key_derivation_operation_t *operation,
5449 psa_key_derivation_step_t step,
5450 const uint8_t *data,
5451 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005452{
Gilles Peskineb8965192019-09-24 16:21:10 +02005453 return( psa_key_derivation_input_internal( operation, step,
5454 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005455 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005456}
5457
Janos Follath51f4a0f2019-06-14 11:35:55 +01005458psa_status_t psa_key_derivation_input_key(
5459 psa_key_derivation_operation_t *operation,
5460 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005461 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005462{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005463 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005464 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005465 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005466
Ronald Cron5c522922020-11-14 16:35:34 +01005467 status = psa_get_and_lock_transparent_key_slot_with_policy(
5468 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005469 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005470 {
5471 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005472 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005473 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005474
5475 /* Passing a key object as a SECRET input unlocks the permission
5476 * to output to a key object. */
5477 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5478 operation->can_output_key = 1;
5479
Ronald Cronf95a2b12020-10-22 15:24:49 +02005480 status = psa_key_derivation_input_internal( operation,
5481 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005482 slot->key.data,
5483 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005484
Ronald Cron5c522922020-11-14 16:35:34 +01005485 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005486
Ronald Cron5c522922020-11-14 16:35:34 +01005487 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005488}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005489
5490
5491
5492/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005493/* Key agreement */
5494/****************************************************************/
5495
John Durkop6ba40d12020-11-10 08:50:04 -08005496#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005497static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5498 size_t peer_key_length,
5499 const mbedtls_ecp_keypair *our_key,
5500 uint8_t *shared_secret,
5501 size_t shared_secret_size,
5502 size_t *shared_secret_length )
5503{
Steven Cooremand4867872020-08-05 16:31:39 +02005504 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005505 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005506 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005507 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005508 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005509 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005510
Ronald Cron90857082020-11-25 14:58:33 +01005511 status = mbedtls_psa_ecp_load_representation(
5512 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005513 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005514 peer_key,
5515 peer_key_length,
5516 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005517 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005518 goto exit;
5519
Jaeden Amero97271b32019-01-10 19:38:51 +00005520 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005521 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005522 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005523 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005524 status = mbedtls_to_psa_error(
5525 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5526 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005527 goto exit;
5528
Jaeden Amero97271b32019-01-10 19:38:51 +00005529 status = mbedtls_to_psa_error(
5530 mbedtls_ecdh_calc_secret( &ecdh,
5531 shared_secret_length,
5532 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005533 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005534 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005535 if( status != PSA_SUCCESS )
5536 goto exit;
5537 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5538 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005539
5540exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005541 if( status != PSA_SUCCESS )
5542 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005543 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005544 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005545 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005546
Jaeden Amero97271b32019-01-10 19:38:51 +00005547 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005548}
John Durkop6ba40d12020-11-10 08:50:04 -08005549#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005550
Gilles Peskine01d718c2018-09-18 12:01:02 +02005551#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5552
Gilles Peskine0216fe12019-04-11 21:23:21 +02005553static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5554 psa_key_slot_t *private_key,
5555 const uint8_t *peer_key,
5556 size_t peer_key_length,
5557 uint8_t *shared_secret,
5558 size_t shared_secret_size,
5559 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005560{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005561 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005562 {
John Durkop6ba40d12020-11-10 08:50:04 -08005563#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005564 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005565 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005566 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005567 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005568 psa_status_t status = mbedtls_psa_ecp_load_representation(
5569 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005570 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005571 private_key->key.data,
5572 private_key->key.bytes,
5573 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005574 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005575 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005576 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005577 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005578 shared_secret, shared_secret_size,
5579 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005580 mbedtls_ecp_keypair_free( ecp );
5581 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005582 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005583#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005584 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005585 (void) private_key;
5586 (void) peer_key;
5587 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005588 (void) shared_secret;
5589 (void) shared_secret_size;
5590 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005591 return( PSA_ERROR_NOT_SUPPORTED );
5592 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005593}
5594
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005595/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005596 * to potentially free embedded data structures and wipe confidential data.
5597 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005598static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005599 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005600 psa_key_slot_t *private_key,
5601 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005602 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005603{
5604 psa_status_t status;
5605 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5606 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005607 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005608
5609 /* Step 1: run the secret agreement algorithm to generate the shared
5610 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005611 status = psa_key_agreement_raw_internal( ka_alg,
5612 private_key,
5613 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005614 shared_secret,
5615 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005616 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005617 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005618 goto exit;
5619
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005620 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005621 * the shared secret. A shared secret is permitted wherever a key
5622 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005623 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005624 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005625 shared_secret,
5626 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005627exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005628 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005629 return( status );
5630}
5631
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005632psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005633 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005634 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005635 const uint8_t *peer_key,
5636 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005637{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005638 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005639 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005640 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005641
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005642 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005643 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005644 status = psa_get_and_lock_transparent_key_slot_with_policy(
5645 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005646 if( status != PSA_SUCCESS )
5647 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005648 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005649 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005650 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005651 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005652 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005653 else
5654 {
5655 /* If a private key has been added as SECRET, we allow the derived
5656 * key material to be used as a key in PSA Crypto. */
5657 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5658 operation->can_output_key = 1;
5659 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005660
Ronald Cron5c522922020-11-14 16:35:34 +01005661 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005662
Ronald Cron5c522922020-11-14 16:35:34 +01005663 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005664}
5665
Gilles Peskinebe697d82019-05-16 18:00:41 +02005666psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005667 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005668 const uint8_t *peer_key,
5669 size_t peer_key_length,
5670 uint8_t *output,
5671 size_t output_size,
5672 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005673{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005674 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005675 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005676 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005677
5678 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5679 {
5680 status = PSA_ERROR_INVALID_ARGUMENT;
5681 goto exit;
5682 }
Ronald Cron5c522922020-11-14 16:35:34 +01005683 status = psa_get_and_lock_transparent_key_slot_with_policy(
5684 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005685 if( status != PSA_SUCCESS )
5686 goto exit;
5687
5688 status = psa_key_agreement_raw_internal( alg, slot,
5689 peer_key, peer_key_length,
5690 output, output_size,
5691 output_length );
5692
5693exit:
5694 if( status != PSA_SUCCESS )
5695 {
5696 /* If an error happens and is not handled properly, the output
5697 * may be used as a key to protect sensitive data. Arrange for such
5698 * a key to be random, which is likely to result in decryption or
5699 * verification errors. This is better than filling the buffer with
5700 * some constant data such as zeros, which would result in the data
5701 * being protected with a reproducible, easily knowable key.
5702 */
5703 psa_generate_random( output, output_size );
5704 *output_length = output_size;
5705 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005706
Ronald Cron5c522922020-11-14 16:35:34 +01005707 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005708
Ronald Cron5c522922020-11-14 16:35:34 +01005709 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005710}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005711
5712
Gilles Peskine30524eb2020-11-13 17:02:26 +01005713
Gilles Peskine86a440b2018-11-12 18:39:40 +01005714/****************************************************************/
5715/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005716/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005717
Gilles Peskine30524eb2020-11-13 17:02:26 +01005718/** Initialize the PSA random generator.
5719 */
5720static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5721{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005722#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5723 memset( rng, 0, sizeof( *rng ) );
5724#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5725
Gilles Peskine30524eb2020-11-13 17:02:26 +01005726 /* Set default configuration if
5727 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5728 if( rng->entropy_init == NULL )
5729 rng->entropy_init = mbedtls_entropy_init;
5730 if( rng->entropy_free == NULL )
5731 rng->entropy_free = mbedtls_entropy_free;
5732
5733 rng->entropy_init( &rng->entropy );
5734#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5735 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5736 /* The PSA entropy injection feature depends on using NV seed as an entropy
5737 * source. Add NV seed as an entropy source for PSA entropy injection. */
5738 mbedtls_entropy_add_source( &rng->entropy,
5739 mbedtls_nv_seed_poll, NULL,
5740 MBEDTLS_ENTROPY_BLOCK_SIZE,
5741 MBEDTLS_ENTROPY_SOURCE_STRONG );
5742#endif
5743
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005744 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005745#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005746}
5747
5748/** Deinitialize the PSA random generator.
5749 */
5750static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5751{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005752#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5753 memset( rng, 0, sizeof( *rng ) );
5754#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005755 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005756 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005757#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005758}
5759
5760/** Seed the PSA random generator.
5761 */
5762static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5763{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005764#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5765 /* Do nothing: the external RNG seeds itself. */
5766 (void) rng;
5767 return( PSA_SUCCESS );
5768#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005769 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005770 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5771 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005772 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005773#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005774}
5775
Gilles Peskinee59236f2018-01-27 23:32:46 +01005776psa_status_t psa_generate_random( uint8_t *output,
5777 size_t output_size )
5778{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005779 GUARD_MODULE_INITIALIZED;
5780
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005781#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5782
5783 size_t output_length = 0;
5784 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5785 output, output_size,
5786 &output_length );
5787 if( status != PSA_SUCCESS )
5788 return( status );
5789 /* Breaking up a request into smaller chunks is currently not supported
5790 * for the extrernal RNG interface. */
5791 if( output_length != output_size )
5792 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5793 return( PSA_SUCCESS );
5794
5795#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5796
Gilles Peskine71ddab92021-01-04 21:01:07 +01005797 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005798 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005799 size_t request_size =
5800 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5801 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5802 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005803 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
5804 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02005805 if( ret != 0 )
5806 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01005807 output_size -= request_size;
5808 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005809 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005810 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005811#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005812}
5813
Gilles Peskine8814fc42020-12-14 15:33:44 +01005814/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005815 *
5816 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5817 * `psa_generate_random(...)`. The state parameter is ignored since the
5818 * PSA API doesn't support passing an explicit state.
5819 *
5820 * In the non-external case, psa_generate_random() calls an
5821 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5822 * and semantics as mbedtls_psa_get_random(). As an optimization,
5823 * instead of doing this back-and-forth between the PSA API and the
5824 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5825 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005826 */
5827#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5828int mbedtls_psa_get_random( void *p_rng,
5829 unsigned char *output,
5830 size_t output_size )
5831{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005832 /* This function takes a pointer to the RNG state because that's what
5833 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5834 * its own state internally and doesn't let the caller access that state.
5835 * So we just ignore the state parameter, and in practice we'll pass
5836 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005837 (void) p_rng;
5838 psa_status_t status = psa_generate_random( output, output_size );
5839 if( status == PSA_SUCCESS )
5840 return( 0 );
5841 else
5842 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
5843}
5844#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5845
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005846#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5847#include "mbedtls/entropy_poll.h"
5848
Gilles Peskine7228da22019-07-15 11:06:15 +02005849psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005850 size_t seed_size )
5851{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005852 if( global_data.initialized )
5853 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005854
5855 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5856 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5857 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5858 return( PSA_ERROR_INVALID_ARGUMENT );
5859
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005860 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005861}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005862#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005863
Ronald Cron3772afe2021-02-08 16:10:05 +01005864/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005865 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005866 * \param type The key type
5867 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005868 *
5869 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005870 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005871 * \retval #PSA_ERROR_INVALID_ARGUMENT
5872 * The size in bits of the key is not valid.
5873 * \retval #PSA_ERROR_NOT_SUPPORTED
5874 * The type and/or the size in bits of the key or the combination of
5875 * the two is not supported.
5876 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005877static psa_status_t psa_validate_key_type_and_size_for_key_generation(
5878 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005879{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005880 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005881
Gilles Peskinee59236f2018-01-27 23:32:46 +01005882 if( key_type_is_raw_bytes( type ) )
5883 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02005884 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005885 if( status != PSA_SUCCESS )
5886 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005887 }
5888 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005889#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005890 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5891 {
5892 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5893 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005894
Ronald Cron01b2aba2020-10-05 09:42:02 +02005895 /* Accept only byte-aligned keys, for the same reasons as
5896 * in psa_import_rsa_key(). */
5897 if( bits % 8 != 0 )
5898 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005899 }
5900 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005901#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005902
Ronald Cron5c4d3862020-12-07 11:07:24 +01005903#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005904 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5905 {
Ronald Crond81ab562021-02-16 09:01:16 +01005906 /* To avoid empty block, return successfully here. */
5907 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005908 }
5909 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005910#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005911 {
5912 return( PSA_ERROR_NOT_SUPPORTED );
5913 }
5914
5915 return( PSA_SUCCESS );
5916}
5917
Ronald Cron55ed0592020-10-05 10:30:40 +02005918psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005919 const psa_key_attributes_t *attributes,
5920 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005921{
5922 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005923 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005924
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005925 if( ( attributes->domain_parameters == NULL ) &&
5926 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005927 return( PSA_ERROR_INVALID_ARGUMENT );
5928
Ronald Cron01b2aba2020-10-05 09:42:02 +02005929 if( key_type_is_raw_bytes( type ) )
5930 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005931 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005932 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005933 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005934
David Brown12ca5032021-02-11 11:02:00 -07005935#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005936 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005937 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07005938#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005939 }
5940 else
5941
John Durkop07cc04a2020-11-16 22:08:34 -08005942#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005943 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005944 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01005945 return( mbedtls_psa_rsa_generate_key( attributes,
5946 key_buffer,
5947 key_buffer_size,
5948 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005949 }
5950 else
John Durkop07cc04a2020-11-16 22:08:34 -08005951#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005952
John Durkop9814fa22020-11-04 12:28:15 -08005953#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005954 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005955 {
Ronald Cron7023db52020-11-20 18:17:42 +01005956 return( mbedtls_psa_ecp_generate_key( attributes,
5957 key_buffer,
5958 key_buffer_size,
5959 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005960 }
5961 else
John Durkop9814fa22020-11-04 12:28:15 -08005962#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005963 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005964 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005965 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005966 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005967
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005968 return( PSA_SUCCESS );
5969}
Darryl Green0c6575a2018-11-07 16:05:30 +00005970
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005971psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005972 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005973{
5974 psa_status_t status;
5975 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005976 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005977 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005978
Ronald Cron81709fc2020-11-14 12:10:32 +01005979 *key = MBEDTLS_SVC_KEY_ID_INIT;
5980
Gilles Peskine0f84d622019-09-12 19:03:13 +02005981 /* Reject any attempt to create a zero-length key so that we don't
5982 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5983 if( psa_get_key_bits( attributes ) == 0 )
5984 return( PSA_ERROR_INVALID_ARGUMENT );
5985
Ronald Cron81709fc2020-11-14 12:10:32 +01005986 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
5987 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005988 if( status != PSA_SUCCESS )
5989 goto exit;
5990
Ronald Cron977c2472020-10-13 08:32:21 +02005991 /* In the case of a transparent key or an opaque key stored in local
5992 * storage (thus not in the case of generating a key in a secure element
5993 * or cryptoprocessor with storage), we have to allocate a buffer to
5994 * hold the generated key material. */
5995 if( slot->key.data == NULL )
5996 {
5997 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
5998 PSA_KEY_LOCATION_LOCAL_STORAGE )
5999 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006000 status = psa_validate_key_type_and_size_for_key_generation(
6001 attributes->core.type, attributes->core.bits );
6002 if( status != PSA_SUCCESS )
6003 goto exit;
6004
6005 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6006 attributes->core.type,
6007 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006008 }
6009 else
6010 {
6011 status = psa_driver_wrapper_get_key_buffer_size(
6012 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006013 if( status != PSA_SUCCESS )
6014 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006015 }
Ronald Cron977c2472020-10-13 08:32:21 +02006016
6017 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6018 if( status != PSA_SUCCESS )
6019 goto exit;
6020 }
6021
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006022 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006023 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006024
Ronald Cron2b56bc82020-10-05 10:02:26 +02006025 if( status != PSA_SUCCESS )
6026 psa_remove_key_data_from_memory( slot );
6027
Gilles Peskine11792082019-08-06 18:36:36 +02006028exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006029 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006030 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006031 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006032 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006033
Darryl Green0c6575a2018-11-07 16:05:30 +00006034 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006035}
6036
Gilles Peskinee59236f2018-01-27 23:32:46 +01006037/****************************************************************/
6038/* Module setup */
6039/****************************************************************/
6040
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006041#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006042psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6043 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6044 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6045{
6046 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6047 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006048 global_data.rng.entropy_init = entropy_init;
6049 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006050 return( PSA_SUCCESS );
6051}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006052#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006053
Gilles Peskinee59236f2018-01-27 23:32:46 +01006054void mbedtls_psa_crypto_free( void )
6055{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006056 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006057 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6058 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006059 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006060 }
6061 /* Wipe all remaining data, including configuration.
6062 * In particular, this sets all state indicator to the value
6063 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006064 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006065#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006066 /* Unregister all secure element drivers, so that we restart from
6067 * a pristine state. */
6068 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006069#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006070}
6071
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006072#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6073/** Recover a transaction that was interrupted by a power failure.
6074 *
6075 * This function is called during initialization, before psa_crypto_init()
6076 * returns. If this function returns a failure status, the initialization
6077 * fails.
6078 */
6079static psa_status_t psa_crypto_recover_transaction(
6080 const psa_crypto_transaction_t *transaction )
6081{
6082 switch( transaction->unknown.type )
6083 {
6084 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6085 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006086 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006087 * is implemented.
6088 * https://github.com/ARMmbed/mbed-crypto/issues/218
6089 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006090 default:
6091 /* We found an unsupported transaction in the storage.
6092 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006093 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006094 }
6095}
6096#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6097
Gilles Peskinee59236f2018-01-27 23:32:46 +01006098psa_status_t psa_crypto_init( void )
6099{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006100 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006101
Gilles Peskinec6b69072018-11-20 21:42:52 +01006102 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006103 if( global_data.initialized != 0 )
6104 return( PSA_SUCCESS );
6105
Gilles Peskine30524eb2020-11-13 17:02:26 +01006106 /* Initialize and seed the random generator. */
6107 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006108 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006109 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006110 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006111 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006112 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006113
Gilles Peskine66fb1262018-12-10 16:29:04 +01006114 status = psa_initialize_key_slots( );
6115 if( status != PSA_SUCCESS )
6116 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006117
Gilles Peskined9348f22019-10-01 15:22:29 +02006118#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6119 status = psa_init_all_se_drivers( );
6120 if( status != PSA_SUCCESS )
6121 goto exit;
6122#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6123
Gilles Peskine4b734222019-07-24 15:56:31 +02006124#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006125 status = psa_crypto_load_transaction( );
6126 if( status == PSA_SUCCESS )
6127 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006128 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6129 if( status != PSA_SUCCESS )
6130 goto exit;
6131 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006132 }
6133 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6134 {
6135 /* There's no transaction to complete. It's all good. */
6136 status = PSA_SUCCESS;
6137 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006138#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006139
Gilles Peskinec6b69072018-11-20 21:42:52 +01006140 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006141 global_data.initialized = 1;
6142
6143exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006144 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006145 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006146 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006147}
6148
6149#endif /* MBEDTLS_PSA_CRYPTO_C */