blob: e047489b50029b06e1a790c258126a2577e06dff [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035#include "psa_crypto_ecp.h"
36#include "psa_crypto_rsa.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020038#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020039#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010040#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010041/* Include internal declarations that are useful for implementing persistently
42 * stored keys. */
43#include "psa_crypto_storage.h"
44
Gilles Peskineb2b64d32020-12-14 16:43:58 +010045#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010046
Gilles Peskineb46bef22019-07-30 21:32:04 +020047#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#include <stdlib.h>
49#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020051#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#define mbedtls_calloc calloc
53#define mbedtls_free free
54#endif
55
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010056#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020058#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000059#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020060#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/blowfish.h"
62#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020063#include "mbedtls/chacha20.h"
64#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/cipher.h"
66#include "mbedtls/ccm.h"
67#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020069#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010071#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/error.h"
73#include "mbedtls/gcm.h"
74#include "mbedtls/md2.h"
75#include "mbedtls/md4.h"
76#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010077#include "mbedtls/md.h"
78#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010079#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010081#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000082#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010084#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085#include "mbedtls/sha1.h"
86#include "mbedtls/sha256.h"
87#include "mbedtls/sha512.h"
88#include "mbedtls/xtea.h"
89
Gilles Peskine996deb12018-08-01 15:45:45 +020090#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
91
Gilles Peskine9ef733f2018-02-07 21:05:37 +010092/* constant-time buffer comparison */
93static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
94{
95 size_t i;
96 unsigned char diff = 0;
97
98 for( i = 0; i < n; i++ )
99 diff |= a[i] ^ b[i];
100
101 return( diff );
102}
103
104
105
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100106/****************************************************************/
107/* Global data, support functions and library management */
108/****************************************************************/
109
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200110static int key_type_is_raw_bytes( psa_key_type_t type )
111{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200112 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200113}
114
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100115/* Values for psa_global_data_t::rng_state */
116#define RNG_NOT_INITIALIZED 0
117#define RNG_INITIALIZED 1
118#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100119
Gilles Peskine2d277862018-06-18 15:41:12 +0200120typedef struct
121{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100122 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100123 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100124 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100125} psa_global_data_t;
126
127static psa_global_data_t global_data;
128
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100129#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
130mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
131 &global_data.rng.drbg;
132#endif
133
itayzafrir0adf0fc2018-09-06 16:24:41 +0300134#define GUARD_MODULE_INITIALIZED \
135 if( global_data.initialized == 0 ) \
136 return( PSA_ERROR_BAD_STATE );
137
Steven Cooreman01164162020-07-20 15:31:37 +0200138psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100139{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100140 /* Mbed TLS error codes can combine a high-level error code and a
141 * low-level error code. The low-level error usually reflects the
142 * root cause better, so dispatch on that preferably. */
143 int low_level_ret = - ( -ret & 0x007f );
144 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145 {
146 case 0:
147 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100148
149 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
150 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
151 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
152 return( PSA_ERROR_NOT_SUPPORTED );
153 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
154 return( PSA_ERROR_HARDWARE_FAILURE );
155
156 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
157 return( PSA_ERROR_HARDWARE_FAILURE );
158
Gilles Peskine9a944802018-06-21 09:35:35 +0200159 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
160 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
161 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
162 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
163 case MBEDTLS_ERR_ASN1_INVALID_DATA:
164 return( PSA_ERROR_INVALID_ARGUMENT );
165 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
166 return( PSA_ERROR_INSUFFICIENT_MEMORY );
167 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
168 return( PSA_ERROR_BUFFER_TOO_SMALL );
169
Jaeden Amero93e21112019-02-20 13:57:28 +0000170#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000171 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
173 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
174#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
Jaeden Amero93e21112019-02-20 13:57:28 +0000180#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000181 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000182#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
183 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
184#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
186 return( PSA_ERROR_NOT_SUPPORTED );
187 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
188 return( PSA_ERROR_HARDWARE_FAILURE );
189
190 case MBEDTLS_ERR_CCM_BAD_INPUT:
191 return( PSA_ERROR_INVALID_ARGUMENT );
192 case MBEDTLS_ERR_CCM_AUTH_FAILED:
193 return( PSA_ERROR_INVALID_SIGNATURE );
194 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
195 return( PSA_ERROR_HARDWARE_FAILURE );
196
Gilles Peskine26869f22019-05-06 15:25:00 +0200197 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199
200 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
201 return( PSA_ERROR_BAD_STATE );
202 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
203 return( PSA_ERROR_INVALID_SIGNATURE );
204
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
206 return( PSA_ERROR_NOT_SUPPORTED );
207 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
208 return( PSA_ERROR_INVALID_ARGUMENT );
209 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
210 return( PSA_ERROR_INSUFFICIENT_MEMORY );
211 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
212 return( PSA_ERROR_INVALID_PADDING );
213 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200214 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
216 return( PSA_ERROR_INVALID_SIGNATURE );
217 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200218 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100219 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
220 return( PSA_ERROR_HARDWARE_FAILURE );
221
222 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
223 return( PSA_ERROR_HARDWARE_FAILURE );
224
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
226 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100227 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
228 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100229 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
232 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
235 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100236#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
238 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
239 return( PSA_ERROR_NOT_SUPPORTED );
240 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
Gilles Peskinee59236f2018-01-27 23:32:46 +0100243 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
244 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
245 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
246 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100247
248 case MBEDTLS_ERR_GCM_AUTH_FAILED:
249 return( PSA_ERROR_INVALID_SIGNATURE );
250 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200251 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100252 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskine82e57d12020-11-13 21:31:17 +0100255#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
256 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100257 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
258 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100259 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
260 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
261 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
262 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
263 return( PSA_ERROR_NOT_SUPPORTED );
264 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
265 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
266#endif
267
Gilles Peskinea5905292018-02-07 20:59:33 +0100268 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
269 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
270 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
271 return( PSA_ERROR_HARDWARE_FAILURE );
272
273 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
274 return( PSA_ERROR_NOT_SUPPORTED );
275 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_MD_ALLOC_FAILED:
278 return( PSA_ERROR_INSUFFICIENT_MEMORY );
279 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
280 return( PSA_ERROR_STORAGE_FAILURE );
281 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
282 return( PSA_ERROR_HARDWARE_FAILURE );
283
Gilles Peskinef76aa772018-10-29 19:24:33 +0100284 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
285 return( PSA_ERROR_STORAGE_FAILURE );
286 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
291 return( PSA_ERROR_BUFFER_TOO_SMALL );
292 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
299 return( PSA_ERROR_INSUFFICIENT_MEMORY );
300
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 case MBEDTLS_ERR_PK_ALLOC_FAILED:
302 return( PSA_ERROR_INSUFFICIENT_MEMORY );
303 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
304 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
305 return( PSA_ERROR_INVALID_ARGUMENT );
306 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100308 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
309 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
312 return( PSA_ERROR_NOT_SUPPORTED );
313 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
314 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
315 return( PSA_ERROR_NOT_PERMITTED );
316 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
317 return( PSA_ERROR_INVALID_ARGUMENT );
318 case MBEDTLS_ERR_PK_INVALID_ALG:
319 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
320 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
321 return( PSA_ERROR_NOT_SUPPORTED );
322 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
323 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100324 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
Gilles Peskineff2d2002019-05-06 15:26:23 +0200327 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
328 return( PSA_ERROR_HARDWARE_FAILURE );
329 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
330 return( PSA_ERROR_NOT_SUPPORTED );
331
Gilles Peskinea5905292018-02-07 20:59:33 +0100332 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334
335 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_RSA_INVALID_PADDING:
338 return( PSA_ERROR_INVALID_PADDING );
339 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
340 return( PSA_ERROR_HARDWARE_FAILURE );
341 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
342 return( PSA_ERROR_INVALID_ARGUMENT );
343 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
344 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200345 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100346 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
347 return( PSA_ERROR_INVALID_SIGNATURE );
348 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
349 return( PSA_ERROR_BUFFER_TOO_SMALL );
350 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100351 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100352 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
353 return( PSA_ERROR_NOT_SUPPORTED );
354 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
355 return( PSA_ERROR_HARDWARE_FAILURE );
356
357 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
358 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
359 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
360 return( PSA_ERROR_HARDWARE_FAILURE );
361
362 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
363 return( PSA_ERROR_INVALID_ARGUMENT );
364 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
365 return( PSA_ERROR_HARDWARE_FAILURE );
366
itayzafrir5c753392018-05-08 11:18:38 +0300367 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300368 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300369 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300370 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
371 return( PSA_ERROR_BUFFER_TOO_SMALL );
372 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
373 return( PSA_ERROR_NOT_SUPPORTED );
374 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
375 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
376 return( PSA_ERROR_INVALID_SIGNATURE );
377 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
378 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100379 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
380 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300381 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
382 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100383
Janos Follatha13b9052019-11-22 12:48:59 +0000384 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
385 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300386
Gilles Peskinee59236f2018-01-27 23:32:46 +0100387 default:
David Saadab4ecc272019-02-14 13:48:10 +0200388 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100389 }
390}
391
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200392
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200393
394
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100395/****************************************************************/
396/* Key management */
397/****************************************************************/
398
Gilles Peskine73167e12019-07-12 23:44:37 +0200399#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
400static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
401{
Gilles Peskine8e338702019-07-30 20:06:31 +0200402 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200403}
404#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
405
John Durkop07cc04a2020-11-16 22:08:34 -0800406/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
407 * current test driver in key_management.c is using this function
408 * when accelerators are used for ECC key pair and public key.
409 * Once that dependency is resolved these guards can be removed.
410 */
John Durkop9814fa22020-11-04 12:28:15 -0800411#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800412 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
413 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
414 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100415mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100416 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200417{
418 switch( curve )
419 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100420 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100421 switch( byte_length )
422 {
423 case PSA_BITS_TO_BYTES( 192 ):
424 return( MBEDTLS_ECP_DP_SECP192R1 );
425 case PSA_BITS_TO_BYTES( 224 ):
426 return( MBEDTLS_ECP_DP_SECP224R1 );
427 case PSA_BITS_TO_BYTES( 256 ):
428 return( MBEDTLS_ECP_DP_SECP256R1 );
429 case PSA_BITS_TO_BYTES( 384 ):
430 return( MBEDTLS_ECP_DP_SECP384R1 );
431 case PSA_BITS_TO_BYTES( 521 ):
432 return( MBEDTLS_ECP_DP_SECP521R1 );
433 default:
434 return( MBEDTLS_ECP_DP_NONE );
435 }
436 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100437
Paul Elliott8ff510a2020-06-02 17:19:28 +0100438 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100439 switch( byte_length )
440 {
441 case PSA_BITS_TO_BYTES( 256 ):
442 return( MBEDTLS_ECP_DP_BP256R1 );
443 case PSA_BITS_TO_BYTES( 384 ):
444 return( MBEDTLS_ECP_DP_BP384R1 );
445 case PSA_BITS_TO_BYTES( 512 ):
446 return( MBEDTLS_ECP_DP_BP512R1 );
447 default:
448 return( MBEDTLS_ECP_DP_NONE );
449 }
450 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100451
Paul Elliott8ff510a2020-06-02 17:19:28 +0100452 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100453 switch( byte_length )
454 {
455 case PSA_BITS_TO_BYTES( 255 ):
456 return( MBEDTLS_ECP_DP_CURVE25519 );
457 case PSA_BITS_TO_BYTES( 448 ):
458 return( MBEDTLS_ECP_DP_CURVE448 );
459 default:
460 return( MBEDTLS_ECP_DP_NONE );
461 }
462 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100463
Paul Elliott8ff510a2020-06-02 17:19:28 +0100464 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100465 switch( byte_length )
466 {
467 case PSA_BITS_TO_BYTES( 192 ):
468 return( MBEDTLS_ECP_DP_SECP192K1 );
469 case PSA_BITS_TO_BYTES( 224 ):
470 return( MBEDTLS_ECP_DP_SECP224K1 );
471 case PSA_BITS_TO_BYTES( 256 ):
472 return( MBEDTLS_ECP_DP_SECP256K1 );
473 default:
474 return( MBEDTLS_ECP_DP_NONE );
475 }
476 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100477
Gilles Peskine12313cd2018-06-20 00:20:32 +0200478 default:
479 return( MBEDTLS_ECP_DP_NONE );
480 }
481}
John Durkop9814fa22020-11-04 12:28:15 -0800482#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800483 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
484 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
485 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200486
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200487static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
488 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489{
490 /* Check that the bit size is acceptable for the key type */
491 switch( type )
492 {
493 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200494 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200495 case PSA_KEY_TYPE_DERIVE:
496 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497#if defined(MBEDTLS_AES_C)
498 case PSA_KEY_TYPE_AES:
499 if( bits != 128 && bits != 192 && bits != 256 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
501 break;
502#endif
503#if defined(MBEDTLS_CAMELLIA_C)
504 case PSA_KEY_TYPE_CAMELLIA:
505 if( bits != 128 && bits != 192 && bits != 256 )
506 return( PSA_ERROR_INVALID_ARGUMENT );
507 break;
508#endif
509#if defined(MBEDTLS_DES_C)
510 case PSA_KEY_TYPE_DES:
511 if( bits != 64 && bits != 128 && bits != 192 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
515#if defined(MBEDTLS_ARC4_C)
516 case PSA_KEY_TYPE_ARC4:
517 if( bits < 8 || bits > 2048 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200521#if defined(MBEDTLS_CHACHA20_C)
522 case PSA_KEY_TYPE_CHACHA20:
523 if( bits != 256 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200527 default:
528 return( PSA_ERROR_NOT_SUPPORTED );
529 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200530 if( bits % 8 != 0 )
531 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200532
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533 return( PSA_SUCCESS );
534}
535
John Durkop6ba40d12020-11-10 08:50:04 -0800536#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
537 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
538
Steven Cooremana01795d2020-07-24 22:48:15 +0200539/** Export an RSA key to export representation
540 *
541 * \param[in] type The type of key (public/private) to export
542 * \param[in] rsa The internal RSA representation from which to export
543 * \param[out] data The buffer to export to
544 * \param[in] data_size The length of the buffer to export to
545 * \param[out] data_length The amount of bytes written to \p data
546 */
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100547static psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
548 mbedtls_rsa_context *rsa,
549 uint8_t *data,
550 size_t data_size,
551 size_t *data_length )
Steven Cooremana01795d2020-07-24 22:48:15 +0200552{
553#if defined(MBEDTLS_PK_WRITE_C)
554 int ret;
555 mbedtls_pk_context pk;
556 uint8_t *pos = data + data_size;
557
558 mbedtls_pk_init( &pk );
559 pk.pk_info = &mbedtls_rsa_info;
560 pk.pk_ctx = rsa;
561
562 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200563 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
564 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200565 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
566 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
567 else
568 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
569
570 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200571 {
572 /* Clean up in case pk_write failed halfway through. */
573 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200574 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200575 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200576
577 /* The mbedtls_pk_xxx functions write to the end of the buffer.
578 * Move the data to the beginning and erase remaining data
579 * at the original location. */
580 if( 2 * (size_t) ret <= data_size )
581 {
582 memcpy( data, data + data_size - ret, ret );
583 memset( data + data_size - ret, 0, ret );
584 }
585 else if( (size_t) ret < data_size )
586 {
587 memmove( data, data + data_size - ret, ret );
588 memset( data + ret, 0, data_size - ret );
589 }
590
591 *data_length = ret;
592 return( PSA_SUCCESS );
593#else
594 (void) type;
595 (void) rsa;
596 (void) data;
597 (void) data_size;
598 (void) data_length;
599 return( PSA_ERROR_NOT_SUPPORTED );
600#endif /* MBEDTLS_PK_WRITE_C */
601}
602
603/** Import an RSA key from import representation to a slot
604 *
605 * \param[in,out] slot The slot where to store the export representation to
606 * \param[in] data The buffer containing the import representation
607 * \param[in] data_length The amount of bytes in \p data
608 */
609static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
610 const uint8_t *data,
611 size_t data_length )
612{
613 psa_status_t status;
614 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200615 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200616
Steven Cooremana01795d2020-07-24 22:48:15 +0200617 /* Parse input */
Ronald Cron90857082020-11-25 14:58:33 +0100618 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
619 data,
620 data_length,
621 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200622 if( status != PSA_SUCCESS )
623 goto exit;
624
625 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200626 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200627
Steven Cooreman75b74362020-07-28 14:30:13 +0200628 /* Re-export the data to PSA export format, such that we can store export
629 * representation in the key slot. Export representation in case of RSA is
630 * the smallest representation that's allowed as input, so a straight-up
631 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200632 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200633 if( output == NULL )
634 {
635 status = PSA_ERROR_INSUFFICIENT_MEMORY;
636 goto exit;
637 }
638
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100639 status = mbedtls_psa_rsa_export_key( slot->attr.type,
640 rsa,
641 output,
642 data_length,
643 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200644exit:
645 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200646 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200647 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200648
649 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000650 if( status != PSA_SUCCESS )
651 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200652 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000653 return( status );
654 }
655
Steven Cooremana01795d2020-07-24 22:48:15 +0200656 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100657 slot->key.data = output;
658 slot->key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000659
660 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200661}
John Durkop6ba40d12020-11-10 08:50:04 -0800662
663#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800664 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200665
John Durkop9814fa22020-11-04 12:28:15 -0800666#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800667 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200668/** Export an ECP key to export representation
669 *
670 * \param[in] type The type of key (public/private) to export
671 * \param[in] ecp The internal ECP representation from which to export
672 * \param[out] data The buffer to export to
673 * \param[in] data_size The length of the buffer to export to
674 * \param[out] data_length The amount of bytes written to \p data
675 */
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100676static psa_status_t mbedtls_psa_ecp_export_key( psa_key_type_t type,
677 mbedtls_ecp_keypair *ecp,
678 uint8_t *data,
679 size_t data_size,
680 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200681{
Steven Cooremanacda8342020-07-24 23:09:52 +0200682 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000683
Steven Cooremanacda8342020-07-24 23:09:52 +0200684 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200685 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200686 /* Check whether the public part is loaded */
687 if( mbedtls_ecp_is_zero( &ecp->Q ) )
688 {
689 /* Calculate the public key */
690 status = mbedtls_to_psa_error(
691 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100692 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200693 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200694 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200695 }
696
Steven Cooreman4fed4552020-08-03 14:46:03 +0200697 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200698 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
699 MBEDTLS_ECP_PF_UNCOMPRESSED,
700 data_length,
701 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200702 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200703 if( status != PSA_SUCCESS )
704 memset( data, 0, data_size );
705
Steven Cooreman29149862020-08-05 15:43:42 +0200706 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200707 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200708 else
709 {
Steven Cooreman29149862020-08-05 15:43:42 +0200710 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200711 return( PSA_ERROR_BUFFER_TOO_SMALL );
712
713 status = mbedtls_to_psa_error(
714 mbedtls_ecp_write_key( ecp,
715 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200716 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200717 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200718 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200719 else
720 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200721
722 return( status );
723 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200724}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200725
Steven Cooreman4fed4552020-08-03 14:46:03 +0200726/** Import an ECP key from import representation to a slot
727 *
728 * \param[in,out] slot The slot where to store the export representation to
729 * \param[in] data The buffer containing the import representation
730 * \param[in] data_length The amount of bytes in \p data
731 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200732static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
733 const uint8_t *data,
734 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100735{
Steven Cooremanacda8342020-07-24 23:09:52 +0200736 psa_status_t status;
737 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200738 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100739
Steven Cooremanacda8342020-07-24 23:09:52 +0200740 /* Parse input */
Ronald Cron90857082020-11-25 14:58:33 +0100741 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
742 data,
743 data_length,
744 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100745 if( status != PSA_SUCCESS )
746 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100747
Steven Cooremanacda8342020-07-24 23:09:52 +0200748 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200749 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200750 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200751 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200752
Steven Cooremanacda8342020-07-24 23:09:52 +0200753 /* Re-export the data to PSA export format. There is currently no support
754 * for other input formats then the export format, so this is a 1-1
755 * copy operation. */
756 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200757 if( output == NULL )
758 {
759 status = PSA_ERROR_INSUFFICIENT_MEMORY;
760 goto exit;
761 }
762
Ronald Cron3c8ca3a2020-11-26 16:45:24 +0100763 status = mbedtls_psa_ecp_export_key( slot->attr.type,
764 ecp,
765 output,
766 data_length,
767 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200769 /* Always free the PK object (will also free contained ECP context) */
770 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200771 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200772
773 /* Free the allocated buffer only on error. */
774 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100775 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200776 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100778 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200779
780 /* On success, store the allocated export-formatted key. */
Ronald Cronea0f8a62020-11-25 17:52:23 +0100781 slot->key.data = output;
782 slot->key.bytes = data_length;
Steven Cooremanacda8342020-07-24 23:09:52 +0200783
784 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100785}
John Durkop9814fa22020-11-04 12:28:15 -0800786#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
787 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100788
Gilles Peskineb46bef22019-07-30 21:32:04 +0200789/** Return the size of the key in the given slot, in bits.
790 *
791 * \param[in] slot A key slot.
792 *
793 * \return The key size in bits, read from the metadata in the slot.
794 */
795static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
796{
797 return( slot->attr.bits );
798}
799
Steven Cooreman75b74362020-07-28 14:30:13 +0200800/** Try to allocate a buffer to an empty key slot.
801 *
802 * \param[in,out] slot Key slot to attach buffer to.
803 * \param[in] buffer_length Requested size of the buffer.
804 *
805 * \retval #PSA_SUCCESS
806 * The buffer has been successfully allocated.
807 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
808 * Not enough memory was available for allocation.
809 * \retval #PSA_ERROR_ALREADY_EXISTS
810 * Trying to allocate a buffer to a non-empty key slot.
811 */
812static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
813 size_t buffer_length )
814{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100815 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200816 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200817
Ronald Cronea0f8a62020-11-25 17:52:23 +0100818 slot->key.data = mbedtls_calloc( 1, buffer_length );
819 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200820 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200821
Ronald Cronea0f8a62020-11-25 17:52:23 +0100822 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200823 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200824}
825
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200826psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
827 const uint8_t* data,
828 size_t data_length )
829{
830 psa_status_t status = psa_allocate_buffer_to_slot( slot,
831 data_length );
832 if( status != PSA_SUCCESS )
833 return( status );
834
Ronald Cronea0f8a62020-11-25 17:52:23 +0100835 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200836 return( PSA_SUCCESS );
837}
838
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200839/** Import key data into a slot.
840 *
841 * `slot->type` must have been set previously.
842 * This function assumes that the slot does not contain any key material yet.
843 * On failure, the slot content is unchanged.
844 *
845 * Persistent storage is not affected.
846 *
847 * \param[in,out] slot The key slot to import data into.
848 * Its `type` field must have previously been set to
849 * the desired key type.
850 * It must not contain any key material yet.
851 * \param[in] data Buffer containing the key material to parse and import.
852 * \param data_length Size of \p data in bytes.
853 *
854 * \retval #PSA_SUCCESS
855 * \retval #PSA_ERROR_INVALID_ARGUMENT
856 * \retval #PSA_ERROR_NOT_SUPPORTED
857 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
858 */
859static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
860 const uint8_t *data,
861 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100862{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200863 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +0200864 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200866 /* zero-length keys are never supported. */
867 if( data_length == 0 )
868 return( PSA_ERROR_NOT_SUPPORTED );
869
Gilles Peskine8e338702019-07-30 20:06:31 +0200870 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871 {
Steven Cooreman04524762020-10-13 17:43:44 +0200872 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200873
Steven Cooreman75b74362020-07-28 14:30:13 +0200874 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
875 if( data_length > SIZE_MAX / 8 )
876 return( PSA_ERROR_NOT_SUPPORTED );
877
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200878 /* Enforce a size limit, and in particular ensure that the bit
879 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200880 if( bit_size > PSA_MAX_KEY_BITS )
881 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200882
883 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200884 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200885 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200886
887 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200888 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +0200889 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200890 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200891
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200892 /* Write the actual key size to the slot.
893 * psa_start_key_creation() wrote the size declared by the
894 * caller, which may be 0 (meaning unspecified) or wrong. */
895 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +0100896
897 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100898 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200899 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200900 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200901 /* Try validation through accelerators first. */
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200902 psa_key_attributes_t attributes = {
903 .core = slot->attr
904 };
Ronald Cron83282872020-11-22 14:02:39 +0100905
906 status = psa_allocate_buffer_to_slot( slot, data_length );
907 if( status != PSA_SUCCESS )
908 return( status );
909
910 bit_size = slot->attr.bits;
911 status = psa_driver_wrapper_import_key( &attributes,
912 data, data_length,
913 slot->key.data,
914 slot->key.bytes,
915 &slot->key.bytes,
916 &bit_size );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200917 if( status == PSA_SUCCESS )
918 {
Ronald Cron83282872020-11-22 14:02:39 +0100919 if( slot->attr.bits == 0 )
920 slot->attr.bits = (psa_key_bits_t) bit_size;
921 else if( bit_size != slot->attr.bits )
922 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200923
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200924 return( PSA_SUCCESS );
925 }
Ronald Cron83282872020-11-22 14:02:39 +0100926 else
927 {
928 if( status != PSA_ERROR_NOT_SUPPORTED )
929 return( status );
930 }
931
932 mbedtls_platform_zeroize( slot->key.data, data_length );
933 mbedtls_free( slot->key.data );
934 slot->key.data = NULL;
935 slot->key.bytes = 0;
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200936
937 /* Key format is not supported by any accelerator, try software fallback
938 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -0800939#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
940 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200941 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
942 {
Steven Cooreman40120f62020-10-29 11:42:22 +0100943 return( psa_import_ecp_key( slot, data, data_length ) );
944 }
John Durkop9814fa22020-11-04 12:28:15 -0800945#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
946 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700947#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
948 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +0100949 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200950 {
Steven Cooreman40120f62020-10-29 11:42:22 +0100951 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200952 }
John Durkop9814fa22020-11-04 12:28:15 -0800953#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
954 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +0100955
956 /* Fell through the fallback as well, so have nothing else to try. */
957 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958 }
959 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100960 {
Steven Cooreman19fd5742020-07-24 23:31:01 +0200961 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +0100963 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100964}
965
Gilles Peskinef603c712019-01-19 13:40:11 +0100966/** Calculate the intersection of two algorithm usage policies.
967 *
968 * Return 0 (which allows no operation) on incompatibility.
969 */
970static psa_algorithm_t psa_key_policy_algorithm_intersection(
971 psa_algorithm_t alg1,
972 psa_algorithm_t alg2 )
973{
Gilles Peskine549ea862019-05-22 11:45:59 +0200974 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100975 if( alg1 == alg2 )
976 return( alg1 );
977 /* If the policies are from the same hash-and-sign family, check
978 * if one is a wildcard. If so the other has the specific algorithm. */
979 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
980 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
981 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
982 {
983 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
984 return( alg2 );
985 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
986 return( alg1 );
987 }
988 /* If the policies are incompatible, allow nothing. */
989 return( 0 );
990}
991
Gilles Peskined6f371b2019-05-10 19:33:38 +0200992static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
993 psa_algorithm_t requested_alg )
994{
Gilles Peskine549ea862019-05-22 11:45:59 +0200995 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200996 if( requested_alg == policy_alg )
997 return( 1 );
998 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200999 * and requested_alg is the same hash-and-sign family with any hash,
1000 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001001 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1002 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1003 {
1004 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1005 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1006 }
Steven Cooremance48e852020-10-05 16:02:45 +02001007 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001008 * a key derivation with that key agreement should also be allowed. This
1009 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001010 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1011 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1012 {
1013 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1014 policy_alg );
1015 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001016 /* If it isn't permitted, it's forbidden. */
1017 return( 0 );
1018}
1019
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001020/** Test whether a policy permits an algorithm.
1021 *
1022 * The caller must test usage flags separately.
1023 */
1024static int psa_key_policy_permits( const psa_key_policy_t *policy,
1025 psa_algorithm_t alg )
1026{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001027 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1028 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001029}
1030
Gilles Peskinef603c712019-01-19 13:40:11 +01001031/** Restrict a key policy based on a constraint.
1032 *
1033 * \param[in,out] policy The policy to restrict.
1034 * \param[in] constraint The policy constraint to apply.
1035 *
1036 * \retval #PSA_SUCCESS
1037 * \c *policy contains the intersection of the original value of
1038 * \c *policy and \c *constraint.
1039 * \retval #PSA_ERROR_INVALID_ARGUMENT
1040 * \c *policy and \c *constraint are incompatible.
1041 * \c *policy is unchanged.
1042 */
1043static psa_status_t psa_restrict_key_policy(
1044 psa_key_policy_t *policy,
1045 const psa_key_policy_t *constraint )
1046{
1047 psa_algorithm_t intersection_alg =
1048 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001049 psa_algorithm_t intersection_alg2 =
1050 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001051 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1052 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001053 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1054 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001055 policy->usage &= constraint->usage;
1056 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001057 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001058 return( PSA_SUCCESS );
1059}
1060
Ronald Cron5c522922020-11-14 16:35:34 +01001061/** Get the description of a key given its identifier and policy constraints
1062 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001063 *
Ronald Cron5c522922020-11-14 16:35:34 +01001064 * The key must have allow all the usage flags set in \p usage. If \p alg is
1065 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001066 *
Ronald Cron5c522922020-11-14 16:35:34 +01001067 * In case of a persistent key, the function loads the description of the key
1068 * into a key slot if not already done.
1069 *
1070 * On success, the returned key slot is locked. It is the responsibility of
1071 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001072 */
Ronald Cron5c522922020-11-14 16:35:34 +01001073static psa_status_t psa_get_and_lock_key_slot_with_policy(
1074 mbedtls_svc_key_id_t key,
1075 psa_key_slot_t **p_slot,
1076 psa_key_usage_t usage,
1077 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001078{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001079 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1080 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001081
Ronald Cron5c522922020-11-14 16:35:34 +01001082 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001083 if( status != PSA_SUCCESS )
1084 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001085 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001086
1087 /* Enforce that usage policy for the key slot contains all the flags
1088 * required by the usage parameter. There is one exception: public
1089 * keys can always be exported, so we treat public key objects as
1090 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001091 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001092 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001093
1094 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001095 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001096 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001097
1098 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001099 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001100 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001101
Darryl Green06fd18d2018-07-16 11:21:11 +01001102 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001103
1104error:
1105 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001106 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001107
1108 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001109}
Darryl Green940d72c2018-07-13 13:18:51 +01001110
Ronald Cron5c522922020-11-14 16:35:34 +01001111/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001112 *
1113 * A transparent key is a key for which the key material is directly
1114 * available, as opposed to a key in a secure element.
1115 *
Ronald Cron5c522922020-11-14 16:35:34 +01001116 * This is a temporary function to use instead of
1117 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1118 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001119 *
Ronald Cron5c522922020-11-14 16:35:34 +01001120 * On success, the returned key slot is locked. It is the responsibility of the
1121 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001122 */
1123#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001124static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1125 mbedtls_svc_key_id_t key,
1126 psa_key_slot_t **p_slot,
1127 psa_key_usage_t usage,
1128 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001129{
Ronald Cron5c522922020-11-14 16:35:34 +01001130 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1131 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001132 if( status != PSA_SUCCESS )
1133 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001134
Gilles Peskineadad8132019-07-25 11:31:23 +02001135 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001136 {
Ronald Cron5c522922020-11-14 16:35:34 +01001137 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001138 *p_slot = NULL;
1139 return( PSA_ERROR_NOT_SUPPORTED );
1140 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001141
Gilles Peskine28f8f302019-07-24 13:30:31 +02001142 return( PSA_SUCCESS );
1143}
1144#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1145/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001146#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1147 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001148#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1149
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001150/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001151static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001152{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001153 /* Data pointer will always be either a valid pointer or NULL in an
1154 * initialized slot, so we can just free it. */
1155 if( slot->key.data != NULL )
1156 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1157
1158 mbedtls_free( slot->key.data );
1159 slot->key.data = NULL;
1160 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001161
1162 return( PSA_SUCCESS );
1163}
1164
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001165/** Completely wipe a slot in memory, including its policy.
1166 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001167psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001168{
1169 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001170
1171 /*
1172 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001173 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001174 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1175 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001176 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001177 */
Ronald Cron5c522922020-11-14 16:35:34 +01001178 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001179 {
1180#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001181 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001182#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001183 status = PSA_ERROR_CORRUPTION_DETECTED;
1184 }
1185
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001186 /* Multipart operations may still be using the key. This is safe
1187 * because all multipart operation objects are independent from
1188 * the key slot: if they need to access the key after the setup
1189 * phase, they have a copy of the key. Note that this means that
1190 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001191 /* At this point, key material and other type-specific content has
1192 * been wiped. Clear remaining metadata. We can call memset and not
1193 * zeroize because the metadata is not particularly sensitive. */
1194 memset( slot, 0, sizeof( *slot ) );
1195 return( status );
1196}
1197
Ronald Croncf56a0a2020-08-04 09:51:30 +02001198psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001200 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001201 psa_status_t status; /* status of the last operation */
1202 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001203#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1204 psa_se_drv_table_entry_t *driver;
1205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001206
Ronald Croncf56a0a2020-08-04 09:51:30 +02001207 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001208 return( PSA_SUCCESS );
1209
Ronald Cronf2911112020-10-29 17:51:10 +01001210 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001211 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001212 * key, this will load the key description from persistent memory if not
1213 * done yet. We cannot avoid this loading as without it we don't know if
1214 * the key is operated by an SE or not and this information is needed by
1215 * the current implementation.
1216 */
Ronald Cron5c522922020-11-14 16:35:34 +01001217 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001218 if( status != PSA_SUCCESS )
1219 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001220
Ronald Cronf2911112020-10-29 17:51:10 +01001221 /*
1222 * If the key slot containing the key description is under access by the
1223 * library (apart from the present access), the key cannot be destroyed
1224 * yet. For the time being, just return in error. Eventually (to be
1225 * implemented), the key should be destroyed when all accesses have
1226 * stopped.
1227 */
Ronald Cron5c522922020-11-14 16:35:34 +01001228 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001229 {
Ronald Cron5c522922020-11-14 16:35:34 +01001230 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001231 return( PSA_ERROR_GENERIC_ERROR );
1232 }
1233
Gilles Peskine354f7672019-07-12 23:46:38 +02001234#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001235 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001236 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001237 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001238 /* For a key in a secure element, we need to do three things:
1239 * remove the key file in internal storage, destroy the
1240 * key inside the secure element, and update the driver's
1241 * persistent data. Start a transaction that will encompass these
1242 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001243 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001244 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001245 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001246 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001247 status = psa_crypto_save_transaction( );
1248 if( status != PSA_SUCCESS )
1249 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001250 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001251 /* We should still try to destroy the key in the secure
1252 * element and the key metadata in storage. This is especially
1253 * important if the error is that the storage is full.
1254 * But how to do it exactly without risking an inconsistent
1255 * state after a reset?
1256 * https://github.com/ARMmbed/mbed-crypto/issues/215
1257 */
1258 overall_status = status;
1259 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001260 }
1261
Ronald Cronea0f8a62020-11-25 17:52:23 +01001262 status = psa_destroy_se_key( driver,
1263 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001264 if( overall_status == PSA_SUCCESS )
1265 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001266 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1268
Darryl Greend49a4992018-06-18 17:27:26 +01001269#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001270 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001271 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001272 status = psa_destroy_persistent_key( slot->attr.id );
1273 if( overall_status == PSA_SUCCESS )
1274 overall_status = status;
1275
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001276 /* TODO: other slots may have a copy of the same key. We should
1277 * invalidate them.
1278 * https://github.com/ARMmbed/mbed-crypto/issues/214
1279 */
Darryl Greend49a4992018-06-18 17:27:26 +01001280 }
1281#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001282
Gilles Peskinefc762652019-07-22 19:30:34 +02001283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1284 if( driver != NULL )
1285 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001286 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001287 if( overall_status == PSA_SUCCESS )
1288 overall_status = status;
1289 status = psa_crypto_stop_transaction( );
1290 if( overall_status == PSA_SUCCESS )
1291 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001292 }
1293#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1294
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001295#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1296exit:
1297#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001298 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001299 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1300 if( overall_status == PSA_SUCCESS )
1301 overall_status = status;
1302 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001303}
1304
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001305void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001306{
Gilles Peskineb699f072019-04-26 16:06:02 +02001307 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001308 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001309}
1310
Gilles Peskineb699f072019-04-26 16:06:02 +02001311psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1312 psa_key_type_t type,
1313 const uint8_t *data,
1314 size_t data_length )
1315{
1316 uint8_t *copy = NULL;
1317
1318 if( data_length != 0 )
1319 {
1320 copy = mbedtls_calloc( 1, data_length );
1321 if( copy == NULL )
1322 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1323 memcpy( copy, data, data_length );
1324 }
1325 /* After this point, this function is guaranteed to succeed, so it
1326 * can start modifying `*attributes`. */
1327
1328 if( attributes->domain_parameters != NULL )
1329 {
1330 mbedtls_free( attributes->domain_parameters );
1331 attributes->domain_parameters = NULL;
1332 attributes->domain_parameters_size = 0;
1333 }
1334
1335 attributes->domain_parameters = copy;
1336 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001337 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001338 return( PSA_SUCCESS );
1339}
1340
1341psa_status_t psa_get_key_domain_parameters(
1342 const psa_key_attributes_t *attributes,
1343 uint8_t *data, size_t data_size, size_t *data_length )
1344{
1345 if( attributes->domain_parameters_size > data_size )
1346 return( PSA_ERROR_BUFFER_TOO_SMALL );
1347 *data_length = attributes->domain_parameters_size;
1348 if( attributes->domain_parameters_size != 0 )
1349 memcpy( data, attributes->domain_parameters,
1350 attributes->domain_parameters_size );
1351 return( PSA_SUCCESS );
1352}
1353
John Durkop07cc04a2020-11-16 22:08:34 -08001354#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001355 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001356static psa_status_t psa_get_rsa_public_exponent(
1357 const mbedtls_rsa_context *rsa,
1358 psa_key_attributes_t *attributes )
1359{
1360 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001362 uint8_t *buffer = NULL;
1363 size_t buflen;
1364 mbedtls_mpi_init( &mpi );
1365
1366 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1367 if( ret != 0 )
1368 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001369 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1370 {
1371 /* It's the default value, which is reported as an empty string,
1372 * so there's nothing to do. */
1373 goto exit;
1374 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001375
1376 buflen = mbedtls_mpi_size( &mpi );
1377 buffer = mbedtls_calloc( 1, buflen );
1378 if( buffer == NULL )
1379 {
1380 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1381 goto exit;
1382 }
1383 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1384 if( ret != 0 )
1385 goto exit;
1386 attributes->domain_parameters = buffer;
1387 attributes->domain_parameters_size = buflen;
1388
1389exit:
1390 mbedtls_mpi_free( &mpi );
1391 if( ret != 0 )
1392 mbedtls_free( buffer );
1393 return( mbedtls_to_psa_error( ret ) );
1394}
John Durkop07cc04a2020-11-16 22:08:34 -08001395#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001396 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001397
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001398/** Retrieve all the publicly-accessible attributes of a key.
1399 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001400psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001401 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001402{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001404 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001405 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001406
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001407 psa_reset_key_attributes( attributes );
1408
Ronald Cron5c522922020-11-14 16:35:34 +01001409 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001410 if( status != PSA_SUCCESS )
1411 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001412
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001413 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001414 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1415 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1416
1417#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1418 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001419 psa_set_key_slot_number( attributes,
1420 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001421#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001422
Gilles Peskine8e338702019-07-30 20:06:31 +02001423 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001424 {
John Durkop07cc04a2020-11-16 22:08:34 -08001425#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001426 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001427 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001428 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001429#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001430 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001431 * is not yet implemented.
1432 * https://github.com/ARMmbed/mbed-crypto/issues/216
1433 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001434 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001435 break;
1436#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001437 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001438 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001439
Ronald Cron90857082020-11-25 14:58:33 +01001440 status = mbedtls_psa_rsa_load_representation(
1441 slot->attr.type,
1442 slot->key.data,
1443 slot->key.bytes,
1444 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001445 if( status != PSA_SUCCESS )
1446 break;
1447
Steven Cooremana2371e52020-07-28 14:30:39 +02001448 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001449 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001450 mbedtls_rsa_free( rsa );
1451 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001452 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001453 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001454#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001455 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001456 default:
1457 /* Nothing else to do. */
1458 break;
1459 }
1460
1461 if( status != PSA_SUCCESS )
1462 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001463
Ronald Cron5c522922020-11-14 16:35:34 +01001464 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001465
Ronald Cron5c522922020-11-14 16:35:34 +01001466 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001467}
1468
Gilles Peskinec8000c02019-08-02 20:15:51 +02001469#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1470psa_status_t psa_get_key_slot_number(
1471 const psa_key_attributes_t *attributes,
1472 psa_key_slot_number_t *slot_number )
1473{
1474 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1475 {
1476 *slot_number = attributes->slot_number;
1477 return( PSA_SUCCESS );
1478 }
1479 else
1480 return( PSA_ERROR_INVALID_ARGUMENT );
1481}
1482#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1483
Ronald Cron200a52c2020-11-25 15:55:46 +01001484static psa_status_t psa_export_key_buffer_internal( const psa_key_slot_t *slot,
Steven Cooremana01795d2020-07-24 22:48:15 +02001485 uint8_t *data,
1486 size_t data_size,
1487 size_t *data_length )
1488{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001489 if( slot->key.bytes > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001490 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Cronea0f8a62020-11-25 17:52:23 +01001491 memcpy( data, slot->key.data, slot->key.bytes );
1492 memset( data + slot->key.bytes, 0,
1493 data_size - slot->key.bytes );
1494 *data_length = slot->key.bytes;
Steven Cooremana01795d2020-07-24 22:48:15 +02001495 return( PSA_SUCCESS );
1496}
1497
Ronald Cron200a52c2020-11-25 15:55:46 +01001498static psa_status_t psa_export_key_internal( const psa_key_slot_t *slot,
Gilles Peskinef603c712019-01-19 13:40:11 +01001499 uint8_t *data,
1500 size_t data_size,
Ronald Cron9486f9d2020-11-26 13:36:23 +01001501 size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502{
Gilles Peskine5d309672019-07-12 23:47:28 +02001503#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1504 const psa_drv_se_t *drv;
1505 psa_drv_se_context_t *drv_context;
1506#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1507
Ronald Cron9486f9d2020-11-26 13:36:23 +01001508#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1509 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
1510 {
1511 if( ( drv->key_management == NULL ) ||
1512 ( drv->key_management->p_export == NULL ) )
1513 return( PSA_ERROR_NOT_SUPPORTED );
1514
1515 return( drv->key_management->p_export(
1516 drv_context, psa_key_slot_get_slot_number( slot ),
1517 data, data_size, data_length ) );
1518 }
1519#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1520
1521 if( key_type_is_raw_bytes( slot->attr.type ) ||
1522 PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1523 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1524 {
1525 return( psa_export_key_buffer_internal(
1526 slot, data, data_size, data_length ) );
1527 }
1528 else
1529 {
1530 /* This shouldn't happen in the reference implementation, but
1531 it is valid for a special-purpose implementation to omit
1532 support for exporting certain key types. */
1533 return( PSA_ERROR_NOT_SUPPORTED );
1534 }
1535}
1536
1537psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1538 uint8_t *data,
1539 size_t data_size,
1540 size_t *data_length )
1541{
1542 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1543 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1544 psa_key_slot_t *slot;
1545
1546 /* Set the key to empty now, so that even when there are errors, we always
1547 * set data_length to a value between 0 and data_size. On error, setting
1548 * the key to empty is a good choice because an empty key representation is
1549 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001550 *data_length = 0;
1551
Ronald Cron9486f9d2020-11-26 13:36:23 +01001552 /* Export requires the EXPORT flag. There is an exception for public keys,
1553 * which don't require any flag, but
1554 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1555 */
1556 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1557 PSA_KEY_USAGE_EXPORT, 0 );
1558 if( status != PSA_SUCCESS )
1559 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001560
Gilles Peskinef9168942019-09-12 19:20:29 +02001561 /* Reject a zero-length output buffer now, since this can never be a
1562 * valid key representation. This way we know that data must be a valid
1563 * pointer and we can do things like memset(data, ..., data_size). */
1564 if( data_size == 0 )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001565 {
1566 status = PSA_ERROR_BUFFER_TOO_SMALL;
1567 goto exit;
1568 }
1569
1570 status = psa_export_key_internal( slot, data, data_size, data_length );
1571
1572exit:
1573 unlock_status = psa_unlock_key_slot( slot );
1574
1575 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1576}
1577
1578static psa_status_t psa_export_public_key_internal( const psa_key_slot_t *slot,
1579 uint8_t *data,
1580 size_t data_size,
1581 size_t *data_length )
1582{
1583#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1584 const psa_drv_se_t *drv;
1585 psa_drv_se_context_t *drv_context;
1586#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef9168942019-09-12 19:20:29 +02001587
Gilles Peskine5d309672019-07-12 23:47:28 +02001588#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001589 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001590 {
Ronald Cron9486f9d2020-11-26 13:36:23 +01001591 if( ( drv->key_management == NULL ) ||
1592 ( drv->key_management->p_export_public == NULL ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001593 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5d309672019-07-12 23:47:28 +02001594
Ronald Cron9486f9d2020-11-26 13:36:23 +01001595 return( drv->key_management->p_export_public(
1596 drv_context, psa_key_slot_get_slot_number( slot ),
1597 data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001598 }
Ronald Cron9486f9d2020-11-26 13:36:23 +01001599 else
1600#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1601 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
Steven Cooreman560c28a2020-07-24 23:20:24 +02001602 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001603 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001604 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001605 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001606 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001607 return( psa_export_key_buffer_internal(
1608 slot, data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001609 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001610
Steven Cooreman560c28a2020-07-24 23:20:24 +02001611 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001612 * so conversion is needed. Try the accelerators first. */
Ronald Cron84cc9942020-11-25 14:30:05 +01001613 psa_key_attributes_t attributes = {
1614 .core = slot->attr
1615 };
1616 psa_status_t status = psa_driver_wrapper_export_public_key(
1617 &attributes, slot->key.data, slot->key.bytes,
1618 data, data_size, data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +02001619
1620 if( status != PSA_ERROR_NOT_SUPPORTED ||
1621 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1622 return( status );
1623
Steven Cooreman560c28a2020-07-24 23:20:24 +02001624 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1625 {
John Durkop0e005192020-10-31 22:06:54 -07001626#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1627 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001628 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01001629 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
1630 slot->key.data,
1631 slot->key.bytes,
1632 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001633 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001634 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001635
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01001636 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
1637 rsa,
1638 data,
1639 data_size,
1640 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001641
Steven Cooremana2371e52020-07-28 14:30:39 +02001642 mbedtls_rsa_free( rsa );
1643 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001644
Steven Cooreman560c28a2020-07-24 23:20:24 +02001645 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001646#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001647 /* We don't know how to convert a private RSA key to public. */
1648 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001649#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1650 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001651 }
1652 else
Moran Pekera998bc62018-04-16 18:16:20 +03001653 {
John Durkop6ba40d12020-11-10 08:50:04 -08001654#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1655 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001656 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01001657 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
1658 slot->key.data,
1659 slot->key.bytes,
1660 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001661 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001662 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001663
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01001664 status = mbedtls_psa_ecp_export_key(
1665 PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1666 PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) ),
1667 ecp,
1668 data,
1669 data_size,
1670 data_length );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001671
Steven Cooremana2371e52020-07-28 14:30:39 +02001672 mbedtls_ecp_keypair_free( ecp );
1673 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001674 return( status );
1675#else
1676 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001677 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001678#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1679 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001680 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001681 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001682 else
1683 {
1684 /* This shouldn't happen in the reference implementation, but
1685 it is valid for a special-purpose implementation to omit
1686 support for exporting certain key types. */
1687 return( PSA_ERROR_NOT_SUPPORTED );
1688 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001689}
Ronald Croncf56a0a2020-08-04 09:51:30 +02001690psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001691 uint8_t *data,
1692 size_t data_size,
1693 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001694{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001695 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001696 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001697 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001698
1699 /* Set the key to empty now, so that even when there are errors, we always
1700 * set data_length to a value between 0 and data_size. On error, setting
1701 * the key to empty is a good choice because an empty key representation is
1702 * unlikely to be accepted anywhere. */
1703 *data_length = 0;
1704
1705 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001706 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001707 if( status != PSA_SUCCESS )
1708 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001709
Ronald Cron9486f9d2020-11-26 13:36:23 +01001710 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1711 {
1712 status = PSA_ERROR_INVALID_ARGUMENT;
1713 goto exit;
1714 }
1715
1716 /* Reject a zero-length output buffer now, since this can never be a
1717 * valid key representation. This way we know that data must be a valid
1718 * pointer and we can do things like memset(data, ..., data_size). */
1719 if( data_size == 0 )
1720 {
1721 status = PSA_ERROR_BUFFER_TOO_SMALL;
1722 goto exit;
1723 }
1724
1725 status = psa_export_public_key_internal(
1726 slot, data, data_size, data_length );
1727
1728exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001729 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001730
Ronald Cron5c522922020-11-14 16:35:34 +01001731 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001732}
1733
Gilles Peskine91e8c332019-08-02 19:19:39 +02001734#if defined(static_assert)
1735static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1736 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001737static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001738 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001739static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001740 "One or more key attribute flag is listed as both internal-only and external-only" );
1741#endif
1742
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001743/** Validate that a key policy is internally well-formed.
1744 *
1745 * This function only rejects invalid policies. It does not validate the
1746 * consistency of the policy with respect to other attributes of the key
1747 * such as the key type.
1748 */
1749static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001750{
1751 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001752 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001753 PSA_KEY_USAGE_ENCRYPT |
1754 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001755 PSA_KEY_USAGE_SIGN_HASH |
1756 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001757 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1758 return( PSA_ERROR_INVALID_ARGUMENT );
1759
Gilles Peskine4747d192019-04-17 15:05:45 +02001760 return( PSA_SUCCESS );
1761}
1762
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001763/** Validate the internal consistency of key attributes.
1764 *
1765 * This function only rejects invalid attribute values. If does not
1766 * validate the consistency of the attributes with any key data that may
1767 * be involved in the creation of the key.
1768 *
1769 * Call this function early in the key creation process.
1770 *
1771 * \param[in] attributes Key attributes for the new key.
1772 * \param[out] p_drv On any return, the driver for the key, if any.
1773 * NULL for a transparent key.
1774 *
1775 */
1776static psa_status_t psa_validate_key_attributes(
1777 const psa_key_attributes_t *attributes,
1778 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001779{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001780 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001781 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001782 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001783
Ronald Cron54b90082020-10-29 15:26:43 +01001784 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001785 if( status != PSA_SUCCESS )
1786 return( status );
1787
Ronald Crond2ed4812020-07-17 16:11:30 +02001788 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001789 if( status != PSA_SUCCESS )
1790 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001791
Ronald Cron65f38a32020-10-23 17:11:13 +02001792 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1793 {
1794 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1795 return( PSA_ERROR_INVALID_ARGUMENT );
1796 }
1797 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001798 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001799 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001800 if( status != PSA_SUCCESS )
1801 return( status );
1802 }
1803
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001804 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001805 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001806 return( status );
1807
1808 /* Refuse to create overly large keys.
1809 * Note that this doesn't trigger on import if the attributes don't
1810 * explicitly specify a size (so psa_get_key_bits returns 0), so
1811 * psa_import_key() needs its own checks. */
1812 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1813 return( PSA_ERROR_NOT_SUPPORTED );
1814
Gilles Peskine91e8c332019-08-02 19:19:39 +02001815 /* Reject invalid flags. These should not be reachable through the API. */
1816 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1817 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1818 return( PSA_ERROR_INVALID_ARGUMENT );
1819
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001820 return( PSA_SUCCESS );
1821}
1822
Gilles Peskine4747d192019-04-17 15:05:45 +02001823/** Prepare a key slot to receive key material.
1824 *
1825 * This function allocates a key slot and sets its metadata.
1826 *
1827 * If this function fails, call psa_fail_key_creation().
1828 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001829 * This function is intended to be used as follows:
1830 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001831 * it with the specified attributes, and in case of a volatile key assign it
1832 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001833 * -# Populate the slot with the key material.
1834 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1835 * In case of failure at any step, stop the sequence and call
1836 * psa_fail_key_creation().
1837 *
Ronald Cron5c522922020-11-14 16:35:34 +01001838 * On success, the key slot is locked. It is the responsibility of the caller
1839 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001840 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001841 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001842 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001843 * \param[out] p_slot On success, a pointer to the prepared slot.
1844 * \param[out] p_drv On any return, the driver for the key, if any.
1845 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001846 *
1847 * \retval #PSA_SUCCESS
1848 * The key slot is ready to receive key material.
1849 * \return If this function fails, the key slot is an invalid state.
1850 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001851 */
1852static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001853 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001854 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001855 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001856 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001857{
1858 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001859 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001860 psa_key_slot_t *slot;
1861
Gilles Peskinedf179142019-07-15 22:02:14 +02001862 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001863 *p_drv = NULL;
1864
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001865 status = psa_validate_key_attributes( attributes, p_drv );
1866 if( status != PSA_SUCCESS )
1867 return( status );
1868
Ronald Cronc4d1b512020-07-31 11:26:37 +02001869 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001870 if( status != PSA_SUCCESS )
1871 return( status );
1872 slot = *p_slot;
1873
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001874 /* We're storing the declared bit-size of the key. It's up to each
1875 * creation mechanism to verify that this information is correct.
1876 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001877 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001878 * is optional (import, copy). In case of a volatile key, assign it the
1879 * volatile key identifier associated to the slot returned to contain its
1880 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001881
1882 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001883 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1884 {
1885#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1886 slot->attr.id = volatile_key_id;
1887#else
1888 slot->attr.id.key_id = volatile_key_id;
1889#endif
1890 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001891
Gilles Peskine91e8c332019-08-02 19:19:39 +02001892 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001893 * external-only flags, query `attributes`. Thanks to the check
1894 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001895 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001896 * may have set. */
1897 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001898
Gilles Peskinecbaff462019-07-12 23:46:04 +02001899#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001900 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001901 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001902 * create the key file in internal storage, create the
1903 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001904 * persistent data. This is done by starting a transaction that will
1905 * encompass these three actions.
1906 * For registering a volatile key, we just need to find an appropriate
1907 * slot number inside the SE. Since the key is designated volatile, creating
1908 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001909 /* The first thing to do is to find a slot number for the new key.
1910 * We save the slot number in persistent storage as part of the
1911 * transaction data. It will be needed to recover if the power
1912 * fails during the key creation process, to clean up on the secure
1913 * element side after restarting. Obtaining a slot number from the
1914 * secure element driver updates its persistent state, but we do not yet
1915 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001916 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001917 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001918 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001919 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001920 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001921 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001922 if( status != PSA_SUCCESS )
1923 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001924
1925 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001926 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001927 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1928 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001929 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001930 psa_crypto_transaction.key.id = slot->attr.id;
1931 status = psa_crypto_save_transaction( );
1932 if( status != PSA_SUCCESS )
1933 {
1934 (void) psa_crypto_stop_transaction( );
1935 return( status );
1936 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001937 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001938
1939 status = psa_copy_key_material_into_slot(
1940 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001941 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001942
1943 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1944 {
1945 /* Key registration only makes sense with a secure element. */
1946 return( PSA_ERROR_INVALID_ARGUMENT );
1947 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001948#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1949
Ronald Cronc4d1b512020-07-31 11:26:37 +02001950 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001951}
Gilles Peskine4747d192019-04-17 15:05:45 +02001952
1953/** Finalize the creation of a key once its key material has been set.
1954 *
1955 * This entails writing the key to persistent storage.
1956 *
1957 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001958 * See the documentation of psa_start_key_creation() for the intended use
1959 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001960 *
Ronald Cron5c522922020-11-14 16:35:34 +01001961 * If the finalization succeeds, the function unlocks the key slot (it was
1962 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1963 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001964 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001965 * \param[in,out] slot Pointer to the slot with key material.
1966 * \param[in] driver The secure element driver for the key,
1967 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001968 * \param[out] key On success, identifier of the key. Note that the
1969 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001970 *
1971 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001972 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001973 * \return If this function fails, the key slot is an invalid state.
1974 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001975 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001976static psa_status_t psa_finish_key_creation(
1977 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001978 psa_se_drv_table_entry_t *driver,
1979 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001980{
1981 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001982 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001983 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001984
1985#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001986 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001987 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001988#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1989 if( driver != NULL )
1990 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001991 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001992 psa_key_slot_number_t slot_number =
1993 psa_key_slot_get_slot_number( slot ) ;
1994
Gilles Peskineb46bef22019-07-30 21:32:04 +02001995#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001996 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001997 sizeof( data.slot_number ),
1998 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001999#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01002000 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002001 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002002 (uint8_t*) &data,
2003 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002004 }
2005 else
2006#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2007 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002008 /* Key material is saved in export representation in the slot, so
2009 * just pass the slot buffer for storage. */
2010 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002011 slot->key.data,
2012 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002013 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002014 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002015#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2016
Gilles Peskinecbaff462019-07-12 23:46:04 +02002017#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002018 /* Finish the transaction for a key creation. This does not
2019 * happen when registering an existing key. Detect this case
2020 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002021 * creation of a persistent key in a secure element requires a transaction,
2022 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002023 if( driver != NULL &&
2024 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002025 {
2026 status = psa_save_se_persistent_data( driver );
2027 if( status != PSA_SUCCESS )
2028 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002029 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002030 return( status );
2031 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002032 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002033 }
2034#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2035
Ronald Cron50972942020-11-14 11:28:25 +01002036 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002037 {
2038 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002039 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002040 if( status != PSA_SUCCESS )
2041 *key = MBEDTLS_SVC_KEY_ID_INIT;
2042 }
Ronald Cron50972942020-11-14 11:28:25 +01002043
Gilles Peskine4747d192019-04-17 15:05:45 +02002044 return( status );
2045}
2046
2047/** Abort the creation of a key.
2048 *
2049 * You may call this function after calling psa_start_key_creation(),
2050 * or after psa_finish_key_creation() fails. In other circumstances, this
2051 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002052 * See the documentation of psa_start_key_creation() for the intended use
2053 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002054 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002055 * \param[in,out] slot Pointer to the slot with key material.
2056 * \param[in] driver The secure element driver for the key,
2057 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002058 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002059static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002060 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002061{
Gilles Peskine011e4282019-06-26 18:34:38 +02002062 (void) driver;
2063
Gilles Peskine4747d192019-04-17 15:05:45 +02002064 if( slot == NULL )
2065 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002066
2067#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002068 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002069 * element, and the failure happened later (when saving metadata
2070 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002071 * element.
2072 * https://github.com/ARMmbed/mbed-crypto/issues/217
2073 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002074
Gilles Peskined7729582019-08-05 15:55:54 +02002075 /* Abort the ongoing transaction if any (there may not be one if
2076 * the creation process failed before starting one, or if the
2077 * key creation is a registration of a key in a secure element).
2078 * Earlier functions must already have done what it takes to undo any
2079 * partial creation. All that's left is to update the transaction data
2080 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002081 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002082#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2083
Gilles Peskine4747d192019-04-17 15:05:45 +02002084 psa_wipe_key_slot( slot );
2085}
2086
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002087/** Validate optional attributes during key creation.
2088 *
2089 * Some key attributes are optional during key creation. If they are
2090 * specified in the attributes structure, check that they are consistent
2091 * with the data in the slot.
2092 *
2093 * This function should be called near the end of key creation, after
2094 * the slot in memory is fully populated but before saving persistent data.
2095 */
2096static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002097 const psa_key_slot_t *slot,
2098 const psa_key_attributes_t *attributes )
2099{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002100 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002101 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002102 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002103 return( PSA_ERROR_INVALID_ARGUMENT );
2104 }
2105
2106 if( attributes->domain_parameters_size != 0 )
2107 {
John Durkop0e005192020-10-31 22:06:54 -07002108#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2109 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002110 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002111 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002112 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002113 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002115
Ronald Cron90857082020-11-25 14:58:33 +01002116 psa_status_t status = mbedtls_psa_rsa_load_representation(
2117 slot->attr.type,
2118 slot->key.data,
2119 slot->key.bytes,
2120 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002121 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002122 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002123
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002124 mbedtls_mpi_init( &actual );
2125 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002126 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002127 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002128 mbedtls_rsa_free( rsa );
2129 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002130 if( ret != 0 )
2131 goto rsa_exit;
2132 ret = mbedtls_mpi_read_binary( &required,
2133 attributes->domain_parameters,
2134 attributes->domain_parameters_size );
2135 if( ret != 0 )
2136 goto rsa_exit;
2137 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2138 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2139 rsa_exit:
2140 mbedtls_mpi_free( &actual );
2141 mbedtls_mpi_free( &required );
2142 if( ret != 0)
2143 return( mbedtls_to_psa_error( ret ) );
2144 }
2145 else
John Durkop9814fa22020-11-04 12:28:15 -08002146#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2147 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002148 {
2149 return( PSA_ERROR_INVALID_ARGUMENT );
2150 }
2151 }
2152
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002153 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002154 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002155 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002156 return( PSA_ERROR_INVALID_ARGUMENT );
2157 }
2158
2159 return( PSA_SUCCESS );
2160}
2161
Gilles Peskine4747d192019-04-17 15:05:45 +02002162psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002163 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002164 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002165 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002166{
2167 psa_status_t status;
2168 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002169 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002170
Ronald Cron81709fc2020-11-14 12:10:32 +01002171 *key = MBEDTLS_SVC_KEY_ID_INIT;
2172
Gilles Peskine0f84d622019-09-12 19:03:13 +02002173 /* Reject zero-length symmetric keys (including raw data key objects).
2174 * This also rejects any key which might be encoded as an empty string,
2175 * which is never valid. */
2176 if( data_length == 0 )
2177 return( PSA_ERROR_INVALID_ARGUMENT );
2178
Gilles Peskinedf179142019-07-15 22:02:14 +02002179 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002180 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002181 if( status != PSA_SUCCESS )
2182 goto exit;
2183
Gilles Peskine5d309672019-07-12 23:47:28 +02002184#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2185 if( driver != NULL )
2186 {
2187 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002188 /* The driver should set the number of key bits, however in
2189 * case it doesn't, we initialize bits to an invalid value. */
2190 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002191 if( drv->key_management == NULL ||
2192 drv->key_management->p_import == NULL )
2193 {
2194 status = PSA_ERROR_NOT_SUPPORTED;
2195 goto exit;
2196 }
2197 status = drv->key_management->p_import(
2198 psa_get_se_driver_context( driver ),
Ronald Cronea0f8a62020-11-25 17:52:23 +01002199 psa_key_slot_get_slot_number( slot ),
2200 attributes, data, data_length, &bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002201 if( status != PSA_SUCCESS )
2202 goto exit;
2203 if( bits > PSA_MAX_KEY_BITS )
2204 {
2205 status = PSA_ERROR_NOT_SUPPORTED;
2206 goto exit;
2207 }
2208 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002209 }
2210 else
2211#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremanac3434f2021-01-15 17:36:02 +01002212 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
2213 {
2214 /* Importing a key with external lifetime through the driver wrapper
2215 * interface is not yet supported. Return as if this was an invalid
2216 * lifetime. */
2217 status = PSA_ERROR_INVALID_ARGUMENT;
2218 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002219 }
2220 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002221 {
2222 status = psa_import_key_into_slot( slot, data, data_length );
2223 if( status != PSA_SUCCESS )
2224 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002225 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002226 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002227 if( status != PSA_SUCCESS )
2228 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002229
Ronald Cron81709fc2020-11-14 12:10:32 +01002230 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002231exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002232 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002233 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002234
Gilles Peskine4747d192019-04-17 15:05:45 +02002235 return( status );
2236}
2237
Gilles Peskined7729582019-08-05 15:55:54 +02002238#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2239psa_status_t mbedtls_psa_register_se_key(
2240 const psa_key_attributes_t *attributes )
2241{
2242 psa_status_t status;
2243 psa_key_slot_t *slot = NULL;
2244 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002246
2247 /* Leaving attributes unspecified is not currently supported.
2248 * It could make sense to query the key type and size from the
2249 * secure element, but not all secure elements support this
2250 * and the driver HAL doesn't currently support it. */
2251 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2252 return( PSA_ERROR_NOT_SUPPORTED );
2253 if( psa_get_key_bits( attributes ) == 0 )
2254 return( PSA_ERROR_NOT_SUPPORTED );
2255
2256 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002257 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002258 if( status != PSA_SUCCESS )
2259 goto exit;
2260
Ronald Cron81709fc2020-11-14 12:10:32 +01002261 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002262
2263exit:
2264 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002265 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002266
Gilles Peskined7729582019-08-05 15:55:54 +02002267 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002268 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002269 return( status );
2270}
2271#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2272
Gilles Peskinef603c712019-01-19 13:40:11 +01002273static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002274 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002275{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002276 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002277 source->key.data,
2278 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002279 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002280 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002281
Steven Cooreman398aee52020-10-13 14:35:45 +02002282 target->attr.type = source->attr.type;
2283 target->attr.bits = source->attr.bits;
2284
2285 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002286}
2287
Ronald Croncf56a0a2020-08-04 09:51:30 +02002288psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002289 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002290 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002291{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002293 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002294 psa_key_slot_t *source_slot = NULL;
2295 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002296 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002297 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002298
Ronald Cron81709fc2020-11-14 12:10:32 +01002299 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2300
Ronald Cron5c522922020-11-14 16:35:34 +01002301 status = psa_get_and_lock_transparent_key_slot_with_policy(
2302 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002303 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002304 goto exit;
2305
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002306 status = psa_validate_optional_attributes( source_slot,
2307 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002308 if( status != PSA_SUCCESS )
2309 goto exit;
2310
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002311 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002312 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002313 if( status != PSA_SUCCESS )
2314 goto exit;
2315
Ronald Cron81709fc2020-11-14 12:10:32 +01002316 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2317 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002318 if( status != PSA_SUCCESS )
2319 goto exit;
2320
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002321#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2322 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002323 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002324 /* Copying to a secure element is not implemented yet. */
2325 status = PSA_ERROR_NOT_SUPPORTED;
2326 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002327 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002328#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002329
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002330 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002331 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002332 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002333
Ronald Cron81709fc2020-11-14 12:10:32 +01002334 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002335exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002336 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002337 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002338
Ronald Cron5c522922020-11-14 16:35:34 +01002339 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002340
Ronald Cron5c522922020-11-14 16:35:34 +01002341 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002342}
2343
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002344
2345
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002346/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002347/* Message digests */
2348/****************************************************************/
2349
John Durkop07cc04a2020-11-16 22:08:34 -08002350#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002351 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2352 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002353 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002354static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002355{
2356 switch( alg )
2357 {
John Durkopee4e6602020-11-27 08:48:46 -08002358#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002359 case PSA_ALG_MD2:
2360 return( &mbedtls_md2_info );
2361#endif
John Durkopee4e6602020-11-27 08:48:46 -08002362#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002363 case PSA_ALG_MD4:
2364 return( &mbedtls_md4_info );
2365#endif
John Durkopee4e6602020-11-27 08:48:46 -08002366#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002367 case PSA_ALG_MD5:
2368 return( &mbedtls_md5_info );
2369#endif
John Durkopee4e6602020-11-27 08:48:46 -08002370#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002371 case PSA_ALG_RIPEMD160:
2372 return( &mbedtls_ripemd160_info );
2373#endif
John Durkopee4e6602020-11-27 08:48:46 -08002374#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002375 case PSA_ALG_SHA_1:
2376 return( &mbedtls_sha1_info );
2377#endif
John Durkopee4e6602020-11-27 08:48:46 -08002378#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002379 case PSA_ALG_SHA_224:
2380 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002381#endif
2382#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002383 case PSA_ALG_SHA_256:
2384 return( &mbedtls_sha256_info );
2385#endif
John Durkopee4e6602020-11-27 08:48:46 -08002386#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002387 case PSA_ALG_SHA_384:
2388 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002389#endif
John Durkopee4e6602020-11-27 08:48:46 -08002390#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002391 case PSA_ALG_SHA_512:
2392 return( &mbedtls_sha512_info );
2393#endif
2394 default:
2395 return( NULL );
2396 }
2397}
John Durkop07cc04a2020-11-16 22:08:34 -08002398#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002399 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2400 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2401 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002402
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002403psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2404{
2405 switch( operation->alg )
2406 {
Gilles Peskine81736312018-06-26 15:04:31 +02002407 case 0:
2408 /* The object has (apparently) been initialized but it is not
2409 * in use. It's ok to call abort on such an object, and there's
2410 * nothing to do. */
2411 break;
John Durkopee4e6602020-11-27 08:48:46 -08002412#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002413 case PSA_ALG_MD2:
2414 mbedtls_md2_free( &operation->ctx.md2 );
2415 break;
2416#endif
John Durkopee4e6602020-11-27 08:48:46 -08002417#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002418 case PSA_ALG_MD4:
2419 mbedtls_md4_free( &operation->ctx.md4 );
2420 break;
2421#endif
John Durkopee4e6602020-11-27 08:48:46 -08002422#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002423 case PSA_ALG_MD5:
2424 mbedtls_md5_free( &operation->ctx.md5 );
2425 break;
2426#endif
John Durkopee4e6602020-11-27 08:48:46 -08002427#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002428 case PSA_ALG_RIPEMD160:
2429 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2430 break;
2431#endif
John Durkopee4e6602020-11-27 08:48:46 -08002432#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002433 case PSA_ALG_SHA_1:
2434 mbedtls_sha1_free( &operation->ctx.sha1 );
2435 break;
2436#endif
John Durkop6ca23272020-12-03 06:01:32 -08002437#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002438 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002439 mbedtls_sha256_free( &operation->ctx.sha256 );
2440 break;
2441#endif
2442#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002443 case PSA_ALG_SHA_256:
2444 mbedtls_sha256_free( &operation->ctx.sha256 );
2445 break;
2446#endif
John Durkop6ca23272020-12-03 06:01:32 -08002447#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002448 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002449 mbedtls_sha512_free( &operation->ctx.sha512 );
2450 break;
2451#endif
2452#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002453 case PSA_ALG_SHA_512:
2454 mbedtls_sha512_free( &operation->ctx.sha512 );
2455 break;
2456#endif
2457 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002458 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002459 }
2460 operation->alg = 0;
2461 return( PSA_SUCCESS );
2462}
2463
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002464psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002465 psa_algorithm_t alg )
2466{
Janos Follath24eed8d2019-11-22 13:21:35 +00002467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002468
2469 /* A context must be freshly initialized before it can be set up. */
2470 if( operation->alg != 0 )
2471 {
2472 return( PSA_ERROR_BAD_STATE );
2473 }
2474
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002475 switch( alg )
2476 {
John Durkopee4e6602020-11-27 08:48:46 -08002477#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002478 case PSA_ALG_MD2:
2479 mbedtls_md2_init( &operation->ctx.md2 );
2480 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2481 break;
2482#endif
John Durkopee4e6602020-11-27 08:48:46 -08002483#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002484 case PSA_ALG_MD4:
2485 mbedtls_md4_init( &operation->ctx.md4 );
2486 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2487 break;
2488#endif
John Durkopee4e6602020-11-27 08:48:46 -08002489#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002490 case PSA_ALG_MD5:
2491 mbedtls_md5_init( &operation->ctx.md5 );
2492 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2493 break;
2494#endif
John Durkopee4e6602020-11-27 08:48:46 -08002495#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002496 case PSA_ALG_RIPEMD160:
2497 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2498 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2499 break;
2500#endif
John Durkopee4e6602020-11-27 08:48:46 -08002501#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002502 case PSA_ALG_SHA_1:
2503 mbedtls_sha1_init( &operation->ctx.sha1 );
2504 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2505 break;
2506#endif
John Durkopee4e6602020-11-27 08:48:46 -08002507#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002508 case PSA_ALG_SHA_224:
2509 mbedtls_sha256_init( &operation->ctx.sha256 );
2510 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2511 break;
John Durkopee4e6602020-11-27 08:48:46 -08002512#endif
2513#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002514 case PSA_ALG_SHA_256:
2515 mbedtls_sha256_init( &operation->ctx.sha256 );
2516 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2517 break;
2518#endif
John Durkopee4e6602020-11-27 08:48:46 -08002519#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002520 case PSA_ALG_SHA_384:
2521 mbedtls_sha512_init( &operation->ctx.sha512 );
2522 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2523 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002524#endif
John Durkopee4e6602020-11-27 08:48:46 -08002525#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002526 case PSA_ALG_SHA_512:
2527 mbedtls_sha512_init( &operation->ctx.sha512 );
2528 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2529 break;
2530#endif
2531 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002532 return( PSA_ALG_IS_HASH( alg ) ?
2533 PSA_ERROR_NOT_SUPPORTED :
2534 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002535 }
2536 if( ret == 0 )
2537 operation->alg = alg;
2538 else
2539 psa_hash_abort( operation );
2540 return( mbedtls_to_psa_error( ret ) );
2541}
2542
2543psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2544 const uint8_t *input,
2545 size_t input_length )
2546{
Janos Follath24eed8d2019-11-22 13:21:35 +00002547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002548
2549 /* Don't require hash implementations to behave correctly on a
2550 * zero-length input, which may have an invalid pointer. */
2551 if( input_length == 0 )
2552 return( PSA_SUCCESS );
2553
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002554 switch( operation->alg )
2555 {
John Durkopee4e6602020-11-27 08:48:46 -08002556#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002557 case PSA_ALG_MD2:
2558 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2559 input, input_length );
2560 break;
2561#endif
John Durkopee4e6602020-11-27 08:48:46 -08002562#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002563 case PSA_ALG_MD4:
2564 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2565 input, input_length );
2566 break;
2567#endif
John Durkopee4e6602020-11-27 08:48:46 -08002568#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002569 case PSA_ALG_MD5:
2570 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2571 input, input_length );
2572 break;
2573#endif
John Durkopee4e6602020-11-27 08:48:46 -08002574#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002575 case PSA_ALG_RIPEMD160:
2576 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2577 input, input_length );
2578 break;
2579#endif
John Durkopee4e6602020-11-27 08:48:46 -08002580#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002581 case PSA_ALG_SHA_1:
2582 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2583 input, input_length );
2584 break;
2585#endif
John Durkop6ca23272020-12-03 06:01:32 -08002586#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002587 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002588 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2589 input, input_length );
2590 break;
2591#endif
2592#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002593 case PSA_ALG_SHA_256:
2594 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2595 input, input_length );
2596 break;
2597#endif
John Durkop6ca23272020-12-03 06:01:32 -08002598#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002599 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002600 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2601 input, input_length );
2602 break;
2603#endif
2604#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002605 case PSA_ALG_SHA_512:
2606 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2607 input, input_length );
2608 break;
2609#endif
2610 default:
John Durkopee4e6602020-11-27 08:48:46 -08002611 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002612 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002613 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002614
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002615 if( ret != 0 )
2616 psa_hash_abort( operation );
2617 return( mbedtls_to_psa_error( ret ) );
2618}
2619
2620psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2621 uint8_t *hash,
2622 size_t hash_size,
2623 size_t *hash_length )
2624{
itayzafrir40835d42018-08-02 13:14:17 +03002625 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002626 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002627 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002628
2629 /* Fill the output buffer with something that isn't a valid hash
2630 * (barring an attack on the hash and deliberately-crafted input),
2631 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002632 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002633 /* If hash_size is 0 then hash may be NULL and then the
2634 * call to memset would have undefined behavior. */
2635 if( hash_size != 0 )
2636 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002637
2638 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002639 {
2640 status = PSA_ERROR_BUFFER_TOO_SMALL;
2641 goto exit;
2642 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002643
2644 switch( operation->alg )
2645 {
John Durkopee4e6602020-11-27 08:48:46 -08002646#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002647 case PSA_ALG_MD2:
2648 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2649 break;
2650#endif
John Durkopee4e6602020-11-27 08:48:46 -08002651#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002652 case PSA_ALG_MD4:
2653 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2654 break;
2655#endif
John Durkopee4e6602020-11-27 08:48:46 -08002656#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002657 case PSA_ALG_MD5:
2658 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2659 break;
2660#endif
John Durkopee4e6602020-11-27 08:48:46 -08002661#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002662 case PSA_ALG_RIPEMD160:
2663 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2664 break;
2665#endif
John Durkopee4e6602020-11-27 08:48:46 -08002666#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002667 case PSA_ALG_SHA_1:
2668 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2669 break;
2670#endif
John Durkop6ca23272020-12-03 06:01:32 -08002671#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002672 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002673 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2674 break;
2675#endif
2676#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002677 case PSA_ALG_SHA_256:
2678 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2679 break;
2680#endif
John Durkop6ca23272020-12-03 06:01:32 -08002681#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002682 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002683 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2684 break;
2685#endif
2686#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002687 case PSA_ALG_SHA_512:
2688 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2689 break;
2690#endif
2691 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002692 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002693 }
itayzafrir40835d42018-08-02 13:14:17 +03002694 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002695
itayzafrir40835d42018-08-02 13:14:17 +03002696exit:
2697 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002698 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002699 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002700 return( psa_hash_abort( operation ) );
2701 }
2702 else
2703 {
2704 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002705 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002706 }
2707}
2708
Gilles Peskine2d277862018-06-18 15:41:12 +02002709psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2710 const uint8_t *hash,
2711 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002712{
2713 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2714 size_t actual_hash_length;
2715 psa_status_t status = psa_hash_finish( operation,
2716 actual_hash, sizeof( actual_hash ),
2717 &actual_hash_length );
2718 if( status != PSA_SUCCESS )
2719 return( status );
2720 if( actual_hash_length != hash_length )
2721 return( PSA_ERROR_INVALID_SIGNATURE );
2722 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2723 return( PSA_ERROR_INVALID_SIGNATURE );
2724 return( PSA_SUCCESS );
2725}
2726
Gilles Peskine0a749c82019-11-28 19:33:58 +01002727psa_status_t psa_hash_compute( psa_algorithm_t alg,
2728 const uint8_t *input, size_t input_length,
2729 uint8_t *hash, size_t hash_size,
2730 size_t *hash_length )
2731{
2732 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2733 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2734
2735 *hash_length = hash_size;
2736 status = psa_hash_setup( &operation, alg );
2737 if( status != PSA_SUCCESS )
2738 goto exit;
2739 status = psa_hash_update( &operation, input, input_length );
2740 if( status != PSA_SUCCESS )
2741 goto exit;
2742 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2743 if( status != PSA_SUCCESS )
2744 goto exit;
2745
2746exit:
2747 if( status == PSA_SUCCESS )
2748 status = psa_hash_abort( &operation );
2749 else
2750 psa_hash_abort( &operation );
2751 return( status );
2752}
2753
2754psa_status_t psa_hash_compare( psa_algorithm_t alg,
2755 const uint8_t *input, size_t input_length,
2756 const uint8_t *hash, size_t hash_length )
2757{
2758 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2759 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2760
2761 status = psa_hash_setup( &operation, alg );
2762 if( status != PSA_SUCCESS )
2763 goto exit;
2764 status = psa_hash_update( &operation, input, input_length );
2765 if( status != PSA_SUCCESS )
2766 goto exit;
2767 status = psa_hash_verify( &operation, hash, hash_length );
2768 if( status != PSA_SUCCESS )
2769 goto exit;
2770
2771exit:
2772 if( status == PSA_SUCCESS )
2773 status = psa_hash_abort( &operation );
2774 else
2775 psa_hash_abort( &operation );
2776 return( status );
2777}
2778
Gilles Peskineeb35d782019-01-22 17:56:16 +01002779psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2780 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002781{
2782 if( target_operation->alg != 0 )
2783 return( PSA_ERROR_BAD_STATE );
2784
2785 switch( source_operation->alg )
2786 {
2787 case 0:
2788 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002789#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002790 case PSA_ALG_MD2:
2791 mbedtls_md2_clone( &target_operation->ctx.md2,
2792 &source_operation->ctx.md2 );
2793 break;
2794#endif
John Durkopee4e6602020-11-27 08:48:46 -08002795#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002796 case PSA_ALG_MD4:
2797 mbedtls_md4_clone( &target_operation->ctx.md4,
2798 &source_operation->ctx.md4 );
2799 break;
2800#endif
John Durkopee4e6602020-11-27 08:48:46 -08002801#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002802 case PSA_ALG_MD5:
2803 mbedtls_md5_clone( &target_operation->ctx.md5,
2804 &source_operation->ctx.md5 );
2805 break;
2806#endif
John Durkopee4e6602020-11-27 08:48:46 -08002807#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002808 case PSA_ALG_RIPEMD160:
2809 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2810 &source_operation->ctx.ripemd160 );
2811 break;
2812#endif
John Durkopee4e6602020-11-27 08:48:46 -08002813#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002814 case PSA_ALG_SHA_1:
2815 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2816 &source_operation->ctx.sha1 );
2817 break;
2818#endif
John Durkop6ca23272020-12-03 06:01:32 -08002819#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002820 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002821 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2822 &source_operation->ctx.sha256 );
2823 break;
2824#endif
2825#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002826 case PSA_ALG_SHA_256:
2827 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2828 &source_operation->ctx.sha256 );
2829 break;
2830#endif
John Durkop6ca23272020-12-03 06:01:32 -08002831#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002832 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002833 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2834 &source_operation->ctx.sha512 );
2835 break;
2836#endif
2837#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002838 case PSA_ALG_SHA_512:
2839 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2840 &source_operation->ctx.sha512 );
2841 break;
2842#endif
2843 default:
2844 return( PSA_ERROR_NOT_SUPPORTED );
2845 }
2846
2847 target_operation->alg = source_operation->alg;
2848 return( PSA_SUCCESS );
2849}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002850
2851
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002852/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853/* MAC */
2854/****************************************************************/
2855
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002856static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857 psa_algorithm_t alg,
2858 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002859 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002860 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002861{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002863 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002864
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002865 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002866 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002867
Gilles Peskine8c9def32018-02-08 10:02:12 +01002868 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2869 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002870 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002871 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002872 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873 mode = MBEDTLS_MODE_STREAM;
2874 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875 case PSA_ALG_CTR:
2876 mode = MBEDTLS_MODE_CTR;
2877 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002878 case PSA_ALG_CFB:
2879 mode = MBEDTLS_MODE_CFB;
2880 break;
2881 case PSA_ALG_OFB:
2882 mode = MBEDTLS_MODE_OFB;
2883 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002884 case PSA_ALG_ECB_NO_PADDING:
2885 mode = MBEDTLS_MODE_ECB;
2886 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002887 case PSA_ALG_CBC_NO_PADDING:
2888 mode = MBEDTLS_MODE_CBC;
2889 break;
2890 case PSA_ALG_CBC_PKCS7:
2891 mode = MBEDTLS_MODE_CBC;
2892 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002893 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894 mode = MBEDTLS_MODE_CCM;
2895 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002896 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002897 mode = MBEDTLS_MODE_GCM;
2898 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002899 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2900 mode = MBEDTLS_MODE_CHACHAPOLY;
2901 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002902 default:
2903 return( NULL );
2904 }
2905 }
2906 else if( alg == PSA_ALG_CMAC )
2907 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002908 else
2909 return( NULL );
2910
2911 switch( key_type )
2912 {
2913 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002914 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002915 break;
2916 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002917 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2918 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002919 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002920 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002922 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002923 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2924 * but two-key Triple-DES is functionally three-key Triple-DES
2925 * with K1=K3, so that's how we present it to mbedtls. */
2926 if( key_bits == 128 )
2927 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002928 break;
2929 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002930 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002931 break;
2932 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002933 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002934 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002935 case PSA_KEY_TYPE_CHACHA20:
2936 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2937 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002938 default:
2939 return( NULL );
2940 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002941 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002942 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002943
Jaeden Amero23bbb752018-06-26 14:16:54 +01002944 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2945 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002946}
2947
John Durkop6ba40d12020-11-10 08:50:04 -08002948#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002949static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002950{
Gilles Peskine2d277862018-06-18 15:41:12 +02002951 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002952 {
2953 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002954 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002955 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002956 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002957 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002958 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002959 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002960 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002961 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002962 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002963 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002964 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002965 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002966 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002967 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002968 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002969 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002970 return( 128 );
2971 default:
2972 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002973 }
2974}
John Durkop07cc04a2020-11-16 22:08:34 -08002975#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002976
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002977/* Initialize the MAC operation structure. Once this function has been
2978 * called, psa_mac_abort can run and will do the right thing. */
2979static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2980 psa_algorithm_t alg )
2981{
2982 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2983
2984 operation->alg = alg;
2985 operation->key_set = 0;
2986 operation->iv_set = 0;
2987 operation->iv_required = 0;
2988 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002989 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002990
2991#if defined(MBEDTLS_CMAC_C)
2992 if( alg == PSA_ALG_CMAC )
2993 {
2994 operation->iv_required = 0;
2995 mbedtls_cipher_init( &operation->ctx.cmac );
2996 status = PSA_SUCCESS;
2997 }
2998 else
2999#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003000#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003001 if( PSA_ALG_IS_HMAC( operation->alg ) )
3002 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003003 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3004 operation->ctx.hmac.hash_ctx.alg = 0;
3005 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003006 }
3007 else
John Durkopd0321952020-10-29 21:37:36 -07003008#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003009 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003010 if( ! PSA_ALG_IS_MAC( alg ) )
3011 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003012 }
3013
3014 if( status != PSA_SUCCESS )
3015 memset( operation, 0, sizeof( *operation ) );
3016 return( status );
3017}
3018
John Durkop6ba40d12020-11-10 08:50:04 -08003019#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003020static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3021{
Gilles Peskine3f108122018-12-07 18:14:53 +01003022 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003023 return( psa_hash_abort( &hmac->hash_ctx ) );
3024}
John Durkop6ba40d12020-11-10 08:50:04 -08003025#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003026
Gilles Peskine8c9def32018-02-08 10:02:12 +01003027psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3028{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003029 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003031 /* The object has (apparently) been initialized but it is not
3032 * in use. It's ok to call abort on such an object, and there's
3033 * nothing to do. */
3034 return( PSA_SUCCESS );
3035 }
3036 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003037#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003038 if( operation->alg == PSA_ALG_CMAC )
3039 {
3040 mbedtls_cipher_free( &operation->ctx.cmac );
3041 }
3042 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003044#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003045 if( PSA_ALG_IS_HMAC( operation->alg ) )
3046 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003047 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003048 }
3049 else
John Durkopd0321952020-10-29 21:37:36 -07003050#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003051 {
3052 /* Sanity check (shouldn't happen: operation->alg should
3053 * always have been initialized to a valid value). */
3054 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003055 }
Moran Peker41deec42018-04-04 15:43:05 +03003056
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057 operation->alg = 0;
3058 operation->key_set = 0;
3059 operation->iv_set = 0;
3060 operation->iv_required = 0;
3061 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003062 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003063
Gilles Peskine8c9def32018-02-08 10:02:12 +01003064 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003065
3066bad_state:
3067 /* If abort is called on an uninitialized object, we can't trust
3068 * anything. Wipe the object in case it contains confidential data.
3069 * This may result in a memory leak if a pointer gets overwritten,
3070 * but it's too late to do anything about this. */
3071 memset( operation, 0, sizeof( *operation ) );
3072 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003073}
3074
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003075#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003076static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003077 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003078 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003079 const mbedtls_cipher_info_t *cipher_info )
3080{
Janos Follath24eed8d2019-11-22 13:21:35 +00003081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003082
3083 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003084
3085 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3086 if( ret != 0 )
3087 return( ret );
3088
3089 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003090 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003091 key_bits );
3092 return( ret );
3093}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003094#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003095
John Durkop6ba40d12020-11-10 08:50:04 -08003096#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003097static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3098 const uint8_t *key,
3099 size_t key_length,
3100 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003101{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003102 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003103 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003104 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003105 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003106 psa_status_t status;
3107
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003108 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3109 * overflow below. This should never trigger if the hash algorithm
3110 * is implemented correctly. */
3111 /* The size checks against the ipad and opad buffers cannot be written
3112 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3113 * because that triggers -Wlogical-op on GCC 7.3. */
3114 if( block_size > sizeof( ipad ) )
3115 return( PSA_ERROR_NOT_SUPPORTED );
3116 if( block_size > sizeof( hmac->opad ) )
3117 return( PSA_ERROR_NOT_SUPPORTED );
3118 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003119 return( PSA_ERROR_NOT_SUPPORTED );
3120
Gilles Peskined223b522018-06-11 18:12:58 +02003121 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003122 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003123 status = psa_hash_compute( hash_alg, key, key_length,
3124 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003125 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003126 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003127 }
Gilles Peskine96889972018-07-12 17:07:03 +02003128 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3129 * but it is permitted. It is common when HMAC is used in HKDF, for
3130 * example. Don't call `memcpy` in the 0-length because `key` could be
3131 * an invalid pointer which would make the behavior undefined. */
3132 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003133 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003134
Gilles Peskined223b522018-06-11 18:12:58 +02003135 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3136 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003137 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003138 ipad[i] ^= 0x36;
3139 memset( ipad + key_length, 0x36, block_size - key_length );
3140
3141 /* Copy the key material from ipad to opad, flipping the requisite bits,
3142 * and filling the rest of opad with the requisite constant. */
3143 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003144 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3145 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003146
Gilles Peskine01126fa2018-07-12 17:04:55 +02003147 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003148 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003149 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003150
Gilles Peskine01126fa2018-07-12 17:04:55 +02003151 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003152
3153cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003154 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003155
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003156 return( status );
3157}
John Durkop6ba40d12020-11-10 08:50:04 -08003158#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003159
Gilles Peskine89167cb2018-07-08 20:12:23 +02003160static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003161 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003162 psa_algorithm_t alg,
3163 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003164{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003165 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003166 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003167 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003168 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003169 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003170 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003171 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003172 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003173
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003174 /* A context must be freshly initialized before it can be set up. */
3175 if( operation->alg != 0 )
3176 {
3177 return( PSA_ERROR_BAD_STATE );
3178 }
3179
Gilles Peskined911eb72018-08-14 15:18:45 +02003180 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003181 if( status != PSA_SUCCESS )
3182 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003183 if( is_sign )
3184 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003185
Ronald Cron5c522922020-11-14 16:35:34 +01003186 status = psa_get_and_lock_transparent_key_slot_with_policy(
3187 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003188 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003189 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003190 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003191
Gilles Peskine8c9def32018-02-08 10:02:12 +01003192#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003193 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003194 {
3195 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003196 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003197 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003199 if( cipher_info == NULL )
3200 {
3201 status = PSA_ERROR_NOT_SUPPORTED;
3202 goto exit;
3203 }
3204 operation->mac_size = cipher_info->block_size;
3205 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3206 status = mbedtls_to_psa_error( ret );
3207 }
3208 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003209#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003210#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003211 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003212 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003213 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003214 if( hash_alg == 0 )
3215 {
3216 status = PSA_ERROR_NOT_SUPPORTED;
3217 goto exit;
3218 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003219
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003220 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003221 /* Sanity check. This shouldn't fail on a valid configuration. */
3222 if( operation->mac_size == 0 ||
3223 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3224 {
3225 status = PSA_ERROR_NOT_SUPPORTED;
3226 goto exit;
3227 }
3228
Gilles Peskine8e338702019-07-30 20:06:31 +02003229 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003230 {
3231 status = PSA_ERROR_INVALID_ARGUMENT;
3232 goto exit;
3233 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003234
Gilles Peskine01126fa2018-07-12 17:04:55 +02003235 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003236 slot->key.data,
3237 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003238 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003239 }
3240 else
John Durkopd0321952020-10-29 21:37:36 -07003241#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003242 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003243 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003244 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003245 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003246
Gilles Peskined911eb72018-08-14 15:18:45 +02003247 if( truncated == 0 )
3248 {
3249 /* The "normal" case: untruncated algorithm. Nothing to do. */
3250 }
3251 else if( truncated < 4 )
3252 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003253 /* A very short MAC is too short for security since it can be
3254 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3255 * so we make this our minimum, even though 32 bits is still
3256 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003257 status = PSA_ERROR_NOT_SUPPORTED;
3258 }
3259 else if( truncated > operation->mac_size )
3260 {
3261 /* It's impossible to "truncate" to a larger length. */
3262 status = PSA_ERROR_INVALID_ARGUMENT;
3263 }
3264 else
3265 operation->mac_size = truncated;
3266
Gilles Peskinefbfac682018-07-08 20:51:54 +02003267exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003268 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003269 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003270 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003271 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003272 else
3273 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003274 operation->key_set = 1;
3275 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003276
Ronald Cron5c522922020-11-14 16:35:34 +01003277 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003278
Ronald Cron5c522922020-11-14 16:35:34 +01003279 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003280}
3281
Gilles Peskine89167cb2018-07-08 20:12:23 +02003282psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003283 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003284 psa_algorithm_t alg )
3285{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003286 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003287}
3288
3289psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003290 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003291 psa_algorithm_t alg )
3292{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003293 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003294}
3295
Gilles Peskine8c9def32018-02-08 10:02:12 +01003296psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3297 const uint8_t *input,
3298 size_t input_length )
3299{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003300 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003301 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003302 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003303 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003304 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003305 operation->has_input = 1;
3306
Gilles Peskine8c9def32018-02-08 10:02:12 +01003307#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003308 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003309 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003310 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3311 input, input_length );
3312 status = mbedtls_to_psa_error( ret );
3313 }
3314 else
3315#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003316#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003317 if( PSA_ALG_IS_HMAC( operation->alg ) )
3318 {
3319 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3320 input_length );
3321 }
3322 else
John Durkopd0321952020-10-29 21:37:36 -07003323#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003324 {
3325 /* This shouldn't happen if `operation` was initialized by
3326 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003327 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003328 }
3329
Gilles Peskinefbfac682018-07-08 20:51:54 +02003330 if( status != PSA_SUCCESS )
3331 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003332 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003333}
3334
John Durkop6ba40d12020-11-10 08:50:04 -08003335#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003336static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3337 uint8_t *mac,
3338 size_t mac_size )
3339{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003340 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003341 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3342 size_t hash_size = 0;
3343 size_t block_size = psa_get_hash_block_size( hash_alg );
3344 psa_status_t status;
3345
Gilles Peskine01126fa2018-07-12 17:04:55 +02003346 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3347 if( status != PSA_SUCCESS )
3348 return( status );
3349 /* From here on, tmp needs to be wiped. */
3350
3351 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3352 if( status != PSA_SUCCESS )
3353 goto exit;
3354
3355 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3356 if( status != PSA_SUCCESS )
3357 goto exit;
3358
3359 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3360 if( status != PSA_SUCCESS )
3361 goto exit;
3362
Gilles Peskined911eb72018-08-14 15:18:45 +02003363 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3364 if( status != PSA_SUCCESS )
3365 goto exit;
3366
3367 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003368
3369exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003370 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003371 return( status );
3372}
John Durkop6ba40d12020-11-10 08:50:04 -08003373#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003374
mohammad16036df908f2018-04-02 08:34:15 -07003375static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003376 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003377 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003378{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003379 if( ! operation->key_set )
3380 return( PSA_ERROR_BAD_STATE );
3381 if( operation->iv_required && ! operation->iv_set )
3382 return( PSA_ERROR_BAD_STATE );
3383
Gilles Peskine8c9def32018-02-08 10:02:12 +01003384 if( mac_size < operation->mac_size )
3385 return( PSA_ERROR_BUFFER_TOO_SMALL );
3386
Gilles Peskine8c9def32018-02-08 10:02:12 +01003387#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003388 if( operation->alg == PSA_ALG_CMAC )
3389 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003390 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003391 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3392 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003393 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003394 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003395 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003396 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003397 else
3398#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003399#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003400 if( PSA_ALG_IS_HMAC( operation->alg ) )
3401 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003402 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003403 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003404 }
3405 else
John Durkopd0321952020-10-29 21:37:36 -07003406#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003407 {
3408 /* This shouldn't happen if `operation` was initialized by
3409 * a setup function. */
3410 return( PSA_ERROR_BAD_STATE );
3411 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003412}
3413
Gilles Peskineacd4be32018-07-08 19:56:25 +02003414psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3415 uint8_t *mac,
3416 size_t mac_size,
3417 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003418{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003419 psa_status_t status;
3420
Jaeden Amero252ef282019-02-15 14:05:35 +00003421 if( operation->alg == 0 )
3422 {
3423 return( PSA_ERROR_BAD_STATE );
3424 }
3425
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003426 /* Fill the output buffer with something that isn't a valid mac
3427 * (barring an attack on the mac and deliberately-crafted input),
3428 * in case the caller doesn't check the return status properly. */
3429 *mac_length = mac_size;
3430 /* If mac_size is 0 then mac may be NULL and then the
3431 * call to memset would have undefined behavior. */
3432 if( mac_size != 0 )
3433 memset( mac, '!', mac_size );
3434
Gilles Peskine89167cb2018-07-08 20:12:23 +02003435 if( ! operation->is_sign )
3436 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003437 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003438 }
mohammad16036df908f2018-04-02 08:34:15 -07003439
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003440 status = psa_mac_finish_internal( operation, mac, mac_size );
3441
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003442 if( status == PSA_SUCCESS )
3443 {
3444 status = psa_mac_abort( operation );
3445 if( status == PSA_SUCCESS )
3446 *mac_length = operation->mac_size;
3447 else
3448 memset( mac, '!', mac_size );
3449 }
3450 else
3451 psa_mac_abort( operation );
3452 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003453}
3454
Gilles Peskineacd4be32018-07-08 19:56:25 +02003455psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3456 const uint8_t *mac,
3457 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003458{
Gilles Peskine828ed142018-06-18 23:25:51 +02003459 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003460 psa_status_t status;
3461
Jaeden Amero252ef282019-02-15 14:05:35 +00003462 if( operation->alg == 0 )
3463 {
3464 return( PSA_ERROR_BAD_STATE );
3465 }
3466
Gilles Peskine89167cb2018-07-08 20:12:23 +02003467 if( operation->is_sign )
3468 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003469 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003470 }
3471 if( operation->mac_size != mac_length )
3472 {
3473 status = PSA_ERROR_INVALID_SIGNATURE;
3474 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003475 }
mohammad16036df908f2018-04-02 08:34:15 -07003476
3477 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003478 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003479 if( status != PSA_SUCCESS )
3480 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003481
3482 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3483 status = PSA_ERROR_INVALID_SIGNATURE;
3484
3485cleanup:
3486 if( status == PSA_SUCCESS )
3487 status = psa_mac_abort( operation );
3488 else
3489 psa_mac_abort( operation );
3490
Gilles Peskine3f108122018-12-07 18:14:53 +01003491 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003492
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003493 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003494}
3495
3496
Gilles Peskine20035e32018-02-03 22:44:14 +01003497
Gilles Peskine20035e32018-02-03 22:44:14 +01003498/****************************************************************/
3499/* Asymmetric cryptography */
3500/****************************************************************/
3501
John Durkop6ba40d12020-11-10 08:50:04 -08003502#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3503 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003504/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003505 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003506static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3507 size_t hash_length,
3508 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003509{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003510 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003511 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003512 *md_alg = mbedtls_md_get_type( md_info );
3513
3514 /* The Mbed TLS RSA module uses an unsigned int for hash length
3515 * parameters. Validate that it fits so that we don't risk an
3516 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003517#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003518 if( hash_length > UINT_MAX )
3519 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003520#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003521
John Durkop0e005192020-10-31 22:06:54 -07003522#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003523 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3524 * must be correct. */
3525 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3526 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003527 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003528 if( md_info == NULL )
3529 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003530 if( mbedtls_md_get_size( md_info ) != hash_length )
3531 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003532 }
John Durkop0e005192020-10-31 22:06:54 -07003533#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003534
John Durkop0e005192020-10-31 22:06:54 -07003535#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003536 /* PSS requires a hash internally. */
3537 if( PSA_ALG_IS_RSA_PSS( alg ) )
3538 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003539 if( md_info == NULL )
3540 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003541 }
John Durkop0e005192020-10-31 22:06:54 -07003542#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003543
Gilles Peskine61b91d42018-06-08 16:09:36 +02003544 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003545}
3546
Gilles Peskine2b450e32018-06-27 15:42:46 +02003547static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3548 psa_algorithm_t alg,
3549 const uint8_t *hash,
3550 size_t hash_length,
3551 uint8_t *signature,
3552 size_t signature_size,
3553 size_t *signature_length )
3554{
3555 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003557 mbedtls_md_type_t md_alg;
3558
3559 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3560 if( status != PSA_SUCCESS )
3561 return( status );
3562
Gilles Peskine630a18a2018-06-29 17:49:35 +02003563 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003564 return( PSA_ERROR_BUFFER_TOO_SMALL );
3565
John Durkop0e005192020-10-31 22:06:54 -07003566#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003567 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3568 {
3569 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3570 MBEDTLS_MD_NONE );
3571 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003572 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003573 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003574 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003575 md_alg,
3576 (unsigned int) hash_length,
3577 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003578 signature );
3579 }
3580 else
John Durkop0e005192020-10-31 22:06:54 -07003581#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3582#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003583 if( PSA_ALG_IS_RSA_PSS( alg ) )
3584 {
3585 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3586 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003587 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003588 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003589 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003590 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003591 (unsigned int) hash_length,
3592 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003593 signature );
3594 }
3595 else
John Durkop0e005192020-10-31 22:06:54 -07003596#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003597 {
3598 return( PSA_ERROR_INVALID_ARGUMENT );
3599 }
3600
3601 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003602 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003603 return( mbedtls_to_psa_error( ret ) );
3604}
3605
3606static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3607 psa_algorithm_t alg,
3608 const uint8_t *hash,
3609 size_t hash_length,
3610 const uint8_t *signature,
3611 size_t signature_length )
3612{
3613 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003615 mbedtls_md_type_t md_alg;
3616
3617 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3618 if( status != PSA_SUCCESS )
3619 return( status );
3620
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003621 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3622 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003623
John Durkop0e005192020-10-31 22:06:54 -07003624#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003625 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3626 {
3627 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3628 MBEDTLS_MD_NONE );
3629 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003630 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003631 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003632 MBEDTLS_RSA_PUBLIC,
3633 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003634 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003635 hash,
3636 signature );
3637 }
3638 else
John Durkop0e005192020-10-31 22:06:54 -07003639#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3640#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003641 if( PSA_ALG_IS_RSA_PSS( alg ) )
3642 {
3643 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3644 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003645 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003646 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003647 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003648 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003649 (unsigned int) hash_length,
3650 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003651 signature );
3652 }
3653 else
John Durkop0e005192020-10-31 22:06:54 -07003654#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003655 {
3656 return( PSA_ERROR_INVALID_ARGUMENT );
3657 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003658
3659 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3660 * the rest of the signature is invalid". This has little use in
3661 * practice and PSA doesn't report this distinction. */
3662 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3663 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003664 return( mbedtls_to_psa_error( ret ) );
3665}
John Durkop6ba40d12020-11-10 08:50:04 -08003666#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3667 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003668
John Durkop6ba40d12020-11-10 08:50:04 -08003669#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3670 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003671/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3672 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3673 * (even though these functions don't modify it). */
3674static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3675 psa_algorithm_t alg,
3676 const uint8_t *hash,
3677 size_t hash_length,
3678 uint8_t *signature,
3679 size_t signature_size,
3680 size_t *signature_length )
3681{
Janos Follath24eed8d2019-11-22 13:21:35 +00003682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003683 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003684 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003685 mbedtls_mpi_init( &r );
3686 mbedtls_mpi_init( &s );
3687
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003688 if( signature_size < 2 * curve_bytes )
3689 {
3690 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3691 goto cleanup;
3692 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003693
John Durkop0ea39e02020-10-13 19:58:20 -07003694#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003695 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3696 {
3697 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3698 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3699 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003700 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3701 &ecp->d, hash,
3702 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003703 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003704 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003705 }
3706 else
John Durkop0ea39e02020-10-13 19:58:20 -07003707#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003708 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003709 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003710 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3711 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003712 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003713 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003714 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003715
3716 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3717 signature,
3718 curve_bytes ) );
3719 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3720 signature + curve_bytes,
3721 curve_bytes ) );
3722
3723cleanup:
3724 mbedtls_mpi_free( &r );
3725 mbedtls_mpi_free( &s );
3726 if( ret == 0 )
3727 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003728 return( mbedtls_to_psa_error( ret ) );
3729}
3730
3731static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3732 const uint8_t *hash,
3733 size_t hash_length,
3734 const uint8_t *signature,
3735 size_t signature_length )
3736{
Janos Follath24eed8d2019-11-22 13:21:35 +00003737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003738 mbedtls_mpi r, s;
3739 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3740 mbedtls_mpi_init( &r );
3741 mbedtls_mpi_init( &s );
3742
3743 if( signature_length != 2 * curve_bytes )
3744 return( PSA_ERROR_INVALID_SIGNATURE );
3745
3746 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3747 signature,
3748 curve_bytes ) );
3749 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3750 signature + curve_bytes,
3751 curve_bytes ) );
3752
Steven Cooremanacda8342020-07-24 23:09:52 +02003753 /* Check whether the public part is loaded. If not, load it. */
3754 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3755 {
3756 MBEDTLS_MPI_CHK(
3757 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003758 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003759 }
3760
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003761 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3762 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003763
3764cleanup:
3765 mbedtls_mpi_free( &r );
3766 mbedtls_mpi_free( &s );
3767 return( mbedtls_to_psa_error( ret ) );
3768}
John Durkop6ba40d12020-11-10 08:50:04 -08003769#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3770 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003771
Ronald Croncf56a0a2020-08-04 09:51:30 +02003772psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003773 psa_algorithm_t alg,
3774 const uint8_t *hash,
3775 size_t hash_length,
3776 uint8_t *signature,
3777 size_t signature_size,
3778 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003779{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003780 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003781 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003782 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003783
3784 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003785 /* Immediately reject a zero-length signature buffer. This guarantees
3786 * that signature must be a valid pointer. (On the other hand, the hash
3787 * buffer can in principle be empty since it doesn't actually have
3788 * to be a hash.) */
3789 if( signature_size == 0 )
3790 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003791
Ronald Cron5c522922020-11-14 16:35:34 +01003792 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3793 PSA_KEY_USAGE_SIGN_HASH,
3794 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003795 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003796 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003797 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003798 {
3799 status = PSA_ERROR_INVALID_ARGUMENT;
3800 goto exit;
3801 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003802
Steven Cooremancd84cb42020-07-16 20:28:36 +02003803 /* Try any of the available accelerators first */
3804 status = psa_driver_wrapper_sign_hash( slot,
3805 alg,
3806 hash,
3807 hash_length,
3808 signature,
3809 signature_size,
3810 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003811 if( status != PSA_ERROR_NOT_SUPPORTED ||
3812 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003813 goto exit;
3814
Steven Cooreman7a250572020-07-17 16:43:05 +02003815 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003816#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3817 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003818 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003819 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003820 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003821
Ronald Cron90857082020-11-25 14:58:33 +01003822 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3823 slot->key.data,
3824 slot->key.bytes,
3825 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003826 if( status != PSA_SUCCESS )
3827 goto exit;
3828
Steven Cooremana2371e52020-07-28 14:30:39 +02003829 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003830 alg,
3831 hash, hash_length,
3832 signature, signature_size,
3833 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003834
Steven Cooremana2371e52020-07-28 14:30:39 +02003835 mbedtls_rsa_free( rsa );
3836 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003837 }
3838 else
John Durkop6ba40d12020-11-10 08:50:04 -08003839#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3840 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003841 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003842 {
John Durkop9814fa22020-11-04 12:28:15 -08003843#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3844 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003845 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003846#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003847 PSA_ALG_IS_ECDSA( alg )
3848#else
3849 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3850#endif
3851 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003852 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003853 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003854 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3855 slot->key.data,
3856 slot->key.bytes,
3857 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003858 if( status != PSA_SUCCESS )
3859 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003860 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003861 alg,
3862 hash, hash_length,
3863 signature, signature_size,
3864 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003865 mbedtls_ecp_keypair_free( ecp );
3866 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003867 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003868 else
John Durkop9814fa22020-11-04 12:28:15 -08003869#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3870 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003871 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003872 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003873 }
itayzafrir5c753392018-05-08 11:18:38 +03003874 }
3875 else
itayzafrir5c753392018-05-08 11:18:38 +03003876 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003877 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003878 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003879
3880exit:
3881 /* Fill the unused part of the output buffer (the whole buffer on error,
3882 * the trailing part on success) with something that isn't a valid mac
3883 * (barring an attack on the mac and deliberately-crafted input),
3884 * in case the caller doesn't check the return status properly. */
3885 if( status == PSA_SUCCESS )
3886 memset( signature + *signature_length, '!',
3887 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003888 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003889 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003890 /* If signature_size is 0 then we have nothing to do. We must not call
3891 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003892
Ronald Cron5c522922020-11-14 16:35:34 +01003893 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003894
Ronald Cron5c522922020-11-14 16:35:34 +01003895 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003896}
3897
Ronald Croncf56a0a2020-08-04 09:51:30 +02003898psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003899 psa_algorithm_t alg,
3900 const uint8_t *hash,
3901 size_t hash_length,
3902 const uint8_t *signature,
3903 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003904{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003905 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003906 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003907 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003908
Ronald Cron5c522922020-11-14 16:35:34 +01003909 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3910 PSA_KEY_USAGE_VERIFY_HASH,
3911 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003912 if( status != PSA_SUCCESS )
3913 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003914
Steven Cooreman55ae2172020-07-17 19:46:15 +02003915 /* Try any of the available accelerators first */
3916 status = psa_driver_wrapper_verify_hash( slot,
3917 alg,
3918 hash,
3919 hash_length,
3920 signature,
3921 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003922 if( status != PSA_ERROR_NOT_SUPPORTED ||
3923 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003924 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003925
John Durkop6ba40d12020-11-10 08:50:04 -08003926#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3927 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003928 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003929 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003930 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003931
Ronald Cron90857082020-11-25 14:58:33 +01003932 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3933 slot->key.data,
3934 slot->key.bytes,
3935 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003936 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003937 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003938
Steven Cooremana2371e52020-07-28 14:30:39 +02003939 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003940 alg,
3941 hash, hash_length,
3942 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003943 mbedtls_rsa_free( rsa );
3944 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003945 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946 }
3947 else
John Durkop6ba40d12020-11-10 08:50:04 -08003948#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3949 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003950 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003951 {
John Durkop9814fa22020-11-04 12:28:15 -08003952#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3953 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003954 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003955 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003956 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003957 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
3958 slot->key.data,
3959 slot->key.bytes,
3960 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003961 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003962 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003963 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003964 hash, hash_length,
3965 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003966 mbedtls_ecp_keypair_free( ecp );
3967 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003968 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003969 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003970 else
John Durkop9814fa22020-11-04 12:28:15 -08003971#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3972 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003973 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003974 status = PSA_ERROR_INVALID_ARGUMENT;
3975 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003976 }
itayzafrir5c753392018-05-08 11:18:38 +03003977 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003978 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003979 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003980 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003981 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003982
3983exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003984 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003985
Ronald Cron5c522922020-11-14 16:35:34 +01003986 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003987}
3988
John Durkop0e005192020-10-31 22:06:54 -07003989#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003990static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3991 mbedtls_rsa_context *rsa )
3992{
3993 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3994 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3995 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3996 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3997}
John Durkop0e005192020-10-31 22:06:54 -07003998#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003999
Ronald Croncf56a0a2020-08-04 09:51:30 +02004000psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004001 psa_algorithm_t alg,
4002 const uint8_t *input,
4003 size_t input_length,
4004 const uint8_t *salt,
4005 size_t salt_length,
4006 uint8_t *output,
4007 size_t output_size,
4008 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004009{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004011 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004012 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004013
Darryl Green5cc689a2018-07-24 15:34:10 +01004014 (void) input;
4015 (void) input_length;
4016 (void) salt;
4017 (void) output;
4018 (void) output_size;
4019
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004020 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004022 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4023 return( PSA_ERROR_INVALID_ARGUMENT );
4024
Ronald Cron5c522922020-11-14 16:35:34 +01004025 status = psa_get_and_lock_transparent_key_slot_with_policy(
4026 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004027 if( status != PSA_SUCCESS )
4028 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004029 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4030 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004031 {
4032 status = PSA_ERROR_INVALID_ARGUMENT;
4033 goto exit;
4034 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035
John Durkop6ba40d12020-11-10 08:50:04 -08004036#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4037 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004038 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004039 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004040 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004041 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
4042 slot->key.data,
4043 slot->key.bytes,
4044 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004045 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004046 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004047
4048 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004049 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004050 status = PSA_ERROR_BUFFER_TOO_SMALL;
4051 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004052 }
John Durkop0e005192020-10-31 22:06:54 -07004053#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004054 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004055 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004056 status = mbedtls_to_psa_error(
4057 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004058 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004059 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004060 MBEDTLS_RSA_PUBLIC,
4061 input_length,
4062 input,
4063 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004064 }
4065 else
John Durkop0e005192020-10-31 22:06:54 -07004066#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4067#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004068 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004069 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004070 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004071 status = mbedtls_to_psa_error(
4072 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004073 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004074 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004075 MBEDTLS_RSA_PUBLIC,
4076 salt, salt_length,
4077 input_length,
4078 input,
4079 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004080 }
4081 else
John Durkop0e005192020-10-31 22:06:54 -07004082#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004084 status = PSA_ERROR_INVALID_ARGUMENT;
4085 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004086 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004087rsa_exit:
4088 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004089 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004090
Steven Cooremana2371e52020-07-28 14:30:39 +02004091 mbedtls_rsa_free( rsa );
4092 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004093 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004094 else
John Durkop9814fa22020-11-04 12:28:15 -08004095#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004096 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004097 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004098 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004099 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004100
4101exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004102 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004103
Ronald Cron5c522922020-11-14 16:35:34 +01004104 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004105}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004106
Ronald Croncf56a0a2020-08-04 09:51:30 +02004107psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004108 psa_algorithm_t alg,
4109 const uint8_t *input,
4110 size_t input_length,
4111 const uint8_t *salt,
4112 size_t salt_length,
4113 uint8_t *output,
4114 size_t output_size,
4115 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004116{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004117 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004118 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004119 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004120
Darryl Green5cc689a2018-07-24 15:34:10 +01004121 (void) input;
4122 (void) input_length;
4123 (void) salt;
4124 (void) output;
4125 (void) output_size;
4126
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004127 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004128
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004129 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4130 return( PSA_ERROR_INVALID_ARGUMENT );
4131
Ronald Cron5c522922020-11-14 16:35:34 +01004132 status = psa_get_and_lock_transparent_key_slot_with_policy(
4133 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004134 if( status != PSA_SUCCESS )
4135 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004136 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004137 {
4138 status = PSA_ERROR_INVALID_ARGUMENT;
4139 goto exit;
4140 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004141
John Durkop6ba40d12020-11-10 08:50:04 -08004142#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4143 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004144 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004145 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004146 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01004147 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
4148 slot->key.data,
4149 slot->key.bytes,
4150 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004151 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004152 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004153
Steven Cooremana2371e52020-07-28 14:30:39 +02004154 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004155 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004156 status = PSA_ERROR_INVALID_ARGUMENT;
4157 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004158 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004159
John Durkop0e005192020-10-31 22:06:54 -07004160#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004161 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004162 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004163 status = mbedtls_to_psa_error(
4164 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004165 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004166 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004167 MBEDTLS_RSA_PRIVATE,
4168 output_length,
4169 input,
4170 output,
4171 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004172 }
4173 else
John Durkop0e005192020-10-31 22:06:54 -07004174#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4175#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004176 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004177 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004178 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004179 status = mbedtls_to_psa_error(
4180 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004181 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004182 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004183 MBEDTLS_RSA_PRIVATE,
4184 salt, salt_length,
4185 output_length,
4186 input,
4187 output,
4188 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004189 }
4190 else
John Durkop0e005192020-10-31 22:06:54 -07004191#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004192 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004193 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004194 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004195
Steven Cooreman4fed4552020-08-03 14:46:03 +02004196rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004197 mbedtls_rsa_free( rsa );
4198 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004200 else
John Durkop6ba40d12020-11-10 08:50:04 -08004201#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4202 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004203 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004204 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004205 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004206
4207exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004208 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004209
Ronald Cron5c522922020-11-14 16:35:34 +01004210 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004211}
Gilles Peskine20035e32018-02-03 22:44:14 +01004212
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004213
4214
mohammad1603503973b2018-03-12 15:59:30 +02004215/****************************************************************/
4216/* Symmetric cryptography */
4217/****************************************************************/
4218
Gilles Peskinee553c652018-06-04 16:22:46 +02004219static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004220 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004221 psa_algorithm_t alg,
4222 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004223{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004224 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004225 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004226 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004227 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004228 size_t key_bits;
4229 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004230 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4231 PSA_KEY_USAGE_ENCRYPT :
4232 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004233
Steven Cooremand3feccd2020-09-01 15:56:14 +02004234 /* A context must be freshly initialized before it can be set up. */
4235 if( operation->alg != 0 )
4236 return( PSA_ERROR_BAD_STATE );
4237
Steven Cooremana07b9972020-09-10 14:54:14 +02004238 /* The requested algorithm must be one that can be processed by cipher. */
4239 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004240 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004241
Steven Cooremanef8575e2020-09-11 11:44:50 +02004242 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004243 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004244 if( status != PSA_SUCCESS )
4245 goto exit;
4246
4247 /* Initialize the operation struct members, except for alg. The alg member
4248 * is used to indicate to psa_cipher_abort that there are resources to free,
4249 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004250 operation->key_set = 0;
4251 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004252 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004253 operation->iv_size = 0;
4254 operation->block_size = 0;
4255 if( alg == PSA_ALG_ECB_NO_PADDING )
4256 operation->iv_required = 0;
4257 else
4258 operation->iv_required = 1;
4259
Steven Cooremana07b9972020-09-10 14:54:14 +02004260 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004261 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004262 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004263 slot,
4264 alg );
4265 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004266 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004267 slot,
4268 alg );
4269
Steven Cooremanef8575e2020-09-11 11:44:50 +02004270 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004271 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004272 /* Once the driver context is initialised, it needs to be freed using
4273 * psa_cipher_abort. Indicate this through setting alg. */
4274 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004275 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004276
Steven Cooremand3feccd2020-09-01 15:56:14 +02004277 if( status != PSA_ERROR_NOT_SUPPORTED ||
4278 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4279 goto exit;
4280
Steven Cooremana07b9972020-09-10 14:54:14 +02004281 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004282 * available for the given algorithm & key. */
4283 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004284
Steven Cooremana07b9972020-09-10 14:54:14 +02004285 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004286 * psa_cipher_abort. Indicate there is something to be freed through setting
4287 * alg, and indicate the operation is being done using mbedtls crypto through
4288 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004289 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004290 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004291
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004292 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004293 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004294 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004295 {
4296 status = PSA_ERROR_NOT_SUPPORTED;
4297 goto exit;
4298 }
mohammad1603503973b2018-03-12 15:59:30 +02004299
mohammad1603503973b2018-03-12 15:59:30 +02004300 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004301 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004302 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004303
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004304#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004305 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004306 {
4307 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004308 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004309 memcpy( keys, slot->key.data, 16 );
4310 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004311 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4312 keys,
4313 192, cipher_operation );
4314 }
4315 else
4316#endif
4317 {
4318 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004319 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004320 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004321 }
Moran Peker41deec42018-04-04 15:43:05 +03004322 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004323 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004324
mohammad16038481e742018-03-18 13:57:31 +02004325#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004326 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004327 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004328 case PSA_ALG_CBC_NO_PADDING:
4329 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4330 MBEDTLS_PADDING_NONE );
4331 break;
4332 case PSA_ALG_CBC_PKCS7:
4333 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4334 MBEDTLS_PADDING_PKCS7 );
4335 break;
4336 default:
4337 /* The algorithm doesn't involve padding. */
4338 ret = 0;
4339 break;
4340 }
4341 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004342 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004343#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4344
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004345 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004346 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004347 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4348 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004349 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004350 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004351 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004352#if defined(MBEDTLS_CHACHA20_C)
4353 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004354 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004355 operation->iv_size = 12;
4356#endif
mohammad1603503973b2018-03-12 15:59:30 +02004357
Steven Cooreman7df02922020-09-09 15:28:49 +02004358 status = PSA_SUCCESS;
4359
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004360exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004361 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004362 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004363 if( status == PSA_SUCCESS )
4364 {
4365 /* Update operation flags for both driver and software implementations */
4366 operation->key_set = 1;
4367 }
4368 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004369 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004370
Ronald Cron5c522922020-11-14 16:35:34 +01004371 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004372
Ronald Cron5c522922020-11-14 16:35:34 +01004373 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004374}
4375
Gilles Peskinefe119512018-07-08 21:39:34 +02004376psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004377 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004378 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004379{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004380 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004381}
4382
Gilles Peskinefe119512018-07-08 21:39:34 +02004383psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004384 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004385 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004386{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004387 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004388}
4389
Gilles Peskinefe119512018-07-08 21:39:34 +02004390psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004391 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004392 size_t iv_size,
4393 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004394{
itayzafrir534bd7c2018-08-02 13:56:32 +03004395 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004397 if( operation->iv_set || ! operation->iv_required )
4398 {
4399 return( PSA_ERROR_BAD_STATE );
4400 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004401
Steven Cooremanef8575e2020-09-11 11:44:50 +02004402 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004403 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004404 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004405 iv,
4406 iv_size,
4407 iv_length );
4408 goto exit;
4409 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004410
Moran Peker41deec42018-04-04 15:43:05 +03004411 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004412 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004413 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004414 goto exit;
4415 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004416 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004417 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004418 if( ret != 0 )
4419 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004420 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004421 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004422 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004423
mohammad16038481e742018-03-18 13:57:31 +02004424 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004425 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004426
Moran Peker395db872018-05-31 14:07:14 +03004427exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004428 if( status == PSA_SUCCESS )
4429 operation->iv_set = 1;
4430 else
Moran Peker395db872018-05-31 14:07:14 +03004431 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004432 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004433}
4434
Gilles Peskinefe119512018-07-08 21:39:34 +02004435psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004436 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004437 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004438{
itayzafrir534bd7c2018-08-02 13:56:32 +03004439 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004441 if( operation->iv_set || ! operation->iv_required )
4442 {
4443 return( PSA_ERROR_BAD_STATE );
4444 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004445
Steven Cooremanef8575e2020-09-11 11:44:50 +02004446 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004447 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004448 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004449 iv,
4450 iv_length );
4451 goto exit;
4452 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004453
Moran Pekera28258c2018-05-29 16:25:04 +03004454 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004455 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004456 status = PSA_ERROR_INVALID_ARGUMENT;
4457 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004458 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004459 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4460 status = mbedtls_to_psa_error( ret );
4461exit:
4462 if( status == PSA_SUCCESS )
4463 operation->iv_set = 1;
4464 else
mohammad1603503973b2018-03-12 15:59:30 +02004465 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004466 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004467}
4468
Steven Cooreman177deba2020-09-07 17:14:14 +02004469/* Process input for which the algorithm is set to ECB mode. This requires
4470 * manual processing, since the PSA API is defined as being able to process
4471 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4472 * underlying mbedtls_cipher_update only takes full blocks. */
4473static psa_status_t psa_cipher_update_ecb_internal(
4474 mbedtls_cipher_context_t *ctx,
4475 const uint8_t *input,
4476 size_t input_length,
4477 uint8_t *output,
4478 size_t output_size,
4479 size_t *output_length )
4480{
4481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4482 size_t block_size = ctx->cipher_info->block_size;
4483 size_t internal_output_length = 0;
4484 *output_length = 0;
4485
4486 if( input_length == 0 )
4487 {
4488 status = PSA_SUCCESS;
4489 goto exit;
4490 }
4491
4492 if( ctx->unprocessed_len > 0 )
4493 {
4494 /* Fill up to block size, and run the block if there's a full one. */
4495 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4496
4497 if( input_length < bytes_to_copy )
4498 bytes_to_copy = input_length;
4499
4500 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4501 input, bytes_to_copy );
4502 input_length -= bytes_to_copy;
4503 input += bytes_to_copy;
4504 ctx->unprocessed_len += bytes_to_copy;
4505
4506 if( ctx->unprocessed_len == block_size )
4507 {
4508 status = mbedtls_to_psa_error(
4509 mbedtls_cipher_update( ctx,
4510 ctx->unprocessed_data,
4511 block_size,
4512 output, &internal_output_length ) );
4513
4514 if( status != PSA_SUCCESS )
4515 goto exit;
4516
4517 output += internal_output_length;
4518 output_size -= internal_output_length;
4519 *output_length += internal_output_length;
4520 ctx->unprocessed_len = 0;
4521 }
4522 }
4523
4524 while( input_length >= block_size )
4525 {
4526 /* Run all full blocks we have, one by one */
4527 status = mbedtls_to_psa_error(
4528 mbedtls_cipher_update( ctx, input,
4529 block_size,
4530 output, &internal_output_length ) );
4531
4532 if( status != PSA_SUCCESS )
4533 goto exit;
4534
4535 input_length -= block_size;
4536 input += block_size;
4537
4538 output += internal_output_length;
4539 output_size -= internal_output_length;
4540 *output_length += internal_output_length;
4541 }
4542
4543 if( input_length > 0 )
4544 {
4545 /* Save unprocessed bytes for later processing */
4546 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4547 input, input_length );
4548 ctx->unprocessed_len += input_length;
4549 }
4550
4551 status = PSA_SUCCESS;
4552
4553exit:
4554 return( status );
4555}
4556
Gilles Peskinee553c652018-06-04 16:22:46 +02004557psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4558 const uint8_t *input,
4559 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004560 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004561 size_t output_size,
4562 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004563{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004564 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004565 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004566 if( operation->alg == 0 )
4567 {
4568 return( PSA_ERROR_BAD_STATE );
4569 }
4570 if( operation->iv_required && ! operation->iv_set )
4571 {
4572 return( PSA_ERROR_BAD_STATE );
4573 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004574
Steven Cooremanef8575e2020-09-11 11:44:50 +02004575 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004576 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004577 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004578 input,
4579 input_length,
4580 output,
4581 output_size,
4582 output_length );
4583 goto exit;
4584 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004585
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004586 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004587 {
4588 /* Take the unprocessed partial block left over from previous
4589 * update calls, if any, plus the input to this call. Remove
4590 * the last partial block, if any. You get the data that will be
4591 * output in this call. */
4592 expected_output_size =
4593 ( operation->ctx.cipher.unprocessed_len + input_length )
4594 / operation->block_size * operation->block_size;
4595 }
4596 else
4597 {
4598 expected_output_size = input_length;
4599 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004600
Gilles Peskine89d789c2018-06-04 17:17:16 +02004601 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004602 {
4603 status = PSA_ERROR_BUFFER_TOO_SMALL;
4604 goto exit;
4605 }
mohammad160382759612018-03-12 18:16:40 +02004606
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004607 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4608 {
4609 /* mbedtls_cipher_update has an API inconsistency: it will only
4610 * process a single block at a time in ECB mode. Abstract away that
4611 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004612 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4613 input,
4614 input_length,
4615 output,
4616 output_size,
4617 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004618 }
4619 else
4620 {
4621 status = mbedtls_to_psa_error(
4622 mbedtls_cipher_update( &operation->ctx.cipher, input,
4623 input_length, output, output_length ) );
4624 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004625exit:
4626 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004627 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004628 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004629}
4630
Gilles Peskinee553c652018-06-04 16:22:46 +02004631psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4632 uint8_t *output,
4633 size_t output_size,
4634 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004635{
David Saadab4ecc272019-02-14 13:48:10 +02004636 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004637 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004638 if( operation->alg == 0 )
4639 {
4640 return( PSA_ERROR_BAD_STATE );
4641 }
4642 if( operation->iv_required && ! operation->iv_set )
4643 {
4644 return( PSA_ERROR_BAD_STATE );
4645 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004646
Steven Cooremanef8575e2020-09-11 11:44:50 +02004647 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004648 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004649 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004650 output,
4651 output_size,
4652 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004653 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004654 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004655
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004656 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004657 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004658 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004659 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004660 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004661 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004662 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004663 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004664 }
4665
Steven Cooremanef8575e2020-09-11 11:44:50 +02004666 status = mbedtls_to_psa_error(
4667 mbedtls_cipher_finish( &operation->ctx.cipher,
4668 temp_output_buffer,
4669 output_length ) );
4670 if( status != PSA_SUCCESS )
4671 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004672
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004673 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004674 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004675 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004676 memcpy( output, temp_output_buffer, *output_length );
4677 else
Janos Follath315b51c2018-07-09 16:04:51 +01004678 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004679
Steven Cooremanef8575e2020-09-11 11:44:50 +02004680exit:
4681 if( operation->mbedtls_in_use == 1 )
4682 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004683
Steven Cooremanef8575e2020-09-11 11:44:50 +02004684 if( status == PSA_SUCCESS )
4685 return( psa_cipher_abort( operation ) );
4686 else
4687 {
4688 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004689 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004690
Steven Cooremanef8575e2020-09-11 11:44:50 +02004691 return( status );
4692 }
mohammad1603503973b2018-03-12 15:59:30 +02004693}
4694
Gilles Peskinee553c652018-06-04 16:22:46 +02004695psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4696{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004697 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004698 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004699 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004700 * in use. It's ok to call abort on such an object, and there's
4701 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004702 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004703 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004704
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004705 /* Sanity check (shouldn't happen: operation->alg should
4706 * always have been initialized to a valid value). */
4707 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4708 return( PSA_ERROR_BAD_STATE );
4709
Steven Cooremanef8575e2020-09-11 11:44:50 +02004710 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004711 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004712 else
4713 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004714
Moran Peker41deec42018-04-04 15:43:05 +03004715 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004716 operation->key_set = 0;
4717 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004718 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004719 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004720 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004721 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004722
Moran Peker395db872018-05-31 14:07:14 +03004723 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004724}
4725
Gilles Peskinea0655c32018-04-30 17:06:50 +02004726
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004727
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004728
mohammad16035955c982018-04-26 00:53:03 +03004729/****************************************************************/
4730/* AEAD */
4731/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004732
Gilles Peskineedf9a652018-08-17 18:11:56 +02004733typedef struct
4734{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004735 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004736 const mbedtls_cipher_info_t *cipher_info;
4737 union
4738 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004739 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004740#if defined(MBEDTLS_CCM_C)
4741 mbedtls_ccm_context ccm;
4742#endif /* MBEDTLS_CCM_C */
4743#if defined(MBEDTLS_GCM_C)
4744 mbedtls_gcm_context gcm;
4745#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004746#if defined(MBEDTLS_CHACHAPOLY_C)
4747 mbedtls_chachapoly_context chachapoly;
4748#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004749 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004750 psa_algorithm_t core_alg;
4751 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004752 uint8_t tag_length;
4753} aead_operation_t;
4754
Ronald Cronf95a2b12020-10-22 15:24:49 +02004755#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4756
Gilles Peskine30a9e412019-01-14 18:36:12 +01004757static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004758{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004759 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004760 {
4761#if defined(MBEDTLS_CCM_C)
4762 case PSA_ALG_CCM:
4763 mbedtls_ccm_free( &operation->ctx.ccm );
4764 break;
4765#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004766#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004767 case PSA_ALG_GCM:
4768 mbedtls_gcm_free( &operation->ctx.gcm );
4769 break;
4770#endif /* MBEDTLS_GCM_C */
4771 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004772
Ronald Cron5c522922020-11-14 16:35:34 +01004773 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004774}
4775
4776static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004777 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004778 psa_key_usage_t usage,
4779 psa_algorithm_t alg )
4780{
4781 psa_status_t status;
4782 size_t key_bits;
4783 mbedtls_cipher_id_t cipher_id;
4784
Ronald Cron5c522922020-11-14 16:35:34 +01004785 status = psa_get_and_lock_transparent_key_slot_with_policy(
4786 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004787 if( status != PSA_SUCCESS )
4788 return( status );
4789
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004790 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004791
4792 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004793 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004794 &cipher_id );
4795 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004796 {
4797 status = PSA_ERROR_NOT_SUPPORTED;
4798 goto cleanup;
4799 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004800
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004801 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004802 {
4803#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004804 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4805 operation->core_alg = PSA_ALG_CCM;
4806 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004807 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4808 * The call to mbedtls_ccm_encrypt_and_tag or
4809 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004810 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004811 {
4812 status = PSA_ERROR_INVALID_ARGUMENT;
4813 goto cleanup;
4814 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004815 mbedtls_ccm_init( &operation->ctx.ccm );
4816 status = mbedtls_to_psa_error(
4817 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004818 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004819 (unsigned int) key_bits ) );
4820 if( status != 0 )
4821 goto cleanup;
4822 break;
4823#endif /* MBEDTLS_CCM_C */
4824
4825#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004826 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4827 operation->core_alg = PSA_ALG_GCM;
4828 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004829 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4830 * The call to mbedtls_gcm_crypt_and_tag or
4831 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004832 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004833 {
4834 status = PSA_ERROR_INVALID_ARGUMENT;
4835 goto cleanup;
4836 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004837 mbedtls_gcm_init( &operation->ctx.gcm );
4838 status = mbedtls_to_psa_error(
4839 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004840 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004841 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004842 if( status != 0 )
4843 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004844 break;
4845#endif /* MBEDTLS_GCM_C */
4846
Gilles Peskine26869f22019-05-06 15:25:00 +02004847#if defined(MBEDTLS_CHACHAPOLY_C)
4848 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4849 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4850 operation->full_tag_length = 16;
4851 /* We only support the default tag length. */
4852 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004853 {
4854 status = PSA_ERROR_NOT_SUPPORTED;
4855 goto cleanup;
4856 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004857 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4858 status = mbedtls_to_psa_error(
4859 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004860 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004861 if( status != 0 )
4862 goto cleanup;
4863 break;
4864#endif /* MBEDTLS_CHACHAPOLY_C */
4865
Gilles Peskineedf9a652018-08-17 18:11:56 +02004866 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004867 status = PSA_ERROR_NOT_SUPPORTED;
4868 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004869 }
4870
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004871 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4872 {
4873 status = PSA_ERROR_INVALID_ARGUMENT;
4874 goto cleanup;
4875 }
4876 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004877
Gilles Peskineedf9a652018-08-17 18:11:56 +02004878 return( PSA_SUCCESS );
4879
4880cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004881 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004882 return( status );
4883}
4884
Ronald Croncf56a0a2020-08-04 09:51:30 +02004885psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004886 psa_algorithm_t alg,
4887 const uint8_t *nonce,
4888 size_t nonce_length,
4889 const uint8_t *additional_data,
4890 size_t additional_data_length,
4891 const uint8_t *plaintext,
4892 size_t plaintext_length,
4893 uint8_t *ciphertext,
4894 size_t ciphertext_size,
4895 size_t *ciphertext_length )
4896{
mohammad16035955c982018-04-26 00:53:03 +03004897 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004898 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004899 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004900
mohammad1603f08a5502018-06-03 15:05:47 +03004901 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004902
Ronald Croncf56a0a2020-08-04 09:51:30 +02004903 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004904 if( status != PSA_SUCCESS )
4905 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004906
Gilles Peskineedf9a652018-08-17 18:11:56 +02004907 /* For all currently supported modes, the tag is at the end of the
4908 * ciphertext. */
4909 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4910 {
4911 status = PSA_ERROR_BUFFER_TOO_SMALL;
4912 goto exit;
4913 }
4914 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004915
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004916#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004917 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004918 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004919 status = mbedtls_to_psa_error(
4920 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4921 MBEDTLS_GCM_ENCRYPT,
4922 plaintext_length,
4923 nonce, nonce_length,
4924 additional_data, additional_data_length,
4925 plaintext, ciphertext,
4926 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004927 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004928 else
4929#endif /* MBEDTLS_GCM_C */
4930#if defined(MBEDTLS_CCM_C)
4931 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004932 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004933 status = mbedtls_to_psa_error(
4934 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4935 plaintext_length,
4936 nonce, nonce_length,
4937 additional_data,
4938 additional_data_length,
4939 plaintext, ciphertext,
4940 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004941 }
mohammad16035c8845f2018-05-09 05:40:09 -07004942 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004943#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004944#if defined(MBEDTLS_CHACHAPOLY_C)
4945 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4946 {
4947 if( nonce_length != 12 || operation.tag_length != 16 )
4948 {
4949 status = PSA_ERROR_NOT_SUPPORTED;
4950 goto exit;
4951 }
4952 status = mbedtls_to_psa_error(
4953 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4954 plaintext_length,
4955 nonce,
4956 additional_data,
4957 additional_data_length,
4958 plaintext,
4959 ciphertext,
4960 tag ) );
4961 }
4962 else
4963#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004964 {
mohammad1603554faad2018-06-03 15:07:38 +03004965 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004966 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004967
Gilles Peskineedf9a652018-08-17 18:11:56 +02004968 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4969 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004970
Gilles Peskineedf9a652018-08-17 18:11:56 +02004971exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004972 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004973 if( status == PSA_SUCCESS )
4974 *ciphertext_length = plaintext_length + operation.tag_length;
4975 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004976}
4977
Gilles Peskineee652a32018-06-01 19:23:52 +02004978/* Locate the tag in a ciphertext buffer containing the encrypted data
4979 * followed by the tag. Return the length of the part preceding the tag in
4980 * *plaintext_length. This is the size of the plaintext in modes where
4981 * the encrypted data has the same size as the plaintext, such as
4982 * CCM and GCM. */
4983static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4984 const uint8_t *ciphertext,
4985 size_t ciphertext_length,
4986 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004987 const uint8_t **p_tag )
4988{
4989 size_t payload_length;
4990 if( tag_length > ciphertext_length )
4991 return( PSA_ERROR_INVALID_ARGUMENT );
4992 payload_length = ciphertext_length - tag_length;
4993 if( payload_length > plaintext_size )
4994 return( PSA_ERROR_BUFFER_TOO_SMALL );
4995 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004996 return( PSA_SUCCESS );
4997}
4998
Ronald Croncf56a0a2020-08-04 09:51:30 +02004999psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005000 psa_algorithm_t alg,
5001 const uint8_t *nonce,
5002 size_t nonce_length,
5003 const uint8_t *additional_data,
5004 size_t additional_data_length,
5005 const uint8_t *ciphertext,
5006 size_t ciphertext_length,
5007 uint8_t *plaintext,
5008 size_t plaintext_size,
5009 size_t *plaintext_length )
5010{
mohammad16035955c982018-04-26 00:53:03 +03005011 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005012 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005013 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005014
Gilles Peskineee652a32018-06-01 19:23:52 +02005015 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005016
Ronald Croncf56a0a2020-08-04 09:51:30 +02005017 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005018 if( status != PSA_SUCCESS )
5019 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005020
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005021 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5022 ciphertext, ciphertext_length,
5023 plaintext_size, &tag );
5024 if( status != PSA_SUCCESS )
5025 goto exit;
5026
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005027#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005028 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005029 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005030 status = mbedtls_to_psa_error(
5031 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5032 ciphertext_length - operation.tag_length,
5033 nonce, nonce_length,
5034 additional_data,
5035 additional_data_length,
5036 tag, operation.tag_length,
5037 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005038 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005039 else
5040#endif /* MBEDTLS_GCM_C */
5041#if defined(MBEDTLS_CCM_C)
5042 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005043 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005044 status = mbedtls_to_psa_error(
5045 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5046 ciphertext_length - operation.tag_length,
5047 nonce, nonce_length,
5048 additional_data,
5049 additional_data_length,
5050 ciphertext, plaintext,
5051 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005052 }
mohammad160339574652018-06-01 04:39:53 -07005053 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005054#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005055#if defined(MBEDTLS_CHACHAPOLY_C)
5056 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5057 {
5058 if( nonce_length != 12 || operation.tag_length != 16 )
5059 {
5060 status = PSA_ERROR_NOT_SUPPORTED;
5061 goto exit;
5062 }
5063 status = mbedtls_to_psa_error(
5064 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5065 ciphertext_length - operation.tag_length,
5066 nonce,
5067 additional_data,
5068 additional_data_length,
5069 tag,
5070 ciphertext,
5071 plaintext ) );
5072 }
5073 else
5074#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005075 {
mohammad1603554faad2018-06-03 15:07:38 +03005076 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005077 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005078
Gilles Peskineedf9a652018-08-17 18:11:56 +02005079 if( status != PSA_SUCCESS && plaintext_size != 0 )
5080 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005081
Gilles Peskineedf9a652018-08-17 18:11:56 +02005082exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005083 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005084 if( status == PSA_SUCCESS )
5085 *plaintext_length = ciphertext_length - operation.tag_length;
5086 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005087}
5088
Gilles Peskinea0655c32018-04-30 17:06:50 +02005089
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005090
Gilles Peskine20035e32018-02-03 22:44:14 +01005091/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005092/* Generators */
5093/****************************************************************/
5094
John Durkop07cc04a2020-11-16 22:08:34 -08005095#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5096 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5097 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5098#define AT_LEAST_ONE_BUILTIN_KDF
5099#endif
5100
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005101#define HKDF_STATE_INIT 0 /* no input yet */
5102#define HKDF_STATE_STARTED 1 /* got salt */
5103#define HKDF_STATE_KEYED 2 /* got key */
5104#define HKDF_STATE_OUTPUT 3 /* output started */
5105
Gilles Peskinecbe66502019-05-16 16:59:18 +02005106static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005107 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005108{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005109 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5110 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005111 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005112 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005113}
5114
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005116{
5117 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005118 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005119 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005120 {
5121 /* The object has (apparently) been initialized but it is not
5122 * in use. It's ok to call abort on such an object, and there's
5123 * nothing to do. */
5124 }
5125 else
John Durkopd0321952020-10-29 21:37:36 -07005126#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005127 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005128 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005129 mbedtls_free( operation->ctx.hkdf.info );
5130 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005131 }
John Durkop07cc04a2020-11-16 22:08:34 -08005132 else
5133#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5134#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5135 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5136 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005137 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005138 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005139 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005140 if( operation->ctx.tls12_prf.seed != NULL )
5141 {
5142 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5143 operation->ctx.tls12_prf.seed_length );
5144 mbedtls_free( operation->ctx.tls12_prf.seed );
5145 }
5146
5147 if( operation->ctx.tls12_prf.label != NULL )
5148 {
5149 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5150 operation->ctx.tls12_prf.label_length );
5151 mbedtls_free( operation->ctx.tls12_prf.label );
5152 }
5153
5154 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5155
5156 /* We leave the fields Ai and output_block to be erased safely by the
5157 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005158 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005159 else
John Durkop07cc04a2020-11-16 22:08:34 -08005160#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5161 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005162 {
5163 status = PSA_ERROR_BAD_STATE;
5164 }
Janos Follath083036a2019-06-11 10:22:26 +01005165 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005166 return( status );
5167}
5168
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005169psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005170 size_t *capacity)
5171{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005172 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005173 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005175 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005176 }
5177
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005178 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005179 return( PSA_SUCCESS );
5180}
5181
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005182psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005183 size_t capacity )
5184{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005185 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005186 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005187 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005188 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005189 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005190 return( PSA_SUCCESS );
5191}
5192
John Durkopd0321952020-10-29 21:37:36 -07005193#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005194/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005195 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005196static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005197 psa_algorithm_t hash_alg,
5198 uint8_t *output,
5199 size_t output_length )
5200{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005201 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005202 psa_status_t status;
5203
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005204 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5205 return( PSA_ERROR_BAD_STATE );
5206 hkdf->state = HKDF_STATE_OUTPUT;
5207
Gilles Peskinebef7f142018-07-12 17:22:21 +02005208 while( output_length != 0 )
5209 {
5210 /* Copy what remains of the current block */
5211 uint8_t n = hash_length - hkdf->offset_in_block;
5212 if( n > output_length )
5213 n = (uint8_t) output_length;
5214 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5215 output += n;
5216 output_length -= n;
5217 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005218 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005219 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005220 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005221 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005222 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005223 * object was corrupted or if this function is called directly
5224 * inside the library. */
5225 if( hkdf->block_number == 0xff )
5226 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005227
5228 /* We need a new block */
5229 ++hkdf->block_number;
5230 hkdf->offset_in_block = 0;
5231 status = psa_hmac_setup_internal( &hkdf->hmac,
5232 hkdf->prk, hash_length,
5233 hash_alg );
5234 if( status != PSA_SUCCESS )
5235 return( status );
5236 if( hkdf->block_number != 1 )
5237 {
5238 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5239 hkdf->output_block,
5240 hash_length );
5241 if( status != PSA_SUCCESS )
5242 return( status );
5243 }
5244 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5245 hkdf->info,
5246 hkdf->info_length );
5247 if( status != PSA_SUCCESS )
5248 return( status );
5249 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5250 &hkdf->block_number, 1 );
5251 if( status != PSA_SUCCESS )
5252 return( status );
5253 status = psa_hmac_finish_internal( &hkdf->hmac,
5254 hkdf->output_block,
5255 sizeof( hkdf->output_block ) );
5256 if( status != PSA_SUCCESS )
5257 return( status );
5258 }
5259
5260 return( PSA_SUCCESS );
5261}
John Durkop07cc04a2020-11-16 22:08:34 -08005262#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005263
John Durkop07cc04a2020-11-16 22:08:34 -08005264#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5265 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005266static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5267 psa_tls12_prf_key_derivation_t *tls12_prf,
5268 psa_algorithm_t alg )
5269{
5270 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005271 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005272 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5273 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005274
Janos Follath7742fee2019-06-17 12:58:10 +01005275 /* We can't be wanting more output after block 0xff, otherwise
5276 * the capacity check in psa_key_derivation_output_bytes() would have
5277 * prevented this call. It could happen only if the operation
5278 * object was corrupted or if this function is called directly
5279 * inside the library. */
5280 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005281 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005282
5283 /* We need a new block */
5284 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005285 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005286
5287 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5288 *
5289 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5290 *
5291 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5292 * HMAC_hash(secret, A(2) + seed) +
5293 * HMAC_hash(secret, A(3) + seed) + ...
5294 *
5295 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005296 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005297 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005298 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005299 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005300 * is currently extracted as `output_block` and where i is
5301 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005302 */
5303
Janos Follathea29bfb2019-06-19 12:21:20 +01005304 /* Save the hash context before using it, to preserve the hash state with
5305 * only the inner padding in it. We need this, because inner padding depends
5306 * on the key (secret in the RFC's terminology). */
5307 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5308 if( status != PSA_SUCCESS )
5309 goto cleanup;
5310
5311 /* Calculate A(i) where i = tls12_prf->block_number. */
5312 if( tls12_prf->block_number == 1 )
5313 {
5314 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5315 * the variable seed and in this instance means it in the context of the
5316 * P_hash function, where seed = label + seed.) */
5317 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5318 tls12_prf->label, tls12_prf->label_length );
5319 if( status != PSA_SUCCESS )
5320 goto cleanup;
5321 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5322 tls12_prf->seed, tls12_prf->seed_length );
5323 if( status != PSA_SUCCESS )
5324 goto cleanup;
5325 }
5326 else
5327 {
5328 /* A(i) = HMAC_hash(secret, A(i-1)) */
5329 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5330 tls12_prf->Ai, hash_length );
5331 if( status != PSA_SUCCESS )
5332 goto cleanup;
5333 }
5334
5335 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5336 tls12_prf->Ai, hash_length );
5337 if( status != PSA_SUCCESS )
5338 goto cleanup;
5339 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5340 if( status != PSA_SUCCESS )
5341 goto cleanup;
5342
5343 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5344 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5345 tls12_prf->Ai, hash_length );
5346 if( status != PSA_SUCCESS )
5347 goto cleanup;
5348 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5349 tls12_prf->label, tls12_prf->label_length );
5350 if( status != PSA_SUCCESS )
5351 goto cleanup;
5352 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5353 tls12_prf->seed, tls12_prf->seed_length );
5354 if( status != PSA_SUCCESS )
5355 goto cleanup;
5356 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5357 tls12_prf->output_block, hash_length );
5358 if( status != PSA_SUCCESS )
5359 goto cleanup;
5360 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5361 if( status != PSA_SUCCESS )
5362 goto cleanup;
5363
Janos Follath7742fee2019-06-17 12:58:10 +01005364
5365cleanup:
5366
Janos Follathea29bfb2019-06-19 12:21:20 +01005367 cleanup_status = psa_hash_abort( &backup );
5368 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5369 status = cleanup_status;
5370
5371 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005372}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005373
Janos Follath844eb0e2019-06-19 12:10:49 +01005374static psa_status_t psa_key_derivation_tls12_prf_read(
5375 psa_tls12_prf_key_derivation_t *tls12_prf,
5376 psa_algorithm_t alg,
5377 uint8_t *output,
5378 size_t output_length )
5379{
5380 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005381 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005382 psa_status_t status;
5383 uint8_t offset, length;
5384
5385 while( output_length != 0 )
5386 {
5387 /* Check if we have fully processed the current block. */
5388 if( tls12_prf->left_in_block == 0 )
5389 {
5390 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5391 alg );
5392 if( status != PSA_SUCCESS )
5393 return( status );
5394
5395 continue;
5396 }
5397
5398 if( tls12_prf->left_in_block > output_length )
5399 length = (uint8_t) output_length;
5400 else
5401 length = tls12_prf->left_in_block;
5402
5403 offset = hash_length - tls12_prf->left_in_block;
5404 memcpy( output, tls12_prf->output_block + offset, length );
5405 output += length;
5406 output_length -= length;
5407 tls12_prf->left_in_block -= length;
5408 }
5409
5410 return( PSA_SUCCESS );
5411}
John Durkop07cc04a2020-11-16 22:08:34 -08005412#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5413 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005414
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005415psa_status_t psa_key_derivation_output_bytes(
5416 psa_key_derivation_operation_t *operation,
5417 uint8_t *output,
5418 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005419{
5420 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005421 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005422
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005423 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005424 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005425 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005426 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005427 }
5428
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005429 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005430 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005431 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005432 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005434 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005435 goto exit;
5436 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005438 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005440 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005441 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5442 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005443 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005444 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005445 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005446 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005447 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005448
John Durkopd0321952020-10-29 21:37:36 -07005449#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005450 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005451 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005452 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005453 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005454 output, output_length );
5455 }
Janos Follathadbec812019-06-14 11:05:39 +01005456 else
John Durkop07cc04a2020-11-16 22:08:34 -08005457#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5458#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5459 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005460 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005461 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005462 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005463 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005464 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005465 output_length );
5466 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005467 else
John Durkop07cc04a2020-11-16 22:08:34 -08005468#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5469 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005470 {
5471 return( PSA_ERROR_BAD_STATE );
5472 }
5473
5474exit:
5475 if( status != PSA_SUCCESS )
5476 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005477 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005478 * This allows us to differentiate between exhausted operations and
5479 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5480 * operations. */
5481 psa_algorithm_t alg = operation->alg;
5482 psa_key_derivation_abort( operation );
5483 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005484 memset( output, '!', output_length );
5485 }
5486 return( status );
5487}
5488
Gilles Peskine08542d82018-07-19 17:05:42 +02005489#if defined(MBEDTLS_DES_C)
5490static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5491{
5492 if( data_size >= 8 )
5493 mbedtls_des_key_set_parity( data );
5494 if( data_size >= 16 )
5495 mbedtls_des_key_set_parity( data + 8 );
5496 if( data_size >= 24 )
5497 mbedtls_des_key_set_parity( data + 16 );
5498}
5499#endif /* MBEDTLS_DES_C */
5500
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005501static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005502 psa_key_slot_t *slot,
5503 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005505{
5506 uint8_t *data = NULL;
5507 size_t bytes = PSA_BITS_TO_BYTES( bits );
5508 psa_status_t status;
5509
Gilles Peskine8e338702019-07-30 20:06:31 +02005510 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005511 return( PSA_ERROR_INVALID_ARGUMENT );
5512 if( bits % 8 != 0 )
5513 return( PSA_ERROR_INVALID_ARGUMENT );
5514 data = mbedtls_calloc( 1, bytes );
5515 if( data == NULL )
5516 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5517
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005518 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005519 if( status != PSA_SUCCESS )
5520 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005521#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005522 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005523 psa_des_set_key_parity( data, bytes );
5524#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005525 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005526
5527exit:
5528 mbedtls_free( data );
5529 return( status );
5530}
5531
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005532psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005533 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005534 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005535{
5536 psa_status_t status;
5537 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005538 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005539
Ronald Cron81709fc2020-11-14 12:10:32 +01005540 *key = MBEDTLS_SVC_KEY_ID_INIT;
5541
Gilles Peskine0f84d622019-09-12 19:03:13 +02005542 /* Reject any attempt to create a zero-length key so that we don't
5543 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5544 if( psa_get_key_bits( attributes ) == 0 )
5545 return( PSA_ERROR_INVALID_ARGUMENT );
5546
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005547 if( ! operation->can_output_key )
5548 return( PSA_ERROR_NOT_PERMITTED );
5549
Ronald Cron81709fc2020-11-14 12:10:32 +01005550 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5551 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005552#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5553 if( driver != NULL )
5554 {
5555 /* Deriving a key in a secure element is not implemented yet. */
5556 status = PSA_ERROR_NOT_SUPPORTED;
5557 }
5558#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005559 if( status == PSA_SUCCESS )
5560 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005561 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005562 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005563 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005564 }
5565 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005566 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005567 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005568 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005569
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005570 return( status );
5571}
5572
Gilles Peskineeab56e42018-07-12 17:12:33 +02005573
5574
5575/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005576/* Key derivation */
5577/****************************************************************/
5578
John Durkop07cc04a2020-11-16 22:08:34 -08005579#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005580static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005581 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005582 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005583{
John Durkop07cc04a2020-11-16 22:08:34 -08005584 int is_kdf_alg_supported;
5585
Janos Follath5fe19732019-06-20 15:09:30 +01005586 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5587 * initialisers for this union leave some bytes unspecified.) */
5588 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5589
Gilles Peskine969c5d62019-01-16 15:53:06 +01005590 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005591#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005592 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5593 is_kdf_alg_supported = 1;
5594 else
5595#endif
5596#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5597 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5598 is_kdf_alg_supported = 1;
5599 else
5600#endif
5601#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5602 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5603 is_kdf_alg_supported = 1;
5604 else
5605#endif
5606 is_kdf_alg_supported = 0;
5607
5608 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005609 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005610 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005611 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005612 if( hash_size == 0 )
5613 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005614 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5615 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005616 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005617 {
5618 return( PSA_ERROR_NOT_SUPPORTED );
5619 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005620 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005621 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005622 }
John Durkop07cc04a2020-11-16 22:08:34 -08005623
5624 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005625}
John Durkop07cc04a2020-11-16 22:08:34 -08005626#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005627
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005628psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005629 psa_algorithm_t alg )
5630{
5631 psa_status_t status;
5632
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005633 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005634 return( PSA_ERROR_BAD_STATE );
5635
Gilles Peskine6843c292019-01-18 16:44:49 +01005636 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5637 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005638#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005639 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005640 {
5641 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005642 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005643 }
5644 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5645 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005646 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005647 }
John Durkop07cc04a2020-11-16 22:08:34 -08005648#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005649 else
5650 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005651
5652 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005653 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005654 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005655}
5656
John Durkopd0321952020-10-29 21:37:36 -07005657#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005658static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005659 psa_algorithm_t hash_alg,
5660 psa_key_derivation_step_t step,
5661 const uint8_t *data,
5662 size_t data_length )
5663{
5664 psa_status_t status;
5665 switch( step )
5666 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005667 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005668 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005669 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005670 status = psa_hmac_setup_internal( &hkdf->hmac,
5671 data, data_length,
5672 hash_alg );
5673 if( status != PSA_SUCCESS )
5674 return( status );
5675 hkdf->state = HKDF_STATE_STARTED;
5676 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005677 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005678 /* If no salt was provided, use an empty salt. */
5679 if( hkdf->state == HKDF_STATE_INIT )
5680 {
5681 status = psa_hmac_setup_internal( &hkdf->hmac,
5682 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005683 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005684 if( status != PSA_SUCCESS )
5685 return( status );
5686 hkdf->state = HKDF_STATE_STARTED;
5687 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005688 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005689 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005690 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5691 data, data_length );
5692 if( status != PSA_SUCCESS )
5693 return( status );
5694 status = psa_hmac_finish_internal( &hkdf->hmac,
5695 hkdf->prk,
5696 sizeof( hkdf->prk ) );
5697 if( status != PSA_SUCCESS )
5698 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005699 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005700 hkdf->block_number = 0;
5701 hkdf->state = HKDF_STATE_KEYED;
5702 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005703 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005704 if( hkdf->state == HKDF_STATE_OUTPUT )
5705 return( PSA_ERROR_BAD_STATE );
5706 if( hkdf->info_set )
5707 return( PSA_ERROR_BAD_STATE );
5708 hkdf->info_length = data_length;
5709 if( data_length != 0 )
5710 {
5711 hkdf->info = mbedtls_calloc( 1, data_length );
5712 if( hkdf->info == NULL )
5713 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5714 memcpy( hkdf->info, data, data_length );
5715 }
5716 hkdf->info_set = 1;
5717 return( PSA_SUCCESS );
5718 default:
5719 return( PSA_ERROR_INVALID_ARGUMENT );
5720 }
5721}
John Durkop07cc04a2020-11-16 22:08:34 -08005722#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005723
John Durkop07cc04a2020-11-16 22:08:34 -08005724#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5725 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005726static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5727 const uint8_t *data,
5728 size_t data_length )
5729{
5730 if( prf->state != TLS12_PRF_STATE_INIT )
5731 return( PSA_ERROR_BAD_STATE );
5732
Janos Follathd6dce9f2019-07-04 09:11:38 +01005733 if( data_length != 0 )
5734 {
5735 prf->seed = mbedtls_calloc( 1, data_length );
5736 if( prf->seed == NULL )
5737 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005738
Janos Follathd6dce9f2019-07-04 09:11:38 +01005739 memcpy( prf->seed, data, data_length );
5740 prf->seed_length = data_length;
5741 }
Janos Follathf08e2652019-06-13 09:05:41 +01005742
5743 prf->state = TLS12_PRF_STATE_SEED_SET;
5744
5745 return( PSA_SUCCESS );
5746}
5747
Janos Follath81550542019-06-13 14:26:34 +01005748static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5749 psa_algorithm_t hash_alg,
5750 const uint8_t *data,
5751 size_t data_length )
5752{
5753 psa_status_t status;
5754 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5755 return( PSA_ERROR_BAD_STATE );
5756
5757 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5758 if( status != PSA_SUCCESS )
5759 return( status );
5760
5761 prf->state = TLS12_PRF_STATE_KEY_SET;
5762
5763 return( PSA_SUCCESS );
5764}
5765
Janos Follath63028dd2019-06-13 09:15:47 +01005766static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5767 const uint8_t *data,
5768 size_t data_length )
5769{
5770 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5771 return( PSA_ERROR_BAD_STATE );
5772
Janos Follathd6dce9f2019-07-04 09:11:38 +01005773 if( data_length != 0 )
5774 {
5775 prf->label = mbedtls_calloc( 1, data_length );
5776 if( prf->label == NULL )
5777 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005778
Janos Follathd6dce9f2019-07-04 09:11:38 +01005779 memcpy( prf->label, data, data_length );
5780 prf->label_length = data_length;
5781 }
Janos Follath63028dd2019-06-13 09:15:47 +01005782
5783 prf->state = TLS12_PRF_STATE_LABEL_SET;
5784
5785 return( PSA_SUCCESS );
5786}
5787
Janos Follathb03233e2019-06-11 15:30:30 +01005788static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5789 psa_algorithm_t hash_alg,
5790 psa_key_derivation_step_t step,
5791 const uint8_t *data,
5792 size_t data_length )
5793{
Janos Follathb03233e2019-06-11 15:30:30 +01005794 switch( step )
5795 {
Janos Follathf08e2652019-06-13 09:05:41 +01005796 case PSA_KEY_DERIVATION_INPUT_SEED:
5797 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005798 case PSA_KEY_DERIVATION_INPUT_SECRET:
5799 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005800 case PSA_KEY_DERIVATION_INPUT_LABEL:
5801 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005802 default:
5803 return( PSA_ERROR_INVALID_ARGUMENT );
5804 }
5805}
John Durkop07cc04a2020-11-16 22:08:34 -08005806#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5807 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5808
5809#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5810static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5811 psa_tls12_prf_key_derivation_t *prf,
5812 psa_algorithm_t hash_alg,
5813 const uint8_t *data,
5814 size_t data_length )
5815{
5816 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005817 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005818 uint8_t *cur = pms;
5819
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005820 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005821 return( PSA_ERROR_INVALID_ARGUMENT );
5822
5823 /* Quoting RFC 4279, Section 2:
5824 *
5825 * The premaster secret is formed as follows: if the PSK is N octets
5826 * long, concatenate a uint16 with the value N, N zero octets, a second
5827 * uint16 with the value N, and the PSK itself.
5828 */
5829
5830 *cur++ = ( data_length >> 8 ) & 0xff;
5831 *cur++ = ( data_length >> 0 ) & 0xff;
5832 memset( cur, 0, data_length );
5833 cur += data_length;
5834 *cur++ = pms[0];
5835 *cur++ = pms[1];
5836 memcpy( cur, data, data_length );
5837 cur += data_length;
5838
5839 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5840
5841 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5842 return( status );
5843}
Janos Follath6660f0e2019-06-17 08:44:03 +01005844
5845static psa_status_t psa_tls12_prf_psk_to_ms_input(
5846 psa_tls12_prf_key_derivation_t *prf,
5847 psa_algorithm_t hash_alg,
5848 psa_key_derivation_step_t step,
5849 const uint8_t *data,
5850 size_t data_length )
5851{
5852 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005853 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005854 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5855 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005856 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005857
5858 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5859}
John Durkop07cc04a2020-11-16 22:08:34 -08005860#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005861
Gilles Peskineb8965192019-09-24 16:21:10 +02005862/** Check whether the given key type is acceptable for the given
5863 * input step of a key derivation.
5864 *
5865 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5866 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5867 * Both secret and non-secret inputs can alternatively have the type
5868 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5869 * that the input was passed as a buffer rather than via a key object.
5870 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005871static int psa_key_derivation_check_input_type(
5872 psa_key_derivation_step_t step,
5873 psa_key_type_t key_type )
5874{
5875 switch( step )
5876 {
5877 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005878 if( key_type == PSA_KEY_TYPE_DERIVE )
5879 return( PSA_SUCCESS );
5880 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005881 return( PSA_SUCCESS );
5882 break;
5883 case PSA_KEY_DERIVATION_INPUT_LABEL:
5884 case PSA_KEY_DERIVATION_INPUT_SALT:
5885 case PSA_KEY_DERIVATION_INPUT_INFO:
5886 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005887 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5888 return( PSA_SUCCESS );
5889 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005890 return( PSA_SUCCESS );
5891 break;
5892 }
5893 return( PSA_ERROR_INVALID_ARGUMENT );
5894}
5895
Janos Follathb80a94e2019-06-12 15:54:46 +01005896static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005897 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005898 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005899 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005900 const uint8_t *data,
5901 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005902{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005903 psa_status_t status;
5904 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5905
5906 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005907 if( status != PSA_SUCCESS )
5908 goto exit;
5909
John Durkopd0321952020-10-29 21:37:36 -07005910#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005911 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005912 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005913 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005914 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005915 step, data, data_length );
5916 }
John Durkop07cc04a2020-11-16 22:08:34 -08005917 else
5918#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5919#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5920 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005921 {
Janos Follathb03233e2019-06-11 15:30:30 +01005922 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5923 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5924 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005925 }
John Durkop07cc04a2020-11-16 22:08:34 -08005926 else
5927#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5928#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5929 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005930 {
5931 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5932 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5933 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005934 }
5935 else
John Durkop07cc04a2020-11-16 22:08:34 -08005936#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005937 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005938 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005939 return( PSA_ERROR_BAD_STATE );
5940 }
5941
Gilles Peskine224b0d62019-09-23 18:13:17 +02005942exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005943 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005944 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005945 return( status );
5946}
5947
Janos Follath51f4a0f2019-06-14 11:35:55 +01005948psa_status_t psa_key_derivation_input_bytes(
5949 psa_key_derivation_operation_t *operation,
5950 psa_key_derivation_step_t step,
5951 const uint8_t *data,
5952 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005953{
Gilles Peskineb8965192019-09-24 16:21:10 +02005954 return( psa_key_derivation_input_internal( operation, step,
5955 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005956 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005957}
5958
Janos Follath51f4a0f2019-06-14 11:35:55 +01005959psa_status_t psa_key_derivation_input_key(
5960 psa_key_derivation_operation_t *operation,
5961 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005962 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005963{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005965 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005966 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005967
Ronald Cron5c522922020-11-14 16:35:34 +01005968 status = psa_get_and_lock_transparent_key_slot_with_policy(
5969 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005970 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005971 {
5972 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005973 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005974 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005975
5976 /* Passing a key object as a SECRET input unlocks the permission
5977 * to output to a key object. */
5978 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5979 operation->can_output_key = 1;
5980
Ronald Cronf95a2b12020-10-22 15:24:49 +02005981 status = psa_key_derivation_input_internal( operation,
5982 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005983 slot->key.data,
5984 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005985
Ronald Cron5c522922020-11-14 16:35:34 +01005986 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005987
Ronald Cron5c522922020-11-14 16:35:34 +01005988 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005989}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005990
5991
5992
5993/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005994/* Key agreement */
5995/****************************************************************/
5996
John Durkop6ba40d12020-11-10 08:50:04 -08005997#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005998static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5999 size_t peer_key_length,
6000 const mbedtls_ecp_keypair *our_key,
6001 uint8_t *shared_secret,
6002 size_t shared_secret_size,
6003 size_t *shared_secret_length )
6004{
Steven Cooremand4867872020-08-05 16:31:39 +02006005 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006006 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006007 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006008 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006009 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006010 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006011
Ronald Cron90857082020-11-25 14:58:33 +01006012 status = mbedtls_psa_ecp_load_representation(
6013 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6014 peer_key,
6015 peer_key_length,
6016 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006017 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006018 goto exit;
6019
Jaeden Amero97271b32019-01-10 19:38:51 +00006020 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006021 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006022 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006023 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006024 status = mbedtls_to_psa_error(
6025 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6026 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006027 goto exit;
6028
Jaeden Amero97271b32019-01-10 19:38:51 +00006029 status = mbedtls_to_psa_error(
6030 mbedtls_ecdh_calc_secret( &ecdh,
6031 shared_secret_length,
6032 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006033 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006034 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006035 if( status != PSA_SUCCESS )
6036 goto exit;
6037 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6038 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006039
6040exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006041 if( status != PSA_SUCCESS )
6042 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006043 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006044 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006045 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006046
Jaeden Amero97271b32019-01-10 19:38:51 +00006047 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006048}
John Durkop6ba40d12020-11-10 08:50:04 -08006049#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006050
Gilles Peskine01d718c2018-09-18 12:01:02 +02006051#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6052
Gilles Peskine0216fe12019-04-11 21:23:21 +02006053static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6054 psa_key_slot_t *private_key,
6055 const uint8_t *peer_key,
6056 size_t peer_key_length,
6057 uint8_t *shared_secret,
6058 size_t shared_secret_size,
6059 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006060{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006061 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006062 {
John Durkop6ba40d12020-11-10 08:50:04 -08006063#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006064 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006065 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006066 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006067 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01006068 psa_status_t status = mbedtls_psa_ecp_load_representation(
6069 private_key->attr.type,
6070 private_key->key.data,
6071 private_key->key.bytes,
6072 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006073 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006074 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006075 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006076 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006077 shared_secret, shared_secret_size,
6078 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006079 mbedtls_ecp_keypair_free( ecp );
6080 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006081 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006082#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006083 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006084 (void) private_key;
6085 (void) peer_key;
6086 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006087 (void) shared_secret;
6088 (void) shared_secret_size;
6089 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006090 return( PSA_ERROR_NOT_SUPPORTED );
6091 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006092}
6093
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006094/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006095 * to potentially free embedded data structures and wipe confidential data.
6096 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006097static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006098 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006099 psa_key_slot_t *private_key,
6100 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006101 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006102{
6103 psa_status_t status;
6104 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6105 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006106 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006107
6108 /* Step 1: run the secret agreement algorithm to generate the shared
6109 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006110 status = psa_key_agreement_raw_internal( ka_alg,
6111 private_key,
6112 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006113 shared_secret,
6114 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006115 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006116 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006117 goto exit;
6118
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006119 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006120 * the shared secret. A shared secret is permitted wherever a key
6121 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006122 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006123 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006124 shared_secret,
6125 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006126exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006127 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006128 return( status );
6129}
6130
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006131psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006132 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006133 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006134 const uint8_t *peer_key,
6135 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006136{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006138 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006139 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006140
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006141 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006142 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006143 status = psa_get_and_lock_transparent_key_slot_with_policy(
6144 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006145 if( status != PSA_SUCCESS )
6146 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006147 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006148 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006149 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006150 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006151 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006152 else
6153 {
6154 /* If a private key has been added as SECRET, we allow the derived
6155 * key material to be used as a key in PSA Crypto. */
6156 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6157 operation->can_output_key = 1;
6158 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006159
Ronald Cron5c522922020-11-14 16:35:34 +01006160 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006161
Ronald Cron5c522922020-11-14 16:35:34 +01006162 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006163}
6164
Gilles Peskinebe697d82019-05-16 18:00:41 +02006165psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006166 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006167 const uint8_t *peer_key,
6168 size_t peer_key_length,
6169 uint8_t *output,
6170 size_t output_size,
6171 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006172{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006174 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006175 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006176
6177 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6178 {
6179 status = PSA_ERROR_INVALID_ARGUMENT;
6180 goto exit;
6181 }
Ronald Cron5c522922020-11-14 16:35:34 +01006182 status = psa_get_and_lock_transparent_key_slot_with_policy(
6183 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006184 if( status != PSA_SUCCESS )
6185 goto exit;
6186
6187 status = psa_key_agreement_raw_internal( alg, slot,
6188 peer_key, peer_key_length,
6189 output, output_size,
6190 output_length );
6191
6192exit:
6193 if( status != PSA_SUCCESS )
6194 {
6195 /* If an error happens and is not handled properly, the output
6196 * may be used as a key to protect sensitive data. Arrange for such
6197 * a key to be random, which is likely to result in decryption or
6198 * verification errors. This is better than filling the buffer with
6199 * some constant data such as zeros, which would result in the data
6200 * being protected with a reproducible, easily knowable key.
6201 */
6202 psa_generate_random( output, output_size );
6203 *output_length = output_size;
6204 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006205
Ronald Cron5c522922020-11-14 16:35:34 +01006206 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006207
Ronald Cron5c522922020-11-14 16:35:34 +01006208 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006209}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006210
6211
Gilles Peskine30524eb2020-11-13 17:02:26 +01006212
Gilles Peskine86a440b2018-11-12 18:39:40 +01006213/****************************************************************/
6214/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006215/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006216
Gilles Peskine30524eb2020-11-13 17:02:26 +01006217/** Initialize the PSA random generator.
6218 */
6219static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6220{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006221#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6222 memset( rng, 0, sizeof( *rng ) );
6223#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6224
Gilles Peskine30524eb2020-11-13 17:02:26 +01006225 /* Set default configuration if
6226 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6227 if( rng->entropy_init == NULL )
6228 rng->entropy_init = mbedtls_entropy_init;
6229 if( rng->entropy_free == NULL )
6230 rng->entropy_free = mbedtls_entropy_free;
6231
6232 rng->entropy_init( &rng->entropy );
6233#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6234 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6235 /* The PSA entropy injection feature depends on using NV seed as an entropy
6236 * source. Add NV seed as an entropy source for PSA entropy injection. */
6237 mbedtls_entropy_add_source( &rng->entropy,
6238 mbedtls_nv_seed_poll, NULL,
6239 MBEDTLS_ENTROPY_BLOCK_SIZE,
6240 MBEDTLS_ENTROPY_SOURCE_STRONG );
6241#endif
6242
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006243 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006244#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006245}
6246
6247/** Deinitialize the PSA random generator.
6248 */
6249static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6250{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006251#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6252 memset( rng, 0, sizeof( *rng ) );
6253#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006254 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006255 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006256#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006257}
6258
6259/** Seed the PSA random generator.
6260 */
6261static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6262{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006263#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6264 /* Do nothing: the external RNG seeds itself. */
6265 (void) rng;
6266 return( PSA_SUCCESS );
6267#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006268 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006269 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6270 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006271 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006272#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006273}
6274
Gilles Peskinee59236f2018-01-27 23:32:46 +01006275psa_status_t psa_generate_random( uint8_t *output,
6276 size_t output_size )
6277{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006278 GUARD_MODULE_INITIALIZED;
6279
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006280#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6281
6282 size_t output_length = 0;
6283 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6284 output, output_size,
6285 &output_length );
6286 if( status != PSA_SUCCESS )
6287 return( status );
6288 /* Breaking up a request into smaller chunks is currently not supported
6289 * for the extrernal RNG interface. */
6290 if( output_length != output_size )
6291 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6292 return( PSA_SUCCESS );
6293
6294#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6295
Gilles Peskine71ddab92021-01-04 21:01:07 +01006296 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006297 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006298 size_t request_size =
6299 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6300 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6301 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006302 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6303 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006304 if( ret != 0 )
6305 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006306 output_size -= request_size;
6307 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006308 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006309 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006310#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006311}
6312
Gilles Peskine8814fc42020-12-14 15:33:44 +01006313/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006314 *
6315 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6316 * `psa_generate_random(...)`. The state parameter is ignored since the
6317 * PSA API doesn't support passing an explicit state.
6318 *
6319 * In the non-external case, psa_generate_random() calls an
6320 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6321 * and semantics as mbedtls_psa_get_random(). As an optimization,
6322 * instead of doing this back-and-forth between the PSA API and the
6323 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6324 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006325 */
6326#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6327int mbedtls_psa_get_random( void *p_rng,
6328 unsigned char *output,
6329 size_t output_size )
6330{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006331 /* This function takes a pointer to the RNG state because that's what
6332 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6333 * its own state internally and doesn't let the caller access that state.
6334 * So we just ignore the state parameter, and in practice we'll pass
6335 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006336 (void) p_rng;
6337 psa_status_t status = psa_generate_random( output, output_size );
6338 if( status == PSA_SUCCESS )
6339 return( 0 );
6340 else
6341 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6342}
6343#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6344
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006345#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6346#include "mbedtls/entropy_poll.h"
6347
Gilles Peskine7228da22019-07-15 11:06:15 +02006348psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006349 size_t seed_size )
6350{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006351 if( global_data.initialized )
6352 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006353
6354 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6355 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6356 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6357 return( PSA_ERROR_INVALID_ARGUMENT );
6358
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006359 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006360}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006361#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006362
John Durkop07cc04a2020-11-16 22:08:34 -08006363#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006364static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6365 size_t domain_parameters_size,
6366 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006367{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006368 size_t i;
6369 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006370
Gilles Peskinee56e8782019-04-26 17:34:02 +02006371 if( domain_parameters_size == 0 )
6372 {
6373 *exponent = 65537;
6374 return( PSA_SUCCESS );
6375 }
6376
6377 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6378 * support values that fit in a 32-bit integer, which is larger than
6379 * int on just about every platform anyway. */
6380 if( domain_parameters_size > sizeof( acc ) )
6381 return( PSA_ERROR_NOT_SUPPORTED );
6382 for( i = 0; i < domain_parameters_size; i++ )
6383 acc = ( acc << 8 ) | domain_parameters[i];
6384 if( acc > INT_MAX )
6385 return( PSA_ERROR_NOT_SUPPORTED );
6386 *exponent = acc;
6387 return( PSA_SUCCESS );
6388}
John Durkop07cc04a2020-11-16 22:08:34 -08006389#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006390
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006391static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006392 psa_key_slot_t *slot, size_t bits,
6393 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006394{
Gilles Peskine8e338702019-07-30 20:06:31 +02006395 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006396
Gilles Peskinee56e8782019-04-26 17:34:02 +02006397 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006398 return( PSA_ERROR_INVALID_ARGUMENT );
6399
Gilles Peskinee59236f2018-01-27 23:32:46 +01006400 if( key_type_is_raw_bytes( type ) )
6401 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006402 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006403
6404 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006405 if( status != PSA_SUCCESS )
6406 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006407
6408 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006409 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6410 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006411 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006412
Ronald Cronea0f8a62020-11-25 17:52:23 +01006413 status = psa_generate_random( slot->key.data,
6414 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006415 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006416 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006417
6418 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006419#if defined(MBEDTLS_DES_C)
6420 if( type == PSA_KEY_TYPE_DES )
Ronald Cronea0f8a62020-11-25 17:52:23 +01006421 psa_des_set_key_parity( slot->key.data,
6422 slot->key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006423#endif /* MBEDTLS_DES_C */
6424 }
6425 else
6426
John Durkop07cc04a2020-11-16 22:08:34 -08006427#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006428 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006429 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006430 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006432 int exponent;
6433 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006434 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6435 return( PSA_ERROR_NOT_SUPPORTED );
6436 /* Accept only byte-aligned keys, for the same reasons as
6437 * in psa_import_rsa_key(). */
6438 if( bits % 8 != 0 )
6439 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006440 status = psa_read_rsa_exponent( domain_parameters,
6441 domain_parameters_size,
6442 &exponent );
6443 if( status != PSA_SUCCESS )
6444 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006445 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6446 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006447 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006448 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006449 (unsigned int) bits,
6450 exponent );
6451 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006452 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006453
6454 /* Make sure to always have an export representation available */
6455 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6456
Steven Cooreman75b74362020-07-28 14:30:13 +02006457 status = psa_allocate_buffer_to_slot( slot, bytes );
6458 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006459 {
6460 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006461 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006462 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006463
Ronald Cron3c8ca3a2020-11-26 16:45:24 +01006464 status = mbedtls_psa_rsa_export_key( type,
6465 &rsa,
6466 slot->key.data,
6467 bytes,
6468 &slot->key.bytes );
Steven Cooremana01795d2020-07-24 22:48:15 +02006469 mbedtls_rsa_free( &rsa );
6470 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006471 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006472 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006473 }
6474 else
John Durkop07cc04a2020-11-16 22:08:34 -08006475#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006476
John Durkop9814fa22020-11-04 12:28:15 -08006477#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006478 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006479 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006480 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006481 mbedtls_ecp_group_id grp_id =
6482 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006483 const mbedtls_ecp_curve_info *curve_info =
6484 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006485 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006487 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006488 return( PSA_ERROR_NOT_SUPPORTED );
6489 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6490 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006491 mbedtls_ecp_keypair_init( &ecp );
6492 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006493 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006494 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006495 if( ret != 0 )
6496 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006497 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006498 return( mbedtls_to_psa_error( ret ) );
6499 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006500
6501
6502 /* Make sure to always have an export representation available */
6503 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006504 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6505 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006506 {
6507 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006508 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006509 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006510
6511 status = mbedtls_to_psa_error(
Ronald Cronea0f8a62020-11-25 17:52:23 +01006512 mbedtls_ecp_write_key( &ecp, slot->key.data, bytes ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02006513
6514 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006515 if( status != PSA_SUCCESS ) {
Ronald Cronea0f8a62020-11-25 17:52:23 +01006516 memset( slot->key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006517 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006518 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006519 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006520 }
6521 else
John Durkop9814fa22020-11-04 12:28:15 -08006522#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006523 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006524 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006525 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006526
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006527 return( PSA_SUCCESS );
6528}
Darryl Green0c6575a2018-11-07 16:05:30 +00006529
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006530psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006531 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006532{
6533 psa_status_t status;
6534 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006535 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006536
Ronald Cron81709fc2020-11-14 12:10:32 +01006537 *key = MBEDTLS_SVC_KEY_ID_INIT;
6538
Gilles Peskine0f84d622019-09-12 19:03:13 +02006539 /* Reject any attempt to create a zero-length key so that we don't
6540 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6541 if( psa_get_key_bits( attributes ) == 0 )
6542 return( PSA_ERROR_INVALID_ARGUMENT );
6543
Ronald Cron81709fc2020-11-14 12:10:32 +01006544 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6545 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006546 if( status != PSA_SUCCESS )
6547 goto exit;
6548
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006549 status = psa_driver_wrapper_generate_key( attributes,
6550 slot );
6551 if( status != PSA_ERROR_NOT_SUPPORTED ||
6552 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6553 goto exit;
6554
6555 status = psa_generate_key_internal(
6556 slot, attributes->core.bits,
6557 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006558
6559exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006560 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006561 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006562 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006563 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006564
Darryl Green0c6575a2018-11-07 16:05:30 +00006565 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006566}
6567
6568
Gilles Peskinee59236f2018-01-27 23:32:46 +01006569
6570/****************************************************************/
6571/* Module setup */
6572/****************************************************************/
6573
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006574#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006575psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6576 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6577 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6578{
6579 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6580 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006581 global_data.rng.entropy_init = entropy_init;
6582 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006583 return( PSA_SUCCESS );
6584}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006585#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006586
Gilles Peskinee59236f2018-01-27 23:32:46 +01006587void mbedtls_psa_crypto_free( void )
6588{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006589 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006590 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6591 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006592 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006593 }
6594 /* Wipe all remaining data, including configuration.
6595 * In particular, this sets all state indicator to the value
6596 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006597 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006598#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006599 /* Unregister all secure element drivers, so that we restart from
6600 * a pristine state. */
6601 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006602#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006603}
6604
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006605#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6606/** Recover a transaction that was interrupted by a power failure.
6607 *
6608 * This function is called during initialization, before psa_crypto_init()
6609 * returns. If this function returns a failure status, the initialization
6610 * fails.
6611 */
6612static psa_status_t psa_crypto_recover_transaction(
6613 const psa_crypto_transaction_t *transaction )
6614{
6615 switch( transaction->unknown.type )
6616 {
6617 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6618 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006619 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006620 * is implemented.
6621 * https://github.com/ARMmbed/mbed-crypto/issues/218
6622 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006623 default:
6624 /* We found an unsupported transaction in the storage.
6625 * We don't know what state the storage is in. Give up. */
6626 return( PSA_ERROR_STORAGE_FAILURE );
6627 }
6628}
6629#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6630
Gilles Peskinee59236f2018-01-27 23:32:46 +01006631psa_status_t psa_crypto_init( void )
6632{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006633 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006634
Gilles Peskinec6b69072018-11-20 21:42:52 +01006635 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006636 if( global_data.initialized != 0 )
6637 return( PSA_SUCCESS );
6638
Gilles Peskine30524eb2020-11-13 17:02:26 +01006639 /* Initialize and seed the random generator. */
6640 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006641 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006642 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006643 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006644 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006645 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006646
Gilles Peskine66fb1262018-12-10 16:29:04 +01006647 status = psa_initialize_key_slots( );
6648 if( status != PSA_SUCCESS )
6649 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006650
Gilles Peskined9348f22019-10-01 15:22:29 +02006651#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6652 status = psa_init_all_se_drivers( );
6653 if( status != PSA_SUCCESS )
6654 goto exit;
6655#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6656
Gilles Peskine4b734222019-07-24 15:56:31 +02006657#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006658 status = psa_crypto_load_transaction( );
6659 if( status == PSA_SUCCESS )
6660 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006661 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6662 if( status != PSA_SUCCESS )
6663 goto exit;
6664 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006665 }
6666 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6667 {
6668 /* There's no transaction to complete. It's all good. */
6669 status = PSA_SUCCESS;
6670 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006671#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006672
Gilles Peskinec6b69072018-11-20 21:42:52 +01006673 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006674 global_data.initialized = 1;
6675
6676exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006677 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006678 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006679 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006680}
6681
6682#endif /* MBEDTLS_PSA_CRYPTO_C */